# 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

**URL:** <https://discuss.elastic.co/t/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/210155>\
**Category:** Elasticsearch\
**Created:** [December 2, 2019, 10:34am UTC](https://discuss.elastic.co/t/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/210155 "2019-12-02T10:34:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Divyanshu\_Pandey](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/divyanshu_pandey/32/45452_2.png) [@Divyanshu\_Pandey](https://discuss.elastic.co/u/Divyanshu_Pandey)\
**Post date:** [December 2, 2019, 10:34am UTC](https://discuss.elastic.co/t/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/210155/1 "2019-12-02T10:34:46Z")

</div>

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  
}

---

<div class="post-metadata">

**Author:** ![mayya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mayya/32/83147_2.png) [@mayya](https://discuss.elastic.co/u/mayya)\
**Post date:** [December 2, 2019, 9:05pm UTC](https://discuss.elastic.co/t/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/210155/2 "2019-12-02T21:05:48Z")

</div>

@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`.

---

<div class="post-metadata">

**Author:** ![Divyanshu\_Pandey](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/divyanshu_pandey/32/45452_2.png) [@Divyanshu\_Pandey](https://discuss.elastic.co/u/Divyanshu_Pandey)\
**Post date:** [December 3, 2019, 7:03am UTC](https://discuss.elastic.co/t/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/210155/3 "2019-12-03T07:03:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mayya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mayya/32/83147_2.png) [@mayya](https://discuss.elastic.co/u/mayya)\
**Post date:** [December 3, 2019, 3:17pm UTC](https://discuss.elastic.co/t/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/210155/4 "2019-12-03T15:17:19Z")

</div>

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

```auto
{                                                                                        
  "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`.

---

<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:** [December 31, 2019, 3:17pm UTC](https://discuss.elastic.co/t/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/210155/5 "2019-12-31T15:17:29Z")

</div>

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