Tag Archives: totalHits

[Solved] Hits[] of ElasticSearch SearchResponse is always one less record than totalHits

During elasticsearch query, it is found that hits []. Length is always 1 less than totalhits. Value
the code is as follows:

  SearchRequest request = new SearchRequest("indexName");
  SearchSourceBuilder builder = new SearchSourceBuilder();
  BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
  builder.query(QueryBuilder.matchPhraseQuery("fieldName","searchStr"));
  builder.from(1);
  builder.size(10);
  request.source(builder);

  SearchResponse response = restHighLevelClient.search(request,RequestOptions.DEFAULT);
  // Get the total number of queries
  System.out.println(response.getHits().getTotalHits().value);
  // Get the number of query paging
  System.out.println(response.getHits().getHits().length);

Output results:

The second line is always 1 less than the first line.

According to the idea of conventional paging, it should actually start from the first page and then keep up with the data of each page. Unexpectedly, from (1) directly skips the first data here.

Solution:
it’s OK to directly from (0).