# Script score vector search performance

**URL:** <https://discuss.elastic.co/t/script-score-vector-search-performance/312341>\
**Category:** Elasticsearch\
**Created:** [August 18, 2022, 5:57am UTC](https://discuss.elastic.co/t/script-score-vector-search-performance/312341 "2022-08-18T05:57:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![zyyme](https://avatars.discourse-cdn.com/v4/letter/z/46a35a/32.png) [@zyyme](https://discuss.elastic.co/u/zyyme)\
**Post date:** [August 18, 2022, 5:57am UTC](https://discuss.elastic.co/t/script-score-vector-search-performance/312341/1 "2022-08-18T05:57:31Z")

</div>

Hello!

I use Elasticsearch 7.14 and I have a mapping like this:

```auto
{
  "mappings": {
    "properties": {
      "vector": {
        "type": "dense_vector",
        "dims": 512
      },
      "category": {
        "type": "keyword"
      },
      "name": {
        "type": "keyword"
      },
      "source": {
        "type": "keyword"
      }
    }
  }
}

```

This index is primarily used to search by vector using cosine similarity, like this:

```auto
{
  script_score: {
    query: { match_all: {} },
    script: {
      source: '(1.0 + cosineSimilarity(params.query_vector, \'vector\'))',
      params: {
        query_vector: vectorArray
      }
    },
    min_score: 0
  }
}

```

I have around 600k documents in this index, and for this amount of documents I think sharding is not necessary. However, even when I have 3 shards, I have total search time of 5 seconds, according to Kibana devtools.

Is there something else I could do to improve search speed?

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [August 19, 2022, 1:01pm UTC](https://discuss.elastic.co/t/script-score-vector-search-performance/312341/2 "2022-08-19T13:01:42Z")

</div>

You are running query with a matchg all clause, so all documents will need to be scored using the script. Each query is run in a single thread against each shard so in order to increase parallelism and use more CPU cores you need to increase the number of primary shards.

If the index is small, try reindexing it into a few new indices with varying number of primary shards and see how querying how the different indices compare.

---

<div class="post-metadata">

**Author:** ![mayya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mayya/32/83147_2.png) [@mayya](https://discuss.elastic.co/u/mayya)\
**Post date:** [August 25, 2022, 9:21pm UTC](https://discuss.elastic.co/t/script-score-vector-search-performance/312341/3 "2022-08-25T21:21:20Z")

</div>

Yes, you can try to introduce a more restrictive filter instead of `match_all` query.

Also, from 8.0 you can try [approximate knn search](https://www.elastic.co/guide/en/elasticsearch/reference/8.4/knn-search.html#approximate-knn) which is much faster that exact knn search.

---

<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:** [September 22, 2022, 9:21pm UTC](https://discuss.elastic.co/t/script-score-vector-search-performance/312341/4 "2022-09-22T21:21:57Z")

</div>

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