# Error cosine similarity

**URL:** <https://discuss.elastic.co/t/error-cosine-similarity/263699>\
**Category:** Elasticsearch\
**Created:** [February 9, 2021, 9:11am UTC](https://discuss.elastic.co/t/error-cosine-similarity/263699 "2021-02-09T09:11:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![fjac](https://avatars.discourse-cdn.com/v4/letter/f/50afbb/32.png) [@fjac](https://discuss.elastic.co/u/fjac)\
**Post date:** [February 9, 2021, 9:11am UTC](https://discuss.elastic.co/t/error-cosine-similarity/263699/1 "2021-02-09T09:11:30Z")

</div>

Hello, I have created a field der type dense\_vector, I have reindexed the database and when performing a search it gives me an error, as you can only have a single dense\_vector value per document, I have added it as "nested", a document can have several patterns.  
I don't know if I have to initialize the field in the structure to have null value in all documents or what.

The Elasticsearch version is 7.5.2.

This is the definition of the field I have created:

- "patrones": {
  - "type": "nested",
  - "properties": {
    - "posicion": {
      - "type": "text",
      - "fields": {
        - "keyword": {
          - "ignore\_above": 256,
          - "type": "keyword"}}},

    - "patron": {
      - "dims": 128,
      - "type": "dense\_vector"}}},

{  
"error": {  
"root\_cause": [  
{  
"type": "class\_cast\_exception",  
"reason": "class\_cast\_exception: Cannot cast from [java.lang.String] to [org.elasticsearch.xpack.vectors.query.VectorScriptDocValues.DenseVectorScriptDocValues]."  
}  
],  
"type": "search\_phase\_execution\_exception",  
"reason": "all shards failed",  
"phase": "query",  
"grouped": true,  
"failed\_shards": [  
{  
"shard": 0,  
"index": "sgaafdu-2021-1",  
"node": "st2-jHV0TLyRQCSjfKJNHg",  
"reason": {  
"type": "query\_shard\_exception",  
"reason": "script\_score: the script could not be loaded",  
"index\_uuid": "K2F9Wd85TI6pp1I1X63IDg",  
"index": "sgaafdu-2021-1",  
"caused\_by": {  
"type": "script\_exception",  
"reason": "compile error",  
"script\_stack": [  
"... ity(params.query\_vector, 'patrones.patron') + 1.0",  
" ^---- HERE"  
],  
"script": "100 \* cosineSimilarity(params.query\_vector, 'patrones.patron') + 1.0",  
"lang": "painless",  
"caused\_by": {  
"type": "class\_cast\_exception",  
"reason": "class\_cast\_exception: Cannot cast from [java.lang.String] to [org.elasticsearch.xpack.vectors.query.VectorScriptDocValues.DenseVectorScriptDocValues]."  
}  
}  
}  
}  
],  
"caused\_by": {  
"type": "class\_cast\_exception",  
"reason": "class\_cast\_exception: Cannot cast from [java.lang.String] to [org.elasticsearch.xpack.vectors.query.VectorScriptDocValues.DenseVectorScriptDocValues]."  
}  
},  
"status": 400  
}

---

<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:** [March 9, 2021, 9:12am UTC](https://discuss.elastic.co/t/error-cosine-similarity/263699/2 "2021-03-09T09:12:11Z")

</div>

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