Browse Source

Add hybrid search to knn query documentation (#104562)

Relates to PR #98916
Closes elastic/search-docs-team#39
Mayya Sharipova 1 year ago
parent
commit
669d4ae9b9
1 changed files with 47 additions and 4 deletions
  1. 47 4
      docs/reference/query-dsl/knn-query.asciidoc

+ 47 - 4
docs/reference/query-dsl/knn-query.asciidoc

@@ -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.
-