Is there a way to combine default BM25 score of Elasticsearch and Dense Vectors similarity

Hello everyone!

I would like to combine the BM25 score computed by Elasticsearch and
The cosine similarity of dense vectors.

I read the following post

It would be nice if I could somehow use the BM25 score in the "script".
Is there a way to do something like that?

Thank you in advance

Hello,
script gives you flexibility to combine textual scores with any other computations the way you like.
For example, the following query uses a linear combination of BM25 score from query: message:elasticsearch with a vector cosine similarity.

{
  "query": {
    "script_score": {
      "query": {
        "match": {
          "message": "elasticsearch"
        }
      },
      "script": {
        "source": "_score + cosineSimilarity(params.query_vector, 'my_dense_vector')",
        "params": {
          "query_vector": [4, 3.4,-0.2]
        }
      }
    }
  }
}
1 Like

Thank you very much

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