Compare a vector to a list of vectors contained in each document

Hi,

Each of my documents contain a list of dense_vectors inside a nested object : words.vector

I try to compare my input vector to all documents' words.vector properties.

I currently try with the following but elasticsearch tells me script_score function must not produce negative scores... :

GET test_words/_search
{
  "query": {
    "nested": {
      "path": "words",
      "score_mode": "min",
      "query": {
        "function_score": {
          "script_score": {
            "script": {
                "source": "cosineSimilarity(params.myvector, 'words.vector')",
                "params": {
                    "myvector": [
			-1.310115814,
			-0.399771153,
			-0.420954197,
			1.589795112
		  ]
              }
            }
          }
        }
      }
    }
  }
}

How could I achieve that ?

Thank you,

Elasticsearch doesn't allow for documents to produce scores < 0. As cosineSimilarity function returns values from [-1;1] to ensure non-negative scores, you need to add 1 to your function like this:
cosineSimilarity(params.myvector, 'words.vector') + 1.

This is also explained in our documentation.

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