# Elastic KNN search questions

**URL:** <https://discuss.elastic.co/t/elastic-knn-search-questions/344684>\
**Category:** Elasticsearch\
**Tags:** vector-search\
**Created:** [October 9, 2023, 5:41pm UTC](https://discuss.elastic.co/t/elastic-knn-search-questions/344684 "2023-10-09T17:41:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![AdarshPrabhakara](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@AdarshPrabhakara](https://discuss.elastic.co/u/AdarshPrabhakara)\
**Post date:** [October 9, 2023, 5:41pm UTC](https://discuss.elastic.co/t/elastic-knn-search-questions/344684/1 "2023-10-09T17:41:25Z")

</div>

I am looking into using Elastic KNN search feature and from what I see this is how we query ES for KNN search.

```auto
GET my-index/_knn_search
{
  "knn": {
    "field": "image_vector",
    "query_vector": [0.3, 0.1, 1.2],
    "k": 10,
    "num_candidates": 100
  },
  "_source": ["name", "file_type"]
}

```

Here **num\_candidates** has a max limit of 10000 and from ES documents I see this - `The number of nearest neighbor candidates to consider per shard. Cannot exceed 10,000. Elasticsearch collects num_candidates results from each shard, then merges them to find the top k results. Increasing num_candidates tends to improve the accuracy of the final k results.`

The above is not very clear to me. Here are some questions:

1. How is the 10,000 candidates chosen?
2. If we have 1M vector documents, to search across all of these should we pick like 100 shards, so that each shard has max 10k documents? We need very pretty good recall on the retrieved results.
3. Their [documents](https://www.elastic.co/guide/en/elasticsearch/reference/current/size-your-shards.html) on picking shard strategy says too many smaller shards is bad and they have their own overhead. So how do we pick shard sizes when we have this limit of 10k candidates per shards for KNN?

Any advice/suggestions are appreciated, thanks.

---

<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:** [October 9, 2023, 6:58pm UTC](https://discuss.elastic.co/t/elastic-knn-search-questions/344684/2 "2023-10-09T18:58:55Z")

</div>

Hey @AdarshPrabhakara ,

> 1. How is the 10,000 candidates chosen?

`num_candidates` is the same idea as `efSearch`. Its the number of candidates we continue to keep track of while searching the HNSW graph per shard. This number is applied per shard.

> 1. If we have 1M vector documents, to search across all of these should we pick like 100 shards, so that each shard has max 10k documents? We need very pretty good recall on the retrieved results.

I would say not. 1M vectors should fit in a single shard. HNSW is really good at providing high recall even in larger graphs.

> So how do we pick shard sizes when we have this limit of 10k candidates per shards for KNN?

I would say you shouldn't.

If you are wanting 100% recall, then you probably don't want to index the vectors at all and just use brute-force. But keep in mind this scales linearly, where HNSW scales logarithmically and provides much faster query speeds.

---

<div class="post-metadata">

**Author:** ![AdarshPrabhakara](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@AdarshPrabhakara](https://discuss.elastic.co/u/AdarshPrabhakara)\
**Post date:** [October 9, 2023, 7:14pm UTC](https://discuss.elastic.co/t/elastic-knn-search-questions/344684/3 "2023-10-09T19:14:12Z")

</div>

Awesome, that is helpful. Thank you. I did not find this clarity in any official documentation.

---

<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:** [November 6, 2023, 7:14pm UTC](https://discuss.elastic.co/t/elastic-knn-search-questions/344684/4 "2023-11-06T19:14:52Z")

</div>

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