# Dense vector search using script\_score

**URL:** <https://discuss.elastic.co/t/dense-vector-search-using-script-score/328375>\
**Category:** Elasticsearch\
**Tags:** vector-search\
**Created:** [March 23, 2023, 4:49pm UTC](https://discuss.elastic.co/t/dense-vector-search-using-script-score/328375 "2023-03-23T16:49:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marco\_Scoppetta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marco_scoppetta/32/118846_2.png) [@Marco\_Scoppetta](https://discuss.elastic.co/u/Marco_Scoppetta)\
**Post date:** [March 23, 2023, 4:49pm UTC](https://discuss.elastic.co/t/dense-vector-search-using-script-score/328375/1 "2023-03-23T16:49:17Z")

</div>

Hi all,

I'm trying to run an exact kNN search.

I've created the following index with the mappings:

```auto
await client.indices.create({
     index: "semantic-stuff",
      mappings: {
        properties: {
          data: { type: "text", index: false },
          embeddings: { type: "dense_vector", index: false, dims: 1536 },
          orgId: { type: "text" },
        },
      },
    });

```

Let me start by noting that when I fetch the mapping for this index, the `embeddings` property has type `float` rather than `dense_vector`, is this expected? I hope so.

But then when running the following query (from [Script score query | Elasticsearch Guide [8.6] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-script-score-query.html#vector-functions-cosine)):

```auto
query: {
        script_score: {
          query: {
            match: {
              orgId: {
                query: "a50ff874-2cf4-4058-a0e1-aa47f27eaef7",
              },
            },
          },
          script: {
            source:
              "cosineSimilarity(params.queryVector, 'embeddings') + 1.0",
            params: {
              queryVector: emeddingsIReceiveFromOpenAiHERE,
            },
          },
        },
      },

```

the embeddings I'm passing to the query are computed in the same exact way as the ones I've used to populate ES.

But I always get the following error:

```auto
'class org.elasticsearch.script.field.FloatDocValuesField cannot be cast to class org.elasticsearch.script.field.vectors.DenseVectorDocValuesField (org.elasticsearch.script.field.FloatDocValuesField and org.elasticsearch.script.field.vectors.DenseVectorDocValuesField are in module org.elasticsearch.server@8.6.2 of loader 'app')'

```

with also

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/c/2/c22f01485770aa741565d64224a450783587262d.png)

Does anyone know what I'm doing wrong here?

EDIT: reloading data I've noticed the mapping now shows `dense_vector` as embedding field type and the search works as expected.

I have changed nothing in my code though, which is a bit concerning, does anyone know if it can happen automatic casting from dense\_vector to float on bulk import or something?

---

<div class="post-metadata">

**Author:** ![Jack\_Conradson](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jack_conradson/32/47236_2.png) [@Jack\_Conradson](https://discuss.elastic.co/u/Jack_Conradson)\
**Post date:** [March 23, 2023, 5:45pm UTC](https://discuss.elastic.co/t/dense-vector-search-using-script-score/328375/2 "2023-03-23T17:45:55Z")

</div>

Hi Marco,

Let me start by saying based on the information presented here, I don't believe this to be directly related to scripting. The `ClassCastException` makes sense if the `embeddings` mapping appeared to be a `float` type to the script.

Now, it's certainly odd that `embeddings` appeared as a `float` type, though. Are you able to reproduce this issue? And if so, would you please provide the steps you are taking to do so? Also when you say "reloading data" what do you mean by that?

Thanks!

---

<div class="post-metadata">

**Author:** ![Marco\_Scoppetta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marco_scoppetta/32/118846_2.png) [@Marco\_Scoppetta](https://discuss.elastic.co/u/Marco_Scoppetta)\
**Post date:** [March 27, 2023, 5:13pm UTC](https://discuss.elastic.co/t/dense-vector-search-using-script-score/328375/3 "2023-03-27T17:13:19Z")

</div>

Nope, I havent been able to reproduce it. I'll report back here If I'll ever manage to.

By "reloading data" I meant: delete index, re-define the mapping for it, re-load data into ES (specifically into that index)

---

<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:** [April 24, 2023, 5:14pm UTC](https://discuss.elastic.co/t/dense-vector-search-using-script-score/328375/4 "2023-04-24T17:14:03Z")

</div>

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