# Counting total hits with java always retrieve 0

**URL:** https://discuss.elastic.co/t/counting-total-hits-with-java-always-retrieve-0/197633
**Category:** Elasticsearch
**Created:** [September 2, 2019, 12:35am UTC](https://discuss.elastic.co/t/counting-total-hits-with-java-always-retrieve-0/197633 "2019-09-02T00:35:38Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![lsdany](https://avatars.discourse-cdn.com/v4/letter/l/e36b37/32.png) [@lsdany](https://discuss.elastic.co/u/lsdany)
#### Post date: [September 2, 2019, 12:35am UTC](https://discuss.elastic.co/t/counting-total-hits-with-java-always-retrieve-0/197633/1 "2019-09-02T00:35:38Z")

</div>

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](https://us1.discourse-cdn.com/elastic/original/3X/4/6/46b574cb213ef5c10cc5568a3807e902cfffa48a.png) ![image](https://us1.discourse-cdn.com/elastic/original/3X/4/6/46b574cb213ef5c10cc5568a3807e902cfffa48a.png)

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?

---

<div class="post-metadata">

### Author: ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)
#### Post date: [September 2, 2019, 8:22am UTC](https://discuss.elastic.co/t/counting-total-hits-with-java-always-retrieve-0/197633/2 "2019-09-02T08:22:51Z")

</div>

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

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [September 30, 2019, 8:23am UTC](https://discuss.elastic.co/t/counting-total-hits-with-java-always-retrieve-0/197633/3 "2019-09-30T08:23:06Z")

</div>

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