# Getting no. of hits to be Zero even when total hits is non-zero value

**URL:** https://discuss.elastic.co/t/getting-no-of-hits-to-be-zero-even-when-total-hits-is-non-zero-value/25994
**Category:** Elasticsearch
**Created:** [July 21, 2015, 2:40pm UTC](https://discuss.elastic.co/t/getting-no-of-hits-to-be-zero-even-when-total-hits-is-non-zero-value/25994 "2015-07-21T14:40:16Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![apanimesh061](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/apanimesh061/32/927_2.png) [@apanimesh061](https://discuss.elastic.co/u/apanimesh061)
#### Post date: [July 21, 2015, 2:40pm UTC](https://discuss.elastic.co/t/getting-no-of-hits-to-be-zero-even-when-total-hits-is-non-zero-value/25994/1 "2015-07-21T14:40:16Z")

</div>

I am using Java API for sending search requests.  
`System.out.println(searchResponse.getHits().totalHits());` gives me a non-zero number which I confirmed is correct as I checked it on Sense as well. But as soon as try to run a loop on the hits like `for (SearchHit hit : searchResponse.getHits()) {...}` or try to access the hits individually `searchResponse.getHits().getAt(0).getSource().get("doc_id");` I get `java.lang.ArrayIndexOutOfBoundsException`.

I don't know why is this happening.

---

<div class="post-metadata">

### Author: ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)
#### Post date: [July 21, 2015, 5:36pm UTC](https://discuss.elastic.co/t/getting-no-of-hits-to-be-zero-even-when-total-hits-is-non-zero-value/25994/2 "2015-07-21T17:36:15Z")

</div>

totalHits is the total number of matching documents. If you are paginating and have a value of `from` that is greater than or equal to totalHits, then your hits will be empty.

---

<div class="post-metadata">

### Author: ![apanimesh061](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/apanimesh061/32/927_2.png) [@apanimesh061](https://discuss.elastic.co/u/apanimesh061)
#### Post date: [July 21, 2015, 5:50pm UTC](https://discuss.elastic.co/t/getting-no-of-hits-to-be-zero-even-when-total-hits-is-non-zero-value/25994/3 "2015-07-21T17:50:42Z")

</div>

Is there any way I can solve this. I actually have not set value to `setFrom` but I have `setSize` as 10. And no. of hits that I get are always less than 10. How should I solve this issue?

I tried reducing the value of `setSize` to 1 but still I am getting this error!

UPDATE: I set both `setFrom` and `setSize` to 1 and it does not give this error now

---

<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: [July 6, 2017, 12:00am UTC](https://discuss.elastic.co/t/getting-no-of-hits-to-be-zero-even-when-total-hits-is-non-zero-value/25994/4 "2017-07-06T00:00:07Z")

</div>


