fast-elasticsearch-vector-scoring icon indicating copy to clipboard operation
fast-elasticsearch-vector-scoring copied to clipboard

How to achieve ANDing on nested Vectors?

Open ahmad-rzk opened this issue 2 years ago • 0 comments

I will start by saying thank you for the amazing work!

Is there a way to achieve multi-vector ANDing when the vectors are saved as nested?

Having the following mapping:

"Faces": {
"type": "nested",
"properties": {
"Features": {
"type": "binary",
"doc_values": true
}}}

we use the score_mode: max attribute to get the documents containing the KNN vectors

When looking for multiple vectors to match any of the given vectors (ORing) we run the function_score for each vector separately like this:

{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "query": {
              "function_score": {
                "boost_mode": "replace",
                "script_score": {
                  "script": {
                    "source": "binary_vector_score",
                    "lang": "knn",
                    "params": {
                      "cosine": true,
                      "field": "Faces.Features",
                      "vector": [
                        -0.5,
                        10.0,
                        10.0
                      ]
                    }
                  }
                }
              }
            },
            "path": "Faces",
            "score_mode": "max"
          }
        },
        {
          "nested": {
            "query": {
              "function_score": {
                "boost_mode": "replace",
                "script_score": {
                  "script": {
                    "source": "binary_vector_score",
                    "lang": "knn",
                    "params": {
                      "cosine": true,
                      "field": "Faces.Features",
                      "vector": [
                        0.5,
                        10.0,
                        6.0
                      ]
                    }
                  }
                }
              }
            },
            "path": "Faces",
            "score_mode": "max"
          }
        }
      ]
    }
  },
  "size": 10
}

Is there a way to add a wrapper around this saying I want my documents to be scored by the score of the first function (+) the score from the second one so we can achieve scoring by more than one vector?

ahmad-rzk avatar Nov 11 '21 07:11 ahmad-rzk