Skip to content

Commit 669d4ae

Browse files
Add hybrid search to knn query documentation (#104562)
Relates to PR #98916 Closes elastic/developer-docs-team#39
1 parent 528163e commit 669d4ae

File tree

1 file changed

+47
-4
lines changed

1 file changed

+47
-4
lines changed

docs/reference/query-dsl/knn-query.asciidoc

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ PUT my-image-index
2727
},
2828
"file-type": {
2929
"type": "keyword"
30+
},
31+
"title": {
32+
"type": "text"
3033
}
3134
}
3235
}
@@ -39,11 +42,11 @@ PUT my-image-index
3942
----
4043
POST my-image-index/_bulk?refresh=true
4144
{ "index": { "_id": "1" } }
42-
{ "image-vector": [1, 5, -20], "file-type": "jpg" }
45+
{ "image-vector": [1, 5, -20], "file-type": "jpg", "title": "mountain lake" }
4346
{ "index": { "_id": "2" } }
44-
{ "image-vector": [42, 8, -15], "file-type": "png" }
47+
{ "image-vector": [42, 8, -15], "file-type": "png", "title": "frozen lake"}
4548
{ "index": { "_id": "3" } }
46-
{ "image-vector": [15, 11, 23], "file-type": "jpg" }
49+
{ "image-vector": [15, 11, 23], "file-type": "jpg", "title": "mountain lake lodge" }
4750
----
4851
//TEST[continued]
4952

@@ -176,6 +179,47 @@ POST my-image-index/_search
176179
----
177180
//TEST[continued]
178181

182+
[[knn-query-in-hybrid-search]]
183+
==== Hybrid search with knn query
184+
Knn query can be used as a part of hybrid search, where knn query is combined
185+
with other lexical queries. For example, the query below finds documents with
186+
`title` matching `mountain lake`, and combines them with the top 10 documents
187+
that have the closest image vectors to the `query_vector`. The combined documents
188+
are then scored and the top 3 top scored documents are returned.
189+
190+
+
191+
[source,console]
192+
----
193+
POST my-image-index/_search
194+
{
195+
"size" : 3,
196+
"query": {
197+
"bool": {
198+
"should": [
199+
{
200+
"match": {
201+
"title": {
202+
"query": "mountain lake",
203+
"boost": 1
204+
}
205+
}
206+
},
207+
{
208+
"knn": {
209+
"field": "image-vector",
210+
"query_vector": [-5, 9, -12],
211+
"num_candidates": 10,
212+
"boost": 2
213+
}
214+
}
215+
]
216+
}
217+
}
218+
}
219+
----
220+
//TEST[continued]
221+
222+
179223
[[knn-query-with-nested-query]]
180224
==== Knn query inside a nested query
181225

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

0 commit comments

Comments
 (0)