# The num\_candidates parameter leads to some confusing query results

**URL:** <https://discuss.elastic.co/t/the-num-candidates-parameter-leads-to-some-confusing-query-results/359355>\
**Category:** Elasticsearch\
**Tags:** vector-search\
**Created:** [May 13, 2024, 8:57am UTC](https://discuss.elastic.co/t/the-num-candidates-parameter-leads-to-some-confusing-query-results/359355 "2024-05-13T08:57:00Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![EricTowns](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/erictowns/32/131926_2.png) [@EricTowns](https://discuss.elastic.co/u/EricTowns)\
**Post date:** [May 13, 2024, 8:57am UTC](https://discuss.elastic.co/t/the-num-candidates-parameter-leads-to-some-confusing-query-results/359355/1 "2024-05-13T08:57:00Z")

</div>

Hi team!  
I have an index, only 1 primary shard, I insert documents and then knn searching. When I do a knn query with k=10 and num\_candidates=20, I get a batch of results. When k=10 and num\_candidates=25, I got some documents with higher scores, which confused me, num\_candidates is supposed to be the top n closest documents per shard, Why don't the documents with higher scores for num\_candidates=25 appear in the results for num\_candidates=20

Elasticsearch version : 8.7.0

result:

 ![n20n25](https://us1.discourse-cdn.com/elastic/original/3X/6/3/63205a3e9f892c3bd69fe4ef37bcc02775511f22.png)

---

<div class="post-metadata">

**Author:** ![BenTrent](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bentrent/32/33915_2.png) [@BenTrent](https://discuss.elastic.co/u/BenTrent)\
**Post date:** [May 13, 2024, 11:22am UTC](https://discuss.elastic.co/t/the-num-candidates-parameter-leads-to-some-confusing-query-results/359355/2 "2024-05-13T11:22:37Z")

</div>

Hey @EricTowns , `num_candidates` is the number of vectors searched per shard. It is the same as the `efSearch` parameter in the original HNSW paper: [[1603.09320] Efficient and robust approximate nearest neighbor search using Hierarchical Navigable Small World graphs](https://arxiv.org/abs/1603.09320)

Meaning, it helps control the approximate nature of the search. Higher `num_candidate` more time is spent exploring the graph, but a more accurate set of neighbors is returned. Lower `num_candidates` is faster search, but less accurate.

---

<div class="post-metadata">

**Author:** ![EricTowns](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/erictowns/32/131926_2.png) [@EricTowns](https://discuss.elastic.co/u/EricTowns)\
**Post date:** [May 13, 2024, 1:12pm UTC](https://discuss.elastic.co/t/the-num-candidates-parameter-leads-to-some-confusing-query-results/359355/3 "2024-05-13T13:12:48Z")

</div>

Thank you for your answer. I have another question. Sometimes, the first time using knn can not search, but I am sure the doc has been stored successfully. When I rewrite it again, the doc can be searched, and the search vector is the same. Give advice freely!

---

<div class="post-metadata">

**Author:** ![BenTrent](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bentrent/32/33915_2.png) [@BenTrent](https://discuss.elastic.co/u/BenTrent)\
**Post date:** [May 13, 2024, 1:54pm UTC](https://discuss.elastic.co/t/the-num-candidates-parameter-leads-to-some-confusing-query-results/359355/4 "2024-05-13T13:54:28Z")

</div>

> Sometimes, the first time using knn can not search, but I am sure the doc has been stored successfully

Do you mean the vector isn't in the result set? Or that the search throws an exception and returns an error?

Elasticsearch has a refresh & API: [Refresh API | Elasticsearch Guide [8.13] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-refresh.html)

If you want things available immediately, there are various ways to ensure things are searchable immediate. You can wait for a refresh on search, or index, or force one at index time or manually call this API. All have various costs and considerations.

But I think your issue is simply that a refresh hasn't occurred between the index & the search.

---

<div class="post-metadata">

**Author:** ![EricTowns](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/erictowns/32/131926_2.png) [@EricTowns](https://discuss.elastic.co/u/EricTowns)\
**Post date:** [May 14, 2024, 2:10am UTC](https://discuss.elastic.co/t/the-num-candidates-parameter-leads-to-some-confusing-query-results/359355/5 "2024-05-14T02:10:19Z")

</div>

The phenomenon is:  
For example, if i vectorize the text "i am a doctor" and store it, then when searching,

1. I vectorize the question "doctor" and perform a knn search. At this time, no search was found,
2. I vectorize the original text "i am a doctor" and perform a knn search, it works.
3. then I rewrite this vector, and then I do the first search, which is "doctor", and then I get the search,  
and these searches k, num\_candidates, these are the same, which is confusing to me, Is this also due to the concept of "nearest neighbor"? At first, I thought that the \_score of the search results could be used as the degree of "neighbor", but now it seems that it is not?

Looking forward to your answer, It bothered me for a long time

---

<div class="post-metadata">

**Author:** ![BenTrent](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bentrent/32/33915_2.png) [@BenTrent](https://discuss.elastic.co/u/BenTrent)\
**Post date:** [May 14, 2024, 11:46am UTC](https://discuss.elastic.co/t/the-num-candidates-parameter-leads-to-some-confusing-query-results/359355/6 "2024-05-14T11:46:49Z")

</div>

> 1. I vectorize the question "doctor" and perform a knn search. At this time, no search was found,

Does this mean no data at all? That doesn't make sense as you should always get `k` neighbors back.

Or are you talking about a particular neighbor that you are interested in?

Are you forcing a refresh before searching in step 1.?

> Is this also due to the concept of "nearest neighbor"?

No. `_score` is determined by vector similarity.

---

<div class="post-metadata">

**Author:** ![EricTowns](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/erictowns/32/131926_2.png) [@EricTowns](https://discuss.elastic.co/u/EricTowns)\
**Post date:** [May 14, 2024, 4:49pm UTC](https://discuss.elastic.co/t/the-num-candidates-parameter-leads-to-some-confusing-query-results/359355/7 "2024-05-14T16:49:00Z")

</div>

> [@BenTrent](#):
>
> Does this mean no data at all? That doesn't make sense as you should always get `k` neighbors back.

Oh, that's a mistake in my description, what I meant by not being able to find "I'm a doctor" is that there were a bunch of vectors, and I used a vector to search for it, set the value of k, and the first time I searched for it I didn't find him in the top K results, and when I rewrote the vector I was able to find it in the top K results, and it had a higher score, and was ranked higher.

---

<div class="post-metadata">

**Author:** ![Alex\_Salgado-Elastic](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alex_salgado-elastic/32/103081_2.png) [@Alex\_Salgado-Elastic](https://discuss.elastic.co/u/Alex_Salgado-Elastic)\
**Post date:** [June 9, 2024, 12:17pm UTC](https://discuss.elastic.co/t/the-num-candidates-parameter-leads-to-some-confusing-query-results/359355/8 "2024-06-09T12:17:04Z")

</div>

Hello @EricTowns, is the error still happening? If so, could you send your query here and share a screenshot of the two behaviors?
