Skip to content

[FEATURE] Provide context on Inner hit of multi vector to aid highlighting/debug use cases #1447

Closed
@heemin32

Description

@heemin32

As a follow up item on #1065, I would like to see inner hit of nested field so that I know which item inside nested fields are matched.

Create KNN index

PUT /my-knn-index-1
{
  "settings": {
    "index": {
      "knn": true,
      "knn.algo_param.ef_search": 100
    }
  },
  "mappings": {
    "properties": {
      "nested_field": {
        "type": "nested",
        "properties": {
          "my_vector": {
            "type": "knn_vector",
            "dimension": 3,
            "method": {
              "name": "hnsw",
              "space_type": "l2",
              "engine": "lucene",
              "parameters": {
                "ef_construction": 100,
                "m": 16
              }
            }
          }
        }
      }
    }
  }
}

Insert Data

PUT /_bulk?refresh=true
{ "index": { "_index": "my-knn-index-1", "_id": "1" } }
{"parking": false, "nested_field":[{"my_vector":[1,1,1]},{"my_vector":[2,2,2]},{"my_vector":[3,3,3]}]}
{ "index": { "_index": "my-knn-index-1", "_id": "2" } }
{"parking": true, "nested_field":[{"my_vector":[10,10,10]},{"my_vector":[20,20,20]},{"my_vector":[30,30,30]}]}
{ "index": { "_index": "my-knn-index-1", "_id": "3" } }
{"parking": true, "nested_field":[{"my_vector":[100,100,100]},{"my_vector":[200,200,200]},{"my_vector":[300,300,300]}]}

Query

GET /my-knn-index-1/_search
{
	"query": {
		"nested": {
			"path": "nested_field",
			"query": {
				"knn": {
					"nested_field.my_vector": {
						"vector": [
							1,
							1,
							1
						],
						"k": 2
					}
				}
			},
			"inner_hits": {}
		}
	}
}

Response

It returns two nearest values among entire nested values, but not best one for each document.

{
	"took": 9,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 2,
			"relation": "eq"
		},
		"max_score": 1.0,
		"hits": [
			{
				"_index": "my-knn-index-1",
				"_id": "1",
				"_score": 1.0,
				"_source": {
					"parking": false,
					"nested_field": [
						{
							"my_vector": [
								1,
								1,
								1
							]
						},
						{
							"my_vector": [
								2,
								2,
								2
							]
						},
						{
							"my_vector": [
								3,
								3,
								3
							]
						}
					]
				},
				"inner_hits": {
					"nested_field": {
						"hits": {
							"total": {
								"value": 2,
								"relation": "eq"
							},
							"max_score": 1.0,
							"hits": [
								{
									"_index": "my-knn-index-1",
									"_id": "1",
									"_nested": {
										"field": "nested_field",
										"offset": 0
									},
									"_score": 1.0,
									"_source": {
										"my_vector": [
											1,
											1,
											1
										]
									}
								},
								{
									"_index": "my-knn-index-1",
									"_id": "1",
									"_nested": {
										"field": "nested_field",
										"offset": 1
									},
									"_score": 0.25,
									"_source": {
										"my_vector": [
											2,
											2,
											2
										]
									}
								}
							]
						}
					}
				}
			},
			{
				"_index": "my-knn-index-1",
				"_id": "3",
				"_score": 3.400898E-5,
				"_source": {
					"parking": true,
					"nested_field": [
						{
							"my_vector": [
								100,
								100,
								100
							]
						},
						{
							"my_vector": [
								200,
								200,
								200
							]
						},
						{
							"my_vector": [
								300,
								300,
								300
							]
						}
					]
				},
				"inner_hits": {
					"nested_field": {
						"hits": {
							"total": {
								"value": 0,
								"relation": "eq"
							},
							"max_score": null,
							"hits": []
						}
					}
				}
			}
		]
	}
}

Metadata

Metadata

Assignees

Type

No type

Projects

Status

✅ Done

Status

2.15.0 (Release window opens on June 10th, 2024 and closes on June 25th, 2024)

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions