1
searchSourceBuilder.query(query).sort(sb).from(fromField).size(sizeField);
searchRequest.indices(elasticsearchUserIndex).source(searchSourceBuilder);
SearchResponse searchResponse = elasticSearchConfig.client().search(searchRequest, RequestOptions.DEFAULT);
SearchHit[] hits = searchResponse.getHits().getHits();
Long totalCount = searchResponse.getHits().getTotalHits();
List<Map<String, Object>> list = new ArrayList<>();
for (SearchHit value: hits) {
    list.add(value.getSourceAsMap());
    }

That is the code I'm using. Once I've set the from and the size parameter I get the hits according to the size set. But to add the total page count in response I need the total hits for that query.

How to get total result count when setFrom is used in elasticsearch QueryBuilders?

Tried to use the solution from that thread but I'm getting total hits as 0 even though I get the hits according to the size. totalCount is returning 0

Aditya K
  • 135
  • 2
  • 4
  • 10

3 Answers3

1

You need to set track_total_hits to true and you will be able to get total hits for response.

SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchAllQuery()).from(0).size(10);
searchSourceBuilder.trackTotalHits(true);
searchRequest.source(searchSourceBuilder);
Long totalCount = searchResponse.getHits().getTotalHits();
Sagar Patel
  • 4,993
  • 1
  • 8
  • 19
  • `searchSourceBuilder.query(query).sort(sb).from(fromField).size(sizeField); searchSourceBuilder.trackTotalHits(true); searchRequest.indices(elasticsearchUserIndex).source(searchSourceBuilder); SearchResponse searchResponse = elasticSearchConfig.client().search(searchRequest, RequestOptions.DEFAULT); SearchHit[] hits = searchResponse.getHits().getHits(); Long totalCount = searchResponse.getHits().getTotalHits();` Tried this but still giving me count as 0 – Aditya K Apr 28 '22 at 11:35
  • which version of ES ? also see `searchResponse.getHits().getTotalHits().value;` this line. I have added `value` in last – Sagar Patel Apr 28 '22 at 11:37
  • compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-elasticsearch', version: '2.2.2.RELEASE'; I don't have an option to add value. Also when I look into the searchresponse object it shows totalHits as 0 – Aditya K Apr 28 '22 at 11:41
  • I can't change the ES version, is there some other method to add with this ES version? – Aditya K Apr 28 '22 at 11:42
  • searchSourceBuilder.query(query).sort(sb).trackTotalHits(true).from(fromField).size(sizeField); tried this too just in case, no luck – Aditya K Apr 28 '22 at 11:43
  • Ya i can understand you can not change version but i am asking ES version because accordingly i will help – Sagar Patel Apr 28 '22 at 11:45
  • That is the spring dependency I'm using – Aditya K Apr 28 '22 at 11:45
  • Yes i know that you are using spring dependancy from exxception but i am asking Elasticsearch version. Because sometime due to version conflic issue occrs. Can you try with `matchAll` query and check. may be possible that for query youare passing for that no document available – Sagar Patel Apr 28 '22 at 11:47
  • So I was testing with size 2, I was getting 2 hits but I know for that query the total hits are 9. There are documents available for this query – Aditya K Apr 28 '22 at 11:49
  • I am not sure because same code i have run with `spring-boot-starter-data-elasticsearch` with version `2.2.2.RELEASE` and i am able to get hit count. it will be great if you can tell about Elasticsearch version. I have tested code on ES version 7.10 – Sagar Patel Apr 28 '22 at 11:54
  • I'm checking my jars, it says ES client is 6.4.3 and spring-data-elasticsearch is 3.1.5 RELEASE. – Aditya K Apr 28 '22 at 12:02
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/244292/discussion-between-aditya-k-and-sagar-patel). – Aditya K Apr 28 '22 at 12:05
0

Changed my ES version to 7.3.0. With it had to update my rest-high-level-client to 7.3.0 as well.

references: https://discuss.elastic.co/t/issue-with-high-level-rest-client-api/195853

Aditya K
  • 135
  • 2
  • 4
  • 10
0

with new Version client here is how to get the total count

Long totalCount = searchResponse.getHits().getTotalHits().value;

this error popped up in idea java: long cannot be dereferenced

Ramkumar Pillai
  • 154
  • 2
  • 8