# Exact KNN queries not cached

**URL:** https://forum.opensearch.org/t/exact-knn-queries-not-cached/15785
**Category:** k-NN
**Created:** [September 5, 2023, 10:13pm UTC](https://forum.opensearch.org/t/exact-knn-queries-not-cached/15785 "2023-09-05T22:13:27Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![dhinesh\_r](https://avatars.discourse-cdn.com/v4/letter/d/8797f3/32.png) [@dhinesh\_r](https://forum.opensearch.org/u/dhinesh_r)
#### Post date: [September 5, 2023, 10:13pm UTC](https://forum.opensearch.org/t/exact-knn-queries-not-cached/15785/1 "2023-09-05T22:13:27Z")

</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 KNN 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: ![Navneet](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.opensearch.org/navneet/32/3645_2.png) [@Navneet](https://forum.opensearch.org/u/Navneet)
#### Post date: [September 15, 2023, 4:13pm UTC](https://forum.opensearch.org/t/exact-knn-queries-not-cached/15785/2 "2023-09-15T16:13:03Z")

</div>

The same question was asked on public slack channel. Pasting the slack conversation link here. [Slack](https://opensearch.slack.com/archives/C05BGJ1N264/p1693889116345549)

For no caching happening for exact queries I have cut a github issue: [[BUG] Script Score Queries Not getting Cached for K-NN painless Script for Exact Search · Issue #1098 · opensearch-project/k-NN · GitHub](https://github.com/opensearch-project/k-NN/issues/1098)

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex019/uploads/mauve_hedgehog/original/2X/3/33547ea01a5b12dcca2958411d3edd97ae2ea8c1.png) [@system](https://forum.opensearch.org/u/system)
#### Post date: [November 14, 2023, 4:13pm UTC](https://forum.opensearch.org/t/exact-knn-queries-not-cached/15785/3 "2023-11-14T16:13:47Z")

</div>

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