Czyzby
Czyzby

Reputation: 3139

Convert doubles array to a dense vector in ElasticSearch

I'm attempting to run a relatively simple query based on the dense vector examples:

{
    "query": {
        "script_score": {
            "query": {"match_all": {}},
            "script": {
                "source": "cosineSimilarity(params.query_vector, doc['vector']) + 1.0",
                "params": {"query_vector": vector}
            }
        }
    }
}

The vector value that I'm passing to the query is a simple array of doubles with size matching those of vectors stored in the database. However, when I attempt to run the query, I'm getting this error:

     'script_stack': ["cosineSimilarity(params.query_vector, doc['vector']) + 1.0",
     '                                         ^---- HERE'],

Is there a way to force the query parameter type?

class org.elasticsearch.index.fielddata.ScriptDocValues$Doubles cannot be cast to class org.elasticsearch.xpack.vectors.query.VectorScriptDocValues$DenseVectorScriptDocValues (org.elasticsearch.index.fielddata.ScriptDocValues$Doubles is in unnamed module of loader 'app'; org.elasticsearch.xpack.vectors.query.VectorScriptDocValues$DenseVectorScriptDocValues is in unnamed module of loader java.net.FactoryURLClassLoader @72ee5d84)

Upvotes: 1

Views: 2943

Answers (1)

jmazella
jmazella

Reputation: 146

I had the same problem, and I wasn't properly defining the vector field in my index mapping. Have you explicitly defined the field in your mapping, and is the type right? Should be something like

{
    "properties": {
      "vector": {
        "type": "dense_vector",
        "dims": 32
      }
}

Where dims=The number of values in your vector

Upvotes: 4

Related Questions