I have facing issue in elastic search cosinesimilarity module. I have made my mappings correct which was suggested by most people, but still facing the issue

MAPPINGs-->

{
"settings" : {
"posts" : {
"number_of_shards" : 1,
"number_of_replicas" : 1
},

	"mappings": {
		"properties": {
				"title_vector": {
					"type": "dense_vector",
					"dims": 3
	     				 },
  				"comment":	{
					"type":"text"
					}
			}
		}
	}

}

INPUT QUERY -->
{
"query": {
"script_score": {
"query": {
"match_all": {}
},
"script": {
"source": "cosineSimilarity(params.queryVector, doc['title_vector'])",
"params": {
"queryVector": [0.6226563516985381,0.24777450998447703,0.16749395455557237]
}
}
}
}
}

OUTPUT ERROR-->
{
"error": {
"root_cause": [
{
"type": "script_exception",
"reason": "runtime error",
"script_stack": [
"cosineSimilarity(params.queryVector, doc['title_vector'])",
" ^---- HERE"
],
"script": "cosineSimilarity(params.queryVector, doc['title_vector'])",
"lang": "painless"
}
],
"type": "search_phase_execution_exception",
"reason": "all shards failed",
"phase": "query",
"grouped": true,
"failed_shards": [
{
"shard": 0,
"index": "posts",
"node": "IkCDVw5AR02qqOi0-EixKQ",
"reason": {
"type": "script_exception",
"reason": "runtime error",
"script_stack": [
"cosineSimilarity(params.queryVector, doc['title_vector'])",
" ^---- HERE"
],
"script": "cosineSimilarity(params.queryVector, doc['title_vector'])",
"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 @204e90f7)"
}
}
}
]
},
"status": 400
}

@Divyanshu_Pandey
Did you first request of creating mapping succeed? If not, please use elasticsearch with at least basic license where dense_vector field mapping is available.

If yes, and you already using elasticsearch with basic license, I would suggest to make sure to query the proper index where title_vector is defined as dense_vector.

mapping got created...the data got stored in the elastic search db...on basic query it is giving correct output....The query index is correct. There is some issue with the similarity function. I am not getting the right syntax...The version is also the latest one 7.4.2

Your script works for me well on 7.4.
Here is the result:

{                                                                                        
  "took": 8,                                                                             
  "timed_out": false,                                                                    
  "_shards": {                                                                           
    "total": 1,                                                                          
    "successful": 1,                                                                     
    "skipped": 0,                                                                        
    "failed": 0                                                                          
  },                                                                                     
  "hits": {                                                                              
    "total": {                                                                           
      "value": 1,                                                                        
      "relation": "eq"                                                                   
    },                                                                                   
    "max_score": 1,                                                                      
    "hits": [                                                                            
      {                                                                                  
        "_index": "test_index",                                                          
        "_type": "_doc",                                                                 
        "_id": "1",                                                                      
        "_score": 1,                                                                     
        "_source": {                                                                     
          "title_vector": [                                                            
            0.6226563516985381,                                                          
            0.24777450998447703,                                                         
            0.16749395455557237                                                          
          ]                                                                              
        }                                                                                
      }                                                                                  
    ]                                                                                    
  }                                              
} 

This error "org.elasticsearch.index.fielddata.ScriptDocValues$Doubles cannot be cast to class org.elasticsearch.xpack.vectors.query.VectorScriptDocValues$DenseVectorScriptDocValues" indicates that your title_vector is not of a type of dense_vector, but of type double. Make sure that your mapping request worked, but getting it back . and confirming that your title_vector is indeed of type dense_vector.

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