Similarity Search in Vector Space with Error

I'm trying to test the new elasticsearch functionality 'word2vec for text similarity'. I use this little example, but I have the following error! I would like to have feedback and tell me where is the error, knowing that I use version 7.3.2 :

######################################################
Mapping
######################################################
PUT /testw2v
{
"mappings":{
"type_1":{
"_all" : {"enabled" : true},
"properties":{
"text":{ "type":"string"},
"text_vector":{"type": "dense_vector",
"dims": 4}
}
}
}
}

######################################################
put data
######################################################
PUT testw2v_1/_doc/1
{
"text" : "A New Jersey guy dedicated to his family, friends, and church, develops unrealistic expectations from watching porn and works to find happiness and intimacy with his potential true love.",
"text_vector" : [0.01,0.23,0.6,0.34]
}

######################################################
GET query
######################################################

GET /testw2v/_search/
{
"size": 1,
"query": {
"script_score": {
"query": {
"match_all": {}
},
"script": {
"source": "cosineSimilarity(params.queryVector, doc['text_vector'])+1.0",
"params": {
"queryVector": [0.01,0.23,0.6,0.34]
}
}
}
}
}

######################################################
ERROR message
######################################################

{
"error": {
"root_cause": [
{
"type": "script_exception",
"reason": "runtime error",
"script_stack": [
"cosineSimilarity(params.queryVector, doc['text_vector'])+1.0",
" ^---- HERE"
],
"script": "cosineSimilarity(params.queryVector, doc['text_vector'])+1.0",
"lang": "painless"
}
],
"type": "search_phase_execution_exception",
"reason": "all shards failed",
"phase": "query",
"grouped": true,
"failed_shards": [
{
"shard": 0,
"index": "testw2v_1",
"node": "Lihza6H3RG6oRqwZYY9AAA",
"reason": {
"type": "script_exception",
"reason": "runtime error",
"script_stack": [
"cosineSimilarity(params.queryVector, doc['text_vector'])+1.0",
" ^---- HERE"
],
"script": "cosineSimilarity(params.queryVector, doc['text_vector'])+1.0",
"lang": "painless",
"caused_by": {
"type": "class_cast_exception",
"reason": "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 @7a1f8def)"
}
}
}
]
},
"status": 400
}

######################################################
######################################################
######################################################
######################################################

1- In the search query we have to use POST or GET ?
2- in the mapping, I defined "text_vector" as 'dense_vector' with dim=4, so why this error ?

Hi there,
You seem to define a mapping in the index testw2v with type type_1; index a document to a different index with a different type testw2v_1/_doc, and searching I am not sure which index with which type. The error says that your trying to cast doc['text_vector'] to a vector type, while it is defined as double.

Can you make sure to work with the same index, and only use _doc instead of type name.
Your example works perfect if I ran this way:

{
  "took": 6,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": 2,
    "hits": [
      {
        "_index": "test_index",
        "_type": "_doc",
        "_id": "1",
        "_score": 2,
        "_source": {
          "text_vector": [
            0.01,
            0.23,
            0.6,
            0.34
          ]
        }
      }
    ]
  }
}

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