# Formula similarity doesn't work correctly

**URL:** <https://forum.opensearch.org/t/formula-similarity-doesnt-work-correctly/13962>\
**Category:** k-NN\
**Created:** [April 17, 2023, 2:55pm UTC](https://forum.opensearch.org/t/formula-similarity-doesnt-work-correctly/13962 "2023-04-17T14:55:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![KolesnikovYA](https://avatars.discourse-cdn.com/v4/letter/k/c67d28/32.png) [@KolesnikovYA](https://forum.opensearch.org/u/KolesnikovYA)\
**Post date:** [April 17, 2023, 2:55pm UTC](https://forum.opensearch.org/t/formula-similarity-doesnt-work-correctly/13962/1 "2023-04-17T14:55:03Z")

</div>

**Versions** (relevant - OpenSearch/Dashboard/Server OS/Browser):  
OpenDistro, 7.10.0

**Describe the issue** :  
Hello!  
Please understand how does formula for similarity in l2-space and cosinesimil works?  
For example, I’ve tried to compare vectors by ElasticSearch and manually. So, I have these vectors in l2-space:

`vector_1 = [129.0, 138.0, 139.0, 114.0, 145.0, 127.0, 122.0, 132.0, 124.0, 100.0, 113.0, 129.0, 118.0, 112.0, 142.0, 125.0, 134.0, 129.0, 121.0, 131.0, 132.0, 129.0, 113.0, 112.0, 122.0, 137.0, 137.0, 116.0, 120.0, 114.0, 108.0, 118.0, 135.0, 142.0, 127.0, 117.0, 133.0, 123.0, 130.0, 141.0, 131.0, 141.0, 119.0, 126.0, 125.0, 126.0, 135.0, 143.0, 125.0, 147.0, 136.0, 118.0, 115.0, 137.0, 134.0, 133.0, 133.0, 138.0, 143.0, 124.0, 120.0, 136.0, 133.0, 124.0, 128.0, 120.0, 130.0, 115.0, 136.0, 117.0, 131.0, 123.0, 122.0, 120.0, 121.0, 121.0, 114.0, 124.0, 130.0, 121.0, 131.0, 129.0, 119.0, 140.0, 124.0, 124.0, 112.0, 126.0, 134.0, 126.0, 125.0, 137.0, 148.0, 118.0, 128.0, 115.0, 128.0, 129.0, 122.0, 132.0, 125.0, 130.0, 124.0, 129.0, 128.0, 125.0, 130.0, 125.0, 132.0, 138.0, 127.0, 127.0, 121.0, 122.0, 127.0, 148.0, 110.0, 129.0, 123.0, 134.0, 127.0, 129.0, 131.0, 131.0, 130.0, 137.0, 131.0, 129.0, 121.0, 124.0, 133.0, 126.0, 120.0, 122.0, 122.0, 110.0, 130.0, 124.0, 129.0, 116.0, 129.0, 133.0, 133.0, 130.0, 129.0, 123.0, 123.0, 126.0, 120.0, 121.0, 126.0, 127.0, 126.0, 120.0, 118.0, 124.0, 122.0, 127.0, 118.0, 141.0, 118.0, 126.0, 112.0, 132.0, 120.0, 130.0, 116.0, 128.0, 119.0, 120.0, 120.0, 135.0, 138.0, 134.0, 128.0, 120.0, 131.0, 114.0, 117.0, 130.0, 133.0, 123.0, 125.0, 122.0, 117.0, 132.0, 134.0, 130.0, 129.0, 130.0, 130.0, 118.0, 131.0, 128.0, 132.0, 132.0, 119.0, 116.0, 132.0, 125.0, 121.0, 134.0, 128.0, 127.0, 122.0, 133.0, 130.0, 126.0, 128.0, 126.0, 134.0, 130.0, 132.0, 126.0, 124.0, 126.0, 120.0, 140.0, 121.0, 135.0, 132.0, 136.0, 133.0, 127.0, 125.0, 127.0, 132.0, 131.0, 126.0, 121.0, 127.0, 117.0, 130.0, 124.0, 130.0, 122.0, 127.0, 129.0, 133.0, 124.0, 130.0, 129.0, 126.0, 127.0, 125.0, 130.0, 118.0, 122.0, 131.0, 122.0, 127.0, 122.0, 126.0, 117.0, 125.0, 121.0, 124.0, 125.0, 127.0, 134.0, 133.0, 128.0, 131.0, 129.0, 133.0, 126.0, 129.0, 124.0, 126.0, 125.0, 127.0, 127.0, 133.0, 120.0, 126.0, 131.0, 129.0, 134.0, 125.0, 127.0, 124.0, 133.0, 126.0, 124.0, 125.0, 129.0, 122.0, 128.0, 129.0, 121.0, 125.0, 125.0, 126.0, 134.0, 133.0, 132.0, 127.0, 123.0, 117.0, 131.0, 130.0, 125.0, 122.0, 130.0, 126.0, 124.0, 125.0, 128.0, 128.0, 130.0, 128.0, 130.0, 127.0, 128.0, 125.0, 129.0, 130.0, 128.0, 123.0, 124.0, 128.0, 130.0, 132.0, 129.0, 125.0, 126.0, 128.0, 130.0, 127.0, 129.0, 129.0, 129.0, 126.0, 129.0, 125.0, 129.0, 127.0, 128.0, 124.0, 127.0, 124.0, 127.0, 128.0, 126.0, 128.0, 123.0, 123.0, 131.0, 132.0, 129.0, 129.0, 127.0, 129.0, 125.0, 126.0, 128.0, 131.0, 130.0, 123.0, 124.0, 127.0, 127.0, 127.0, 129.0, 127.0, 126.0, 130.0, 125.0, 131.0, 125.0, 128.0, 126.0, 128.0, 124.0, 128.0, 131.0, 125.0, 130.0, 126.0, 133.0, 130.0, 130.0, 129.0, 126.0, 131.0, 132.0, 128.0, 132.0, 127.0, 127.0, 131.0, 130.0, 124.0, 125.0, 124.0, 128.0, 127.0, 130.0, 128.0, 127.0, 128.0, 126.0, 132.0, 128.0, 132.0, 127.0, 130.0, 129.0, 125.0, 127.0, 127.0, 126.0, 129.0, 127.0, 123.0, 124.0, 123.0, 128.0, 129.0, 133.0, 128.0, 126.0, 133.0, 131.0, 129.0, 126.0, 126.0, 130.0, 127.0, 123.0, 127.0, 126.0, 127.0, 129.0, 126.0, 127.0, 126.0, 127.0, 126.0, 126.0, 130.0, 129.0, 130.0, 128.0, 129.0, 128.0, 127.0, 132.0, 126.0, 127.0, 125.0, 130.0, 128.0, 127.0, 130.0, 127.0, 126.0, 128.0, 128.0, 128.0, 125.0, 125.0, 132.0, 127.0, 127.0, 127.0, 131.0, 129.0, 124.0, 128.0, 128.0, 126.0, 129.0, 131.0, 130.0, 128.0, 126.0, 123.0, 127.0, 129.0, 127.0, 128.0, 127.0, 128.0, 127.0, 123.0, 128.0, 128.0, 130.0, 128.0, 125.0, 128.0, 129.0, 128.0, 127.0, 127.0, 131.0, 128.0, 127.0, 126.0, 125.0, 126.0, 127.0, 128.0, 125.0, 129.0, 129.0, 124.0, 131.0, 129.0, 127.0, 129.0]`

`vector_2 = [130.0, 135.0, 127.0, 114.0, 154.0, 125.0, 121.0, 131.0, 132.0, 98.0, 127.0, 134.0, 121.0, 115.0, 145.0, 137.0, 140.0, 121.0, 126.0, 129.0, 130.0, 137.0, 106.0, 122.0, 131.0, 135.0, 135.0, 126.0, 124.0, 114.0, 101.0, 121.0, 139.0, 142.0, 114.0, 133.0, 132.0, 122.0, 125.0, 143.0, 123.0, 133.0, 125.0, 137.0, 125.0, 129.0, 142.0, 147.0, 139.0, 132.0, 140.0, 119.0, 131.0, 121.0, 133.0, 130.0, 131.0, 134.0, 140.0, 123.0, 116.0, 138.0, 139.0, 122.0, 126.0, 121.0, 130.0, 113.0, 141.0, 123.0, 131.0, 127.0, 122.0, 110.0, 131.0, 123.0, 123.0, 128.0, 122.0, 120.0, 119.0, 133.0, 112.0, 137.0, 130.0, 131.0, 119.0, 119.0, 124.0, 127.0, 120.0, 133.0, 139.0, 125.0, 135.0, 114.0, 128.0, 130.0, 135.0, 124.0, 130.0, 133.0, 129.0, 131.0, 120.0, 124.0, 126.0, 133.0, 121.0, 134.0, 123.0, 125.0, 130.0, 131.0, 120.0, 134.0, 119.0, 129.0, 122.0, 132.0, 122.0, 125.0, 124.0, 127.0, 129.0, 139.0, 121.0, 134.0, 124.0, 125.0, 132.0, 128.0, 126.0, 121.0, 133.0, 114.0, 122.0, 125.0, 129.0, 124.0, 123.0, 131.0, 130.0, 127.0, 126.0, 132.0, 125.0, 127.0, 125.0, 120.0, 129.0, 128.0, 125.0, 126.0, 115.0, 121.0, 121.0, 133.0, 123.0, 140.0, 129.0, 131.0, 122.0, 128.0, 125.0, 130.0, 118.0, 132.0, 123.0, 121.0, 124.0, 133.0, 137.0, 130.0, 134.0, 119.0, 126.0, 118.0, 123.0, 131.0, 137.0, 130.0, 127.0, 127.0, 116.0, 127.0, 135.0, 138.0, 128.0, 130.0, 131.0, 112.0, 124.0, 131.0, 125.0, 127.0, 116.0, 111.0, 134.0, 119.0, 113.0, 136.0, 130.0, 123.0, 128.0, 124.0, 128.0, 124.0, 131.0, 119.0, 135.0, 140.0, 136.0, 128.0, 121.0, 124.0, 118.0, 132.0, 128.0, 132.0, 131.0, 134.0, 136.0, 121.0, 122.0, 128.0, 131.0, 130.0, 131.0, 122.0, 129.0, 118.0, 123.0, 122.0, 131.0, 123.0, 128.0, 132.0, 131.0, 125.0, 129.0, 124.0, 128.0, 123.0, 124.0, 138.0, 125.0, 118.0, 131.0, 123.0, 130.0, 118.0, 127.0, 124.0, 128.0, 127.0, 131.0, 134.0, 131.0, 128.0, 127.0, 126.0, 129.0, 130.0, 129.0, 126.0, 126.0, 124.0, 125.0, 123.0, 122.0, 130.0, 132.0, 117.0, 124.0, 130.0, 122.0, 130.0, 124.0, 127.0, 128.0, 121.0, 130.0, 126.0, 133.0, 126.0, 119.0, 128.0, 133.0, 123.0, 128.0, 123.0, 125.0, 132.0, 131.0, 134.0, 119.0, 120.0, 123.0, 134.0, 126.0, 130.0, 127.0, 129.0, 124.0, 127.0, 124.0, 126.0, 126.0, 130.0, 129.0, 134.0, 129.0, 126.0, 125.0, 131.0, 128.0, 125.0, 117.0, 123.0, 129.0, 131.0, 129.0, 127.0, 127.0, 128.0, 127.0, 128.0, 132.0, 127.0, 126.0, 127.0, 125.0, 131.0, 126.0, 128.0, 129.0, 126.0, 124.0, 126.0, 124.0, 127.0, 129.0, 129.0, 122.0, 125.0, 125.0, 129.0, 128.0, 129.0, 127.0, 129.0, 124.0, 123.0, 128.0, 126.0, 127.0, 129.0, 124.0, 126.0, 128.0, 127.0, 127.0, 128.0, 126.0, 128.0, 130.0, 128.0, 126.0, 127.0, 133.0, 128.0, 127.0, 124.0, 125.0, 131.0, 127.0, 129.0, 128.0, 130.0, 130.0, 130.0, 125.0, 128.0, 127.0, 134.0, 128.0, 129.0, 127.0, 126.0, 129.0, 127.0, 129.0, 125.0, 125.0, 126.0, 126.0, 130.0, 127.0, 123.0, 125.0, 126.0, 130.0, 127.0, 127.0, 126.0, 125.0, 129.0, 124.0, 129.0, 124.0, 126.0, 127.0, 126.0, 123.0, 126.0, 126.0, 129.0, 129.0, 130.0, 129.0, 127.0, 127.0, 129.0, 129.0, 126.0, 128.0, 126.0, 126.0, 125.0, 123.0, 129.0, 127.0, 126.0, 122.0, 127.0, 128.0, 126.0, 126.0, 128.0, 130.0, 126.0, 129.0, 129.0, 129.0, 129.0, 130.0, 130.0, 127.0, 128.0, 126.0, 130.0, 127.0, 129.0, 128.0, 126.0, 125.0, 128.0, 129.0, 128.0, 125.0, 126.0, 130.0, 125.0, 129.0, 128.0, 126.0, 130.0, 122.0, 128.0, 128.0, 126.0, 128.0, 129.0, 132.0, 129.0, 129.0, 125.0, 129.0, 130.0, 128.0, 126.0, 127.0, 127.0, 129.0, 125.0, 127.0, 127.0, 131.0, 127.0, 126.0, 127.0, 131.0, 130.0, 127.0, 126.0, 131.0, 130.0, 128.0, 126.0, 126.0, 127.0, 128.0, 126.0, 126.0, 129.0, 127.0, 126.0, 129.0, 128.0, 127.0, 129.0]`

ElasticSearch returns next similarity-scores:  
`vector_1 - vector_1 => 1.0`  
`vector_1 - vector_2 => 0,00009978`

These are my manual similarity scores:  
`vector_1 - vector_1 => 1.0`  
`vector_1 - vector_2 => 0.00989071`

I use formula for elasticsearch from [https://opendistro.github.io/for-elasticsearch-docs/docs/knn/approximate-knn/](https://opendistro.github.io/for-elasticsearch-docs/docs/knn/approximate-knn/)

---

<div class="post-metadata">

**Author:** ![navtat](https://avatars.discourse-cdn.com/v4/letter/n/f4b2a3/32.png) [@navtat](https://forum.opensearch.org/u/navtat)\
**Post date:** [April 20, 2023, 4:33am UTC](https://forum.opensearch.org/t/formula-similarity-doesnt-work-correctly/13962/2 "2023-04-20T04:33:20Z")

</div>

@KolesnikovYA Sorry for the delay in response.  
l2 or euclidean distance is `√(∑i=1toN (xi-yi)²)`  
The distance function used by elasticsearch or opensearch is `1/(1+l2^2)`.  
But, based on the score which you provided by computing manually seems to be calculated using this formula `1/(1+l2)`

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/mauve_hedgehog/original/2X/3/33547ea01a5b12dcca2958411d3edd97ae2ea8c1.png) [@system](https://forum.opensearch.org/u/system)\
**Post date:** [June 19, 2023, 4:33am UTC](https://forum.opensearch.org/t/formula-similarity-doesnt-work-correctly/13962/3 "2023-06-19T04:33:28Z")

</div>

Closed on June 19, 2023, 4:33am UTC

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