Skip to content

Commit

Permalink
Add documentation for innerHit on knn nested field
Browse files Browse the repository at this point in the history
Signed-off-by: Heemin Kim <heemin@amazon.com>
  • Loading branch information
heemin32 committed Jun 18, 2024
1 parent b3573df commit a799183
Showing 1 changed file with 136 additions and 11 deletions.
147 changes: 136 additions & 11 deletions _search-plugins/knn/nested-search-knn.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ PUT my-knn-index-1
"m": 16
}
}
},
"color": {
"type": "text",
"index": false
}
}
}
Expand All @@ -62,9 +66,9 @@ After you create the index, add some data to it:
```json
PUT _bulk?refresh=true
{ "index": { "_index": "my-knn-index-1", "_id": "1" } }
{"nested_field":[{"my_vector":[1,1,1]},{"my_vector":[2,2,2]},{"my_vector":[3,3,3]}]}
{"nested_field":[{"my_vector":[1,1,1], "color": "blue"},{"my_vector":[2,2,2], "color": "yellow"},{"my_vector":[3,3,3], "color": "white"}]}
{ "index": { "_index": "my-knn-index-1", "_id": "2" } }
{"nested_field":[{"my_vector":[10,10,10]},{"my_vector":[20,20,20]},{"my_vector":[30,30,30]}]}
{"nested_field":[{"my_vector":[10,10,10], "color": "red"},{"my_vector":[20,20,20], "color": "green"},{"my_vector":[30,30,30], "color": "black"}]}
```
{% include copy-curl.html %}

Expand Down Expand Up @@ -94,7 +98,7 @@ Even though all three vectors nearest to the query vector are in document 1, the

```json
{
"took": 23,
"took": 5,
"timed_out": false,
"_shards": {
"total": 1,
Expand All @@ -107,34 +111,37 @@ Even though all three vectors nearest to the query vector are in document 1, the
"value": 2,
"relation": "eq"
},
"max_score": 1,
"max_score": 1.0,
"hits": [
{
"_index": "my-knn-index-1",
"_id": "1",
"_score": 1,
"_score": 1.0,
"_source": {
"nested_field": [
{
"my_vector": [
1,
1,
1
]
],
"color": "blue"
},
{
"my_vector": [
2,
2,
2
]
],
"color": "yellow"
},
{
"my_vector": [
3,
3,
3
]
],
"color": "white"
}
]
}
Expand All @@ -150,21 +157,24 @@ Even though all three vectors nearest to the query vector are in document 1, the
10,
10,
10
]
],
"color": "red"
},
{
"my_vector": [
20,
20,
20
]
],
"color": "green"
},
{
"my_vector": [
30,
30,
30
]
],
"color": "black"
}
]
}
Expand All @@ -174,6 +184,121 @@ Even though all three vectors nearest to the query vector are in document 1, the
}
```

## InnerHit with nested fields
If you want to retrieve only the matched documents in nested fields, you can use `inner_hits` in your query.
The following example returns only the `color` field of the matched documents within the nested fields.
```json
GET my-knn-index-1/_search
{
"_source": false,
"query": {
"nested": {
"path": "nested_field",
"query": {
"knn": {
"nested_field.my_vector": {
"vector": [1,1,1],
"k": 2
}
}
},
"inner_hits": {
"_source": false,
"fields":["nested_field.color"]
}
}
}
}
```
{% include copy-curl.html %}

`inner_hits` contains only the matched documents from the nested fields.
```json
{
"took": 4,
"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,
"inner_hits": {
"nested_field": {
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "my-knn-index-1",
"_id": "1",
"_nested": {
"field": "nested_field",
"offset": 0
},
"_score": 1.0,
"fields": {
"nested_field.color": [
"blue"
]
}
}
]
}
}
}
},
{
"_index": "my-knn-index-1",
"_id": "2",
"_score": 0.0040983604,
"inner_hits": {
"nested_field": {
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 0.0040983604,
"hits": [
{
"_index": "my-knn-index-1",
"_id": "2",
"_nested": {
"field": "nested_field",
"offset": 0
},
"_score": 0.0040983604,
"fields": {
"nested_field.color": [
"red"
]
}
}
]
}
}
}
}
]
}
}
```

## k-NN search with filtering on nested fields

You can apply a filter to a k-NN search with nested fields. A filter can be applied to either a top-level field or a field inside a nested field.
Expand Down

0 comments on commit a799183

Please sign in to comment.