Dense vector search using script_score

Hi all,

I'm trying to run an exact kNN search.

I've created the following index with the mappings:

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):

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:

'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

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?

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!

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)

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