# Calculate cosine similarity for nested object in elasticsearch using Python

**URL:** <https://discuss.elastic.co/t/calculate-cosine-similarity-for-nested-object-in-elasticsearch-using-python/298601>\
**Category:** Elasticsearch\
**Created:** [March 2, 2022, 9:42am UTC](https://discuss.elastic.co/t/calculate-cosine-similarity-for-nested-object-in-elasticsearch-using-python/298601 "2022-03-02T09:42:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![RiyaP](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/riyap/32/99177_2.png) [@RiyaP](https://discuss.elastic.co/u/RiyaP)\
**Post date:** [March 2, 2022, 9:42am UTC](https://discuss.elastic.co/t/calculate-cosine-similarity-for-nested-object-in-elasticsearch-using-python/298601/1 "2022-03-02T09:42:15Z")

</div>

I am working on an use case which has documents in following format:-

```auto
[{'Fruits': ['Mango', 'Apple']},
 {'Fruits': ['Banana', 'Guava', 'Mango']},
 {'Fruits': ['Grapes', 'Apple']}]

```

I am storing the data into Elasticsearch cluster in below format:-

```auto
["Fruits":[{"value": dense_vector('Mango'),"text": "Mango"},{"value": dense_vector('Apple'),"text": "Apple"}],
"Fruits":[{"value": dense_vector('Banana'),"text": "Banana"},{"value": dense_vector('Guava'),"text": "Guava"},
{"value": dense_vector('Mango'),"text": "Mango"}],...]

```

Am storing dense vector(Bert embeddings) so as to handle synonyms by calculating cosine similarity. My Elasticsearch query looks like this"-

```auto
{
    "nested": {
      "path": "Fruits",
        "score_mode": "max", 
      "query": {
        "function_score": {
          "script_score": {
            "script": {
              "source": "1+cosineSimilarity(params.query_vector0,'Fruits.value')+cosineSimilarity(params.query_vector1,'Fruits.value')",
              "params": {"query_vector0":query_vector,"query_vector1":query_vector1}
            }
          }
        }
      }
    }
}

```

,wherein query\_vector and query\_vector1 are the below values"-

```auto
query_vector=bert_embedding.encode([str(Grape)])[0]
query_vector1=bert_embedding.encode([str(Apples)])[0]

```

When I give user queries as "Grape" and "Apples", I am getting similar score for these 2 documents:-

```auto
[{'Fruits': ['Mango', 'Apple']},
 {'Fruits': ['Grapes', 'Apple']}]

```

Whereas, as expected, I should get a higher score for {'Fruits': ['Grapes', 'Apple']} when compared to the 1st document. Also , upon changing the value for "score\_mode" to avg/sum/max, the results are changing. Which score\_mode should I finalize on based on my document format?

Am not able to figure out how cosine score is similar for both the documents. Can someone please help on that?`Preformatted text`

---

<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 30, 2022, 9:43am UTC](https://discuss.elastic.co/t/calculate-cosine-similarity-for-nested-object-in-elasticsearch-using-python/298601/2 "2022-03-30T09:43:06Z")

</div>

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