# Elastic search sub\_searches with knn and rrf

**URL:** <https://discuss.elastic.co/t/elastic-search-sub-searches-with-knn-and-rrf/357804>\
**Category:** Elastic Search\
**Created:** [April 19, 2024, 3:21pm UTC](https://discuss.elastic.co/t/elastic-search-sub-searches-with-knn-and-rrf/357804 "2024-04-19T15:21:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![avranjan](https://avatars.discourse-cdn.com/v4/letter/a/bc79bd/32.png) [@avranjan](https://discuss.elastic.co/u/avranjan)\
**Post date:** [April 19, 2024, 3:21pm UTC](https://discuss.elastic.co/t/elastic-search-sub-searches-with-knn-and-rrf/357804/1 "2024-04-19T15:21:06Z")

</div>

Is there an example to use sub\_searches with knn? Why the k value is not allowed in query? Here is an example:

```auto
{
                    "_source": {"excludes": ["vector", "elser_vector"]},
                    "size": 1000,
                    "sub_searches": [
                        {
                            "query": {
                                "knn": {
                                    "field": "vector",
                                    "filter": [
                                        {
                                            "terms": {
                                                "metadata.document_id": [1,2,3]
                                            }
                                        }
                                    ],
                                    "query_vector": self.get_embedding().embed_query(
                                        "Who is the president of US?"
                                    ),
                                    "k" : 100 # k doesn't work
                                    "num_candidates": 1.5 * k, # k doesn't work here
                                }
                            }
                        },
                        {
                            "query": {
                                "bool": {
                                    "must": {
                                        "text_expansion": {
                                            "elser_vector": {
                                                "model_id": ".elser_model_2_linux-x86_64",
                                                "model_text": "Who is the president of US?",
                                            },
                                        }
                                    },
                                    "filter": [
                                        {
                                            "terms": {
                                                "metadata.document_id": [1, 2 ,3]
                                            }
                                        }
                                    ],
                                }
                            }
                        },
                        {
                            "query": {
                                "bool": {
                                    "must": {
                                        "match": {"text": {"query": "Who is the president of US?"}},
                                    },
                                    "filter": [
                                        {
                                            "terms": {
                                                "metadata.document_id": [1,2,3]
                                            }
                                        }
                                    ],
                                }
                            }
                        },
                    ],
                    "rank": {"rrf": {"window_size": 1000, "rank_constant": 100}},
                }

```

---

<div class="post-metadata">

**Author:** ![Kathleen\_DeRusso](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kathleen_derusso/32/132039_2.png) [@Kathleen\_DeRusso](https://discuss.elastic.co/u/Kathleen_DeRusso)\
**Post date:** [April 22, 2024, 3:52pm UTC](https://discuss.elastic.co/t/elastic-search-sub-searches-with-knn-and-rrf/357804/2 "2024-04-22T15:52:56Z")

</div>

Hey there @avranjan thanks for the question! In the case of knn as a query, k is not sent in directly as k is determined by size.

---

<div class="post-metadata">

**Author:** ![avranjan](https://avatars.discourse-cdn.com/v4/letter/a/bc79bd/32.png) [@avranjan](https://discuss.elastic.co/u/avranjan)\
**Post date:** [April 22, 2024, 4:48pm UTC](https://discuss.elastic.co/t/elastic-search-sub-searches-with-knn-and-rrf/357804/3 "2024-04-22T16:48:58Z")

</div>

Thanks @Kathleen_DeRusso . Is there a recommended way to dynamically configure below properties in elastic with vector search?

```auto
k
num_candidates
window_size
rank_constant

```

---

<div class="post-metadata">

**Author:** ![Kathleen\_DeRusso](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kathleen_derusso/32/132039_2.png) [@Kathleen\_DeRusso](https://discuss.elastic.co/u/Kathleen_DeRusso)\
**Post date:** [April 24, 2024, 6:37pm UTC](https://discuss.elastic.co/t/elastic-search-sub-searches-with-knn-and-rrf/357804/4 "2024-04-24T18:37:39Z")

</div>

There's no hard and fast rule, but [in general](https://www.elastic.co/guide/en/elasticsearch/reference/current/knn-search.html#tune-approximate-knn-for-speed-accuracy) if you want more accuracy use a higher num candidates and if you want more speed use a lower 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:** [May 22, 2024, 6:38pm UTC](https://discuss.elastic.co/t/elastic-search-sub-searches-with-knn-and-rrf/357804/5 "2024-05-22T18:38:10Z")

</div>

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