|
@@ -27,6 +27,9 @@ PUT my-image-index
|
|
|
},
|
|
|
"file-type": {
|
|
|
"type": "keyword"
|
|
|
+ },
|
|
|
+ "title": {
|
|
|
+ "type": "text"
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -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]
|
|
|
|
|
@@ -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
|
|
|
|
|
@@ -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.
|
|
|
-
|