6

When using elasticsearch 6.7,

GET /indexName/_search 

returns the exact total of the index:

"hits" : {
    "total" : 1527325,
    "max_score" : 1.0,
    ...}

But in elasticsearch 7.0

GET /indexName/_search

gets:

"hits" : {
    "total" : {
      "value" : 10000,
      "relation" : "gte"
    },

which means total is greater than 10000, so how can I get the exact total count of an index in 7.0?

2 Answers 2

10

As of ES 7.0.0, you need to use the track_total_hits parameter:

GET /indexName/_search?track_total_hits=true

You can also add the rest_total_hits_as_int parameter if you want to get back the same format as in pre-7 versions (temporary parameter that will be removed in ES 8):

GET /indexName/_search?track_total_hits=true&rest_total_hits_as_int=true
Sign up to request clarification or add additional context in comments.

Comments

4

For ES 7.5.0 using Java rest api client, you can use

sourceBuilder.trackTotalHits(true);

Preparing search request using the source builder

SearchRequest searchRequest = new SearchRequest(index);
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(queryBuilder);
sourceBuilder.sort(sortBuilder);
sourceBuilder.from(from); 
sourceBuilder.size(size); 
sourceBuilder.trackTotalHits(true);
searchRequest.source(sourceBuilder);
client.search(searchRequest,RequestOptions.DEFAULT);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.