Counting total hits with java always retrieve 0

Hi, I'm using ES 6.4 with java, and I need to get the total hits from a query, but when I do that always retrieve 0 hits.

This is how I retrieve hits.

image image

SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
long hits = response.getHits().getTotalHits();

The query generated is:

{
"size": 0,
"query": {
"bool": {
"must": [
{
"match": {
"field1": {
"query": 7,
"operator": "OR",
"prefix_length": 0,
"max_expansions": 50,
"fuzzy_transpositions": true,
"lenient": false,
"zero_terms_query": "NONE",
"auto_generate_synonyms_phrase_query": true,
"boost": 1.0
}
}
}
],
"adjust_pure_negative": true,
"boost": 1.0
}
},
"aggregations": {
"avg1": {
"avg": {
"field": "avg1"
}
},
"avg2": {
"avg": {
"field": "avg2"
}
},
"avg3": {
"avg": {
"field": "avg3"
}
},
"avg4": {
"avg": {
"field": "avg4"
}
},
"sum1": {
"sum": {
"field": "sum1"
}
},
"sum2": {
"sum": {
"field": "sum2"
}
}
}
}

When debug the code I get

{
"took": 0,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits":
},
"aggregations": {
"avg#avg1": {
"value": 88.458405223703
},
"sum#sum1": {
"value": 1317512.0
},
"avg#avg2": {
"value": 93.01990059486538
},
"avg#avg3": {
"value": 91.52784388386483
},
"sum#sum2": {
"value": 118391.0
},
"avg#sum4": {
"value": 89.48119942884341
}
}
}

And If I take the same query in ES console I get:

{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 4202,
"relation" : "eq"
},
"max_score" : null,
"hits" :
}......

How can I get total number of hits?

The response from debugging is not possible. You cannot have zero hits, but have aggregations being calculated. Something is off here. size: 0 means, there is no hits array, but the total count should be right.

Can you try with a more recent version of the rest client and see if that keeps happening? also, against which elasticsearch version are you querying?

--Alex

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.