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

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.

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.

1 Like

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