# KnnQuery vs scripts\_score

**URL:** <https://discuss.elastic.co/t/knnquery-vs-scripts-score/358189>\
**Category:** Elasticsearch\
**Tags:** vector-search\
**Created:** [April 25, 2024, 9:13am UTC](https://discuss.elastic.co/t/knnquery-vs-scripts-score/358189 "2024-04-25T09:13:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aj\_stor](https://avatars.discourse-cdn.com/v4/letter/a/3ab097/32.png) [@aj\_stor](https://discuss.elastic.co/u/aj_stor)\
**Post date:** [April 25, 2024, 9:13am UTC](https://discuss.elastic.co/t/knnquery-vs-scripts-score/358189/1 "2024-04-25T09:13:00Z")

</div>

I want to implement a vector search in my application but I am trying to understand the differences between a KnnSearch and using the script\_score query. As far as I can see I only see downsides to the Knn Search:

- KnnSearch is limited to max 10.000 results (k param) and script\_score is not.

- KnnSearch Returns different results almost each call. Not that a difference much but with ~6000 matches it varies between 5900 - 6100.

- KnnSearch should be faster since it is approximate but the tests I never noticed a significant change. Both queries execute in max 500ms. (Currently with 51.000.000 documents which contain a vector.)

The documentation says that using script\_score is a brute-force manner to execute the similar search and that it should be significant slower but I don't really see a big difference.

Good to know; I store my vector like this:

```auto
{
   "similarityvector": {
      "type": "dense_vector",
      "dims": 100,
      "index": true,
      "similarity": "dot_product"
    }
}

```

Could you elaborate the differences between the queries and when I should use them?

---

<div class="post-metadata">

**Author:** ![joemcelroy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joemcelroy/32/103757_2.png) [@joemcelroy](https://discuss.elastic.co/u/joemcelroy)\
**Post date:** [April 25, 2024, 9:26am UTC](https://discuss.elastic.co/t/knnquery-vs-scripts-score/358189/2 "2024-04-25T09:26:07Z")

</div>

Yes main difference is one is brute and another is approx using hnsw. Brute force requires evaluating the similarity for each vector in the data-set. It depends on your use-case but I would also say that whilst it appears to be a similar time, it may not be the case in production with parallel queries.

Due to it being approx, its not always guarantee to find the exact nearest neighbour or in the same order. the accuracy will not be equal to brute force.

I would also mention that as the number of vectors scale, you may consider storing vectors using quantisation, reducing the dimensionality of vectors for the cost of less accuracy and still use brute. That can improve memory footprint and allow for brute to be used

> **[Dense vector field type | Elasticsearch Guide \[8.13\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/dense-vector.html#dense-vector-quantization)**

---

<div class="post-metadata">

**Author:** ![aj\_stor](https://avatars.discourse-cdn.com/v4/letter/a/3ab097/32.png) [@aj\_stor](https://discuss.elastic.co/u/aj_stor)\
**Post date:** [April 25, 2024, 9:42am UTC](https://discuss.elastic.co/t/knnquery-vs-scripts-score/358189/3 "2024-04-25T09:42:35Z")

</div>

> [@joemcelroy](#):
>
> It depends on your use-case but I would also say that whilst it appears to be a similar time, it may not be the case in production with parallel queries.

At what number of vectors do you expect a real difference in execution time? Or does rate of queries matter? For now, executing the query in Kibana there is no difference.

And my apologies but I still don't really understand why I should not always use script\_score. As far as I can see it is much better.

---

<div class="post-metadata">

**Author:** ![joemcelroy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joemcelroy/32/103757_2.png) [@joemcelroy](https://discuss.elastic.co/u/joemcelroy)\
**Post date:** [April 25, 2024, 9:49am UTC](https://discuss.elastic.co/t/knnquery-vs-scripts-score/358189/4 "2024-04-25T09:49:56Z")

</div>

> At what number of vectors do you expect a real difference in execution time? Or does rate of queries matter? For now, executing the query in Kibana there is no difference.

It depends on the dims of your vector, number of queries you're serving per second and the memory allocation you have on your node. Executing a single query and comparing between both isn't a fair assessment.

For example in this article you can see that the number of requests per second between approx and exact greatly differ. We have made improvements to the performance of KNN since aswell, so these numbers but be even greater.

> **[Introducing approximate nearest neighbor search in Elasticsearch 8.0](https://www.elastic.co/blog/introducing-approximate-nearest-neighbor-search-in-elasticsearch-8-0)**
>
> Elasticsearch 8.0 improves the scalability of vector search with the introduction of fast approximate nearest neighbor (ANN) search. Read on to learn about this exciting new capability....
