# Use distance on dense vectors in relevance score (at query time)

**URL:** https://discuss.elastic.co/t/use-distance-on-dense-vectors-in-relevance-score-at-query-time/217012
**Category:** Elasticsearch
**Created:** [January 29, 2020, 1:33pm UTC](https://discuss.elastic.co/t/use-distance-on-dense-vectors-in-relevance-score-at-query-time/217012 "2020-01-29T13:33:53Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![thomas-chauvet](https://avatars.discourse-cdn.com/v4/letter/t/ec9cab/32.png) [@thomas-chauvet](https://discuss.elastic.co/u/thomas-chauvet)
#### Post date: [January 29, 2020, 1:33pm UTC](https://discuss.elastic.co/t/use-distance-on-dense-vectors-in-relevance-score-at-query-time/217012/1 "2020-01-29T13:33:53Z")

</div>

I use elasticsearch to combine different things:

- search in text
- score based on dense vector (cosine similarity)

I use a query with `function_score`. The first part is the search in the text (giving a score) and THEN a script is applied to compute cosine similarity.

My problem is that the cosine similarity is not computed during the query phase and my search in the text act as a pre-filter. I will always obtain results linked with the text search even if the cosine similarity is better.

This is the standard behavior of `function_score` according the [doc](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html):

> The function\_score allows you to modify the score of documents that are retrieved by a query. This can be useful if, for example, a score function is computationally expensive and it is sufficient to compute the score on a filtered set of documents.

I would like to compute the cosine similarity at query time and this score will be combined with the text search (with as much importance).

Thanks !

You will find a gist [here](https://gist.github.com/thomas-chauvet/0a8317296800ecd3a92cb926e260fe7d) describing the problem with a "real" example.

_Note: this post is also on [stackoverflow](https://stackoverflow.com/questions/59967308/elasticsearch-use-distance-on-dense-vectors-in-relevance-score-at-query-time)_

---

<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: [January 31, 2020, 8:37pm UTC](https://discuss.elastic.co/t/use-distance-on-dense-vectors-in-relevance-score-at-query-time/217012/2 "2020-01-31T20:37:26Z")

</div>

Hi there!  
Did I understand your intention correctly: you want to go through **all** documents, and apply cosine similarity function to them. Then you also have a query and for the documents that match a query, you want to calculate score for this query. Then you want to combine these two scores: from a cosine similarity and a query?  
Currently, you can do that with a bool query using `should` clauses like this:

```auto
GET my_index/_search
{
  "query": {
      "bool": {
        "should" : [
          {
            "match": {
                "my_text": {
                    "query": "abc"
                }
            }
          },
          {
            "script_score" : {
              "query" : {"match_all" : {}},
              "script" : {
                "source": "50 * cosineSimilarity(params.query_vector, doc['my_vector']) + 1.0",
                "params": {
                    "query_vector": [0, 0, 1]
                }
              }
            }
          }
        ]
      }  
  }
}

```

This will give you a sum of scores: score1 + score2. You can also apply `boost` for any query.  
We also have a plan to develop a [compound query](https://github.com/elastic/elasticsearch/issues/42811#issuecomment-524854701) that will give you an option to combine scores of queries not only through `sum` option. But this is not available yet.

---

<div class="post-metadata">

### Author: ![thomas-chauvet](https://avatars.discourse-cdn.com/v4/letter/t/ec9cab/32.png) [@thomas-chauvet](https://discuss.elastic.co/u/thomas-chauvet)
#### Post date: [February 4, 2020, 3:21pm UTC](https://discuss.elastic.co/t/use-distance-on-dense-vectors-in-relevance-score-at-query-time/217012/3 "2020-02-04T15:21:21Z")

</div>

Thanks you, it was exactly what I was looking for. I didn't now that I could combine queries like this.

If you want to copy your anser on [stackoverlow](https://stackoverflow.com/questions/59967308/elasticsearch-use-distance-on-dense-vectors-in-relevance-score-at-query-time) 🙂

---

<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 3, 2020, 3:21pm UTC](https://discuss.elastic.co/t/use-distance-on-dense-vectors-in-relevance-score-at-query-time/217012/4 "2020-03-03T15:21:29Z")

</div>

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