Hi,
I ran into a scenario today with relatively old and robust code where the Search API returned totalHits of 1 (as expected) but hits array was empty.
The Java client threw the following exception when tying to get the first item:
final var hit = hits.getHits()[0];
Index 0 out of bounds for length 0
java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
The totalHits returned 1. So for sure I expected the hits array to be one result and not 0.
Unfortunately this exception is really inconsistent and only occurred several times today. I cannot seem to reproduce it.
I have googled and saw that there are quite a few threads about this kind of behavior but I could not find a thread that can really explain it. A very similar thread is this one: Getting no. of hits to be Zero even when total hits is non-zero value. The thread owner worked around it but no explanation was to why it was required.
Basically the Java Search request is like so:
SearchRequest{
  searchType=QUERY_THEN_FETCH,
  indices=[
    my_index
  ],
  indicesOptions=IndicesOptions[
    ignore_unavailable=false,
    allow_no_indices=true,
    expand_wildcards_open=true,
    expand_wildcards_closed=false,
    expand_wildcards_hidden=false,
    allow_aliases_to_multiple_indices=true,
    forbid_closed_indices=true,
    ignore_aliases=false,
    ignore_throttled=true
  ],
  types=[
    
  ],
  routing='null',
  preference='null',
  requestCache=null,
  scroll=null,
  maxConcurrentShardRequests=0,
  batchedReduceSize=512,
  preFilterShardSize=null,
  allowPartialSearchResults=null,
  localClusterAlias=null,
  getOrCreateAbsoluteStartMillis=-1,
  ccsMinimizeRoundtrips=true,
  enableFieldsEmulation=false,
  source={
    "query": {
      "bool": {
        "must": [
          {
            "term": {
              "my_id": {
                "value": "id_99079191836507153",
                "boost": 1.0
              }
            }
          }
        ],
        "adjust_pure_negative": true,
        "boost": 1.0
      }
    }
  }
}
So I have no 'scroll', 'from' nor 'size'.
I hope not related, but last week I upgraded to ES client and server to 7.17.4.
What is the cause of this, and how do I prevent it and ensure that the hits array is not empty when totalHits is greater than 0?
Thanks.