# How to handle "ef" and "num\_candidates" parameters in hnsw search

**URL:** <https://discuss.elastic.co/t/how-to-handle-ef-and-num-candidates-parameters-in-hnsw-search/318681>\
**Category:** Elasticsearch\
**Tags:** docker, vector-search\
**Created:** [November 10, 2022, 6:02pm UTC](https://discuss.elastic.co/t/how-to-handle-ef-and-num-candidates-parameters-in-hnsw-search/318681 "2022-11-10T18:02:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![wole](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wole/32/113085_2.png) [@wole](https://discuss.elastic.co/u/wole)\
**Post date:** [November 10, 2022, 6:02pm UTC](https://discuss.elastic.co/t/how-to-handle-ef-and-num-candidates-parameters-in-hnsw-search/318681/1 "2022-11-10T18:02:44Z")

</div>

Hi team,

I am in the process of learning how to use ANN search (with HNSW) on Elasticsearch: in order to do so I am comparing the results I obtain with Elasticsearch and the faiss implementation of the algorithm (using the IndexHNSWFlat index).  
I understand and know how to set the parameters _M_ and _ef\_construction_ using index\_options.  
However, there should be another parameter, called simply _ef_, which is similar to _ef\_construction_, but used during the search operations: they talk about it in the [original HNSW article](https://arxiv.org/pdf/1603.09320.pdf) (pg. 4 and 5) and it is called _ef\_search_ in the [faiss code](https://github.com/facebookresearch/faiss/blob/main/faiss/impl/HNSW.cpp#L52).  
I did not find a way to set _ef_, since it does not appear in index\_options: how to do so? Also, what is its default value?

There is another parameter which troubles me: _num\_candidates_. In the [knn-search guide](https://www.elastic.co/guide/en/elasticsearch/reference/current/knn-search.html) it is written as follows:

> To gather results, the kNN search API finds a `num_candidates` number of approximate nearest neighbor candidates on each shard. The search computes the similarity of these candidate vectors to the query vector, selecting the `k` most similar results from each shard. The search then merges the results from each shard to return the global top `k` nearest neighbors.

In my case, since I am just running tests to compare Elasticsearch and Faiss results, I am running on the Elasticsearch docker image with 1 shard: am I right to assume that in this case num\_candidates parameter is irrelevant?  
In all the examples I always see _num\_candidates_ to be 10 times _k_: is there a reason for that?

Thank you very much

---

<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:** [November 10, 2022, 6:23pm UTC](https://discuss.elastic.co/t/how-to-handle-ef-and-num-candidates-parameters-in-hnsw-search/318681/2 "2022-11-10T18:23:36Z")

</div>

@wole Thanks for digging into KNN search!

You are correct on the parallel values between `M` and `ef_construction`. At search time `ef` indicates how many candidates to consider while gathering your top `K`.

In Elasticsearch, instead of `ef`, we provide `num_candidates`. So, in comparing with FAISS, where they use `ef` at search time, you should use the same value in `num_candidates`

---

<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:** [December 8, 2022, 6:24pm UTC](https://discuss.elastic.co/t/how-to-handle-ef-and-num-candidates-parameters-in-hnsw-search/318681/3 "2022-12-08T18:24:35Z")

</div>

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