# Similarity Search in Vector Space with Error

**URL:** <https://discuss.elastic.co/t/similarity-search-in-vector-space-with-error/201546>\
**Category:** Elasticsearch\
**Created:** [September 29, 2019, 8:00pm UTC](https://discuss.elastic.co/t/similarity-search-in-vector-space-with-error/201546 "2019-09-29T20:00:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![adouib](https://avatars.discourse-cdn.com/v4/letter/a/77aa72/32.png) [@adouib](https://discuss.elastic.co/u/adouib)\
**Post date:** [September 29, 2019, 8:00pm UTC](https://discuss.elastic.co/t/similarity-search-in-vector-space-with-error/201546/1 "2019-09-29T20:00:54Z")

</div>

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 ?

---

<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:** [October 2, 2019, 12:56am UTC](https://discuss.elastic.co/t/similarity-search-in-vector-space-with-error/201546/2 "2019-10-02T00:56:42Z")

</div>

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:

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

```

---

<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:** [October 30, 2019, 12:56am UTC](https://discuss.elastic.co/t/similarity-search-in-vector-space-with-error/201546/3 "2019-10-30T00:56:51Z")

</div>

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