Skip to content

[8.12] Add hybrid search to knn query documentation (#104562) #104565

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 47 additions & 4 deletions docs/reference/query-dsl/knn-query.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ PUT my-image-index
},
"file-type": {
"type": "keyword"
},
"title": {
"type": "text"
}
}
}
Expand All @@ -39,11 +42,11 @@ PUT my-image-index
----
POST my-image-index/_bulk?refresh=true
{ "index": { "_id": "1" } }
{ "image-vector": [1, 5, -20], "file-type": "jpg" }
{ "image-vector": [1, 5, -20], "file-type": "jpg", "title": "mountain lake" }
{ "index": { "_id": "2" } }
{ "image-vector": [42, 8, -15], "file-type": "png" }
{ "image-vector": [42, 8, -15], "file-type": "png", "title": "frozen lake"}
{ "index": { "_id": "3" } }
{ "image-vector": [15, 11, 23], "file-type": "jpg" }
{ "image-vector": [15, 11, 23], "file-type": "jpg", "title": "mountain lake lodge" }
----
//TEST[continued]

Expand Down Expand Up @@ -176,6 +179,47 @@ POST my-image-index/_search
----
//TEST[continued]

[[knn-query-in-hybrid-search]]
==== Hybrid search with knn query
Knn query can be used as a part of hybrid search, where knn query is combined
with other lexical queries. For example, the query below finds documents with
`title` matching `mountain lake`, and combines them with the top 10 documents
that have the closest image vectors to the `query_vector`. The combined documents
are then scored and the top 3 top scored documents are returned.

+
[source,console]
----
POST my-image-index/_search
{
"size" : 3,
"query": {
"bool": {
"should": [
{
"match": {
"title": {
"query": "mountain lake",
"boost": 1
}
}
},
{
"knn": {
"field": "image-vector",
"query_vector": [-5, 9, -12],
"num_candidates": 10,
"boost": 2
}
}
]
}
}
}
----
//TEST[continued]


[[knn-query-with-nested-query]]
==== Knn query inside a nested query

Expand Down Expand Up @@ -219,4 +263,3 @@ Thus, the final results from aggregations contain
`num_candidates * number_of_shards` documents. This is different from
the <<knn-search,top level knn section>> where aggregations are
calculated on the global top k nearest documents.