# Search API returns TotalHits 1 but zero hits in array

**URL:** https://discuss.elastic.co/t/search-api-returns-totalhits-1-but-zero-hits-in-array/308390
**Category:** Elasticsearch
**Created:** [June 28, 2022, 6:55pm UTC](https://discuss.elastic.co/t/search-api-returns-totalhits-1-but-zero-hits-in-array/308390 "2022-06-28T18:55:27Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![ewolfman](https://avatars.discourse-cdn.com/v4/letter/e/5f8ce5/32.png) [@ewolfman](https://discuss.elastic.co/u/ewolfman)
#### Post date: [June 28, 2022, 6:55pm UTC](https://discuss.elastic.co/t/search-api-returns-totalhits-1-but-zero-hits-in-array/308390/1 "2022-06-28T18:55:27Z")

</div>

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](https://discuss.elastic.co/t/getting-no-of-hits-to-be-zero-even-when-total-hits-is-non-zero-value/25994). The thread owner worked around it but no explanation was to why it was required.

Basically the Java Search request is like so:

```auto
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.

---

<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 26, 2022, 6:56pm UTC](https://discuss.elastic.co/t/search-api-returns-totalhits-1-but-zero-hits-in-array/308390/2 "2022-07-26T18:56:00Z")

</div>

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