# ElasticSearch: L2 norm script\_score returns false distances

**URL:** <https://discuss.elastic.co/t/elasticsearch-l2-norm-script-score-returns-false-distances/257384>\
**Category:** Elasticsearch\
**Created:** [December 2, 2020, 3:57pm UTC](https://discuss.elastic.co/t/elasticsearch-l2-norm-script-score-returns-false-distances/257384 "2020-12-02T15:57:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![christiana](https://avatars.discourse-cdn.com/v4/letter/c/d9b06d/32.png) [@christiana](https://discuss.elastic.co/u/christiana)\
**Post date:** [December 2, 2020, 3:57pm UTC](https://discuss.elastic.co/t/elasticsearch-l2-norm-script-score-returns-false-distances/257384/1 "2020-12-02T15:57:48Z")

</div>

i'm trying to calculate the L2-Norm between the fields of my database. To validate the correctness I setup a testing database with two documents, which are having equal vectors.  
My db look like this:

```auto
Documents
 _index:2vecstest
 _type:_doc
 _id:0
 _score:None
 _source:
         imageid:0
         score:1
         gpd
         [0, 0, 1, 1]
         mask:1111
         tempvec
         [0, 0, 1, 1]
 sort
 [0]
 _index:2vecstest
 _type:_doc
 _id:1
 _score:None
 _source:
         imageid:1
         score:1
         gpd
         [0, 0, 0, 1]
         mask:1111
         tempvec
         [0, 0, 0, 1]
 sort
 [1]

```

Then I want to calculate the distance between gpd (dense\_vector) and tempvec (double-field) of each document. For this I wrote this request:

```auto
    request = { "size": size,
                "query": {
                    "script_score": {
                        "query": {
                            "match_all": {}
                        },
                        "script": {
                            "lang":"painless",
                            "source": """
                                return l2norm(doc['tempvec'], doc['gpd']) + 1;
                            """
                            ,   
                        }
                    }
                }
              }

try :
     res = es.search(index=_INDEX, body=request)
except elasticsearch.ElasticsearchException as es1:  
...

```

I would expect both scores to be maximum, since within each doc the vectors are equal. However I'm getting following search result:

```auto
took:18
 timed_out:False
 _shards:
	 total:1
	 successful:1
	 skipped:0
	 failed:0
 hits:
	 total:
		 value:2
		 relation:eq
	 max_score:2.0
	 hits
	 _index:2vecstest
	 _type:_doc
	 _id:1
	 _score:2.0
	 _source:
		 imageid:1
		 score:1
		 gpd
		 [0, 0, 0, 1]
		 mask:1111
		 tempvec
		 [0, 0, 0, 1]
	 _index:2vecstest
	 _type:_doc
	 _id:0
	 _score:1.0
	 _source:
		 imageid:0
		 imid:0
		 score:1
		 gpd
		 [0, 0, 1, 1]
		 mask:1111
		 tempvec
		 [0, 0, 1, 1]

```

So strangely the second document is ranked lower.  
What am I doing wrong here? I don't found an answer in the elasticsearch documentation.

Greetings,  
Christian

---

<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 30, 2020, 3:57pm UTC](https://discuss.elastic.co/t/elasticsearch-l2-norm-script-score-returns-false-distances/257384/2 "2020-12-30T15:57:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Julie\_Tibshirani](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/julie_tibshirani/32/55628_2.png) [@Julie\_Tibshirani](https://discuss.elastic.co/u/Julie_Tibshirani)\
**Post date:** [February 5, 2021, 9:57pm UTC](https://discuss.elastic.co/t/elasticsearch-l2-norm-script-score-returns-false-distances/257384/3 "2021-02-05T21:57:44Z")

</div>

Hello Christian, hope this reply isn't coming too late. It's not actually supported for l2norm to load two vectors from a document. It's intended for comparing a query to a document vector. We updated the function syntax in 7.6 to make this clear. (I wonder if we accidentally allowed it before 7.6, which resulted in buggy output).

I hope that helps. The summary is that comparing two vectors from a single document isn't supported, and to watch out for a syntax change when upgrading to 7.6.

Julie
