# Exact KNN queries not cached

**URL:** <https://discuss.elastic.co/t/exact-knn-queries-not-cached/342225>\
**Category:** Elasticsearch\
**Tags:** vector-search\
**Created:** [September 4, 2023, 7:28am UTC](https://discuss.elastic.co/t/exact-knn-queries-not-cached/342225 "2023-09-04T07:28:34Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dhineshkumar\_R](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dhineshkumar_r/32/122038_2.png) [@Dhineshkumar\_R](https://discuss.elastic.co/u/Dhineshkumar_R)\
**Post date:** [September 4, 2023, 7:28am UTC](https://discuss.elastic.co/t/exact-knn-queries-not-cached/342225/1 "2023-09-04T07:28:34Z")

</div>

Hi Folks,

I have a cluster with vectors indexed in a `knn` index and I'd like to find exact K-nearest neighbors for a given vector using score\_script. There are about 500K documents in total.

I'm using the following query to find the `id` of top 10 nearest neighbors(documents) of the `test_vector` after filtering documents that match `test_key`

```auto
{
          "size": 10,
          "_source": false,
          "fields": ["id"],
          "query": {
            "script_score": {
              "query": {
                "match": {
                  "key.keyword": {{test_key}}
                }
              },
              "script": {
                "source": "knn_score",
                "lang": "knn",
                "params": {
                  "field": "vector",
                  "query_value": {{test_vector}},
                  "space_type": "innerproduct"
                }
              }
            }
          }
 }

```

Following are my questions

1. I observe that `request_cache` and `query_cache` is not used at all. I confirmed `query_cache` and `request_cache` are enabled. I pulled the index stats before and after I queried and I see no change.  
1.1 Can I expect score\_script queries to be cached anywhere?  
1.2 Can I update my search query in anyway to utilize caches and inturn improve search latency?
2. Updating match query to match "key.keyword" from "key" did not improve the latency. Initially, I used match query on just `key` field to filter documents matching `test_key`. I found unexpected document `id`s in the result. So, I changed the query to match on `key.keyword` and expected it to reduce the search latency because matching on just `key` returned a lot more documents compared to `key.keyword`. I understand that the ideal solution will be to use explicit mapping for the `key` field to be interpreted as Keyword type and use Term queries for exact match. However, I was expecting this change to improve latency. Am i missing something here?

Thanks for your time in advance. Please let me know if you need more info. from my side.

Thanks

---

<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:** [October 2, 2023, 7:29am UTC](https://discuss.elastic.co/t/exact-knn-queries-not-cached/342225/2 "2023-10-02T07:29:28Z")

</div>

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