How to Modify Default Scoring Calculation When Using Multiple knn Mixed Searches

POST image-index/_search
{
  "query": {
    "match": {
      "title": {
        "query": "mountain lake",
        "boost": 0.9
      }
    }
  },
  "knn": [ {
    "field": "image-vector",
    "query_vector": [54, 10, -2],
    "k": 5,
    "num_candidates": 50,
    "boost": 0.1
  },
  {
    "field": "title-vector",
    "query_vector": [1, 20, -52, 23, 10],
    "k": 10,
    "num_candidates": 10,
    "boost": 0.5
  }],
  "size": 10
}

The scoring for a doc with the above configured boosts would be:
score = 0.9 * match_score + 0.1 * knn_score_image-vector + 0.5 * knn_score_title-vector

How to Modify Default Scoring Calculation?thanks
Expected modification:
score = 0.9 * match_score + max(0.1 *knn_score_image-vector, 0.5 * knn_score_title-vector)

In Elasticsearch versions >= 8.12, we added knn as a regular query in the DSL. This means you can use knn with a dis_max query.

For example:

{
	"query": {
		"bool": {
			"should": [
				{
					"dis_max": {
						"queries": [
							{
								"knn": {
									"field": "vector",
									"query_vector": [0.5, 0.5],
									"num_candidates": 1
								}
							},
							{
								"knn": {
									"field": "vector",
									"query_vector": [0.5, 0.5],
									"num_candidates": 1
								}
							}
						]
					}
				},
				{
					"match": {
						"field": "value"
					}
				}
			]
		}
	}
}
2 Likes

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