# Sometimes no documents are returned in Elasticsearch

**URL:** <https://discuss.elastic.co/t/sometimes-no-documents-are-returned-in-elasticsearch/377425>\
**Category:** Elasticsearch\
**Created:** [April 23, 2025, 9:34am UTC](https://discuss.elastic.co/t/sometimes-no-documents-are-returned-in-elasticsearch/377425 "2025-04-23T09:34:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sushmita\_Gupta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sushmita_gupta/32/142757_2.png) [@Sushmita\_Gupta](https://discuss.elastic.co/u/Sushmita_Gupta)\
**Post date:** [April 23, 2025, 9:34am UTC](https://discuss.elastic.co/t/sometimes-no-documents-are-returned-in-elasticsearch/377425/1 "2025-04-23T09:34:47Z")

</div>

Hi,

I'm using Elasticsearch and I’ve noticed something strange. For some queries, I get **no documents back** , even though I have set `top_k = 4`. What’s confusing is that sometimes the same query **does return results** , but other times it doesn't.

Out of 100 queries, a few of them return nothing at all.

Has anyone seen this happen before? Any idea what might be causing it?

Thanks for your help!

---

<div class="post-metadata">

**Author:** ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)\
**Post date:** [April 23, 2025, 9:36am UTC](https://discuss.elastic.co/t/sometimes-no-documents-are-returned-in-elasticsearch/377425/2 "2025-04-23T09:36:19Z")

</div>

Hi @Sushmita_Gupta,

Welcome! Which version of Elasticsearch are you using? Can you share the query you are running and the documents you expect to match `top_k=4`?

Let us know!

---

<div class="post-metadata">

**Author:** ![Sushmita\_Gupta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sushmita_gupta/32/142757_2.png) [@Sushmita\_Gupta](https://discuss.elastic.co/u/Sushmita_Gupta)\
**Post date:** [April 23, 2025, 4:43pm UTC](https://discuss.elastic.co/t/sometimes-no-documents-are-returned-in-elasticsearch/377425/3 "2025-04-23T16:43:15Z")

</div>

Hi @carly.richmond,  
I am currently using Elasticsearch version 8.8.1 on my local machine (not on the cloud) in a development server. I ran 100 queries, and out of them, 14 queries did not retrieve any documents. I repeated the experiment with the same 100 queries, and this time, only 4 queries did not retrieve any documents.

---

<div class="post-metadata">

**Author:** ![elasticforme](https://avatars.discourse-cdn.com/v4/letter/e/f05b48/32.png) [@elasticforme](https://discuss.elastic.co/u/elasticforme)\
**Post date:** [April 23, 2025, 6:31pm UTC](https://discuss.elastic.co/t/sometimes-no-documents-are-returned-in-elasticsearch/377425/4 "2025-04-23T18:31:56Z")

</div>

timeout? it will return zero when query is not finish in time and gets kill? is it big dataset that you are query?

---

<div class="post-metadata">

**Author:** ![Sushmita\_Gupta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sushmita_gupta/32/142757_2.png) [@Sushmita\_Gupta](https://discuss.elastic.co/u/Sushmita_Gupta)\
**Post date:** [April 24, 2025, 4:20am UTC](https://discuss.elastic.co/t/sometimes-no-documents-are-returned-in-elasticsearch/377425/5 "2025-04-24T04:20:19Z")

</div>

No, it's not a very large dataset — it's only 10 GB of data in Elasticsearch.

---

<div class="post-metadata">

**Author:** ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)\
**Post date:** [April 24, 2025, 6:05am UTC](https://discuss.elastic.co/t/sometimes-no-documents-are-returned-in-elasticsearch/377425/6 "2025-04-24T06:05:00Z")

</div>

Can you share an example query and documents that you expect to be returned for us to investigate? It's very difficult to say what's happening without more information.

You could also try investigating the query using the Search Profiler in Kibana or the profile API.

Hope that helps!

---

<div class="post-metadata">

**Author:** ![Sushmita\_Gupta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sushmita_gupta/32/142757_2.png) [@Sushmita\_Gupta](https://discuss.elastic.co/u/Sushmita_Gupta)\
**Post date:** [April 24, 2025, 12:57pm UTC](https://discuss.elastic.co/t/sometimes-no-documents-are-returned-in-elasticsearch/377425/7 "2025-04-24T12:57:28Z")

</div>

I used the query profiler and in the case of no documents a **MatchNoDocQuery** was invoked, which states that no documents were found, the current query is a pre-filter query which is used to filter the docs first and then run a knn search on those filtered documents.

Please find the query below

```auto
elastic_query = {
    "size": 4,
    "knn": {
        "field": "embedding",
        "k": 4,
        "query_vector": embedded_query,  
        "num_candidates": 1000,
        "filter": {
            "term": {
                "meta._bot": _bot
            }
        },
    },
    "_source": ["text", "meta"],
    "profile": True
}

```

When I tried increasing num\_candidates the query started working, and also when I reindexed the docs, still the query returned me proper docs. Now I am confused like what could be the reason for such behaviour, is the HSNW graph traversal unoptimal or what?

Also when I read the profiler, in case of faulty index, the num\_candidates were 100 but the profiler told that for response generation the actual number of docs used for query were 33 only, but when I kept the num\_candidates equal to the total no of docs in the index, it worked?, but the same query with 100 num\_candidates worked on a new index with reindexed docs.
