|
| 1 | +[[query-dsl-knn-query]] |
| 2 | +=== Knn query |
| 3 | +++++ |
| 4 | +<titleabbrev>Knn</titleabbrev> |
| 5 | +++++ |
| 6 | + |
| 7 | +Finds the _k_ nearest vectors to a query vector, as measured by a similarity |
| 8 | +metric. _knn_ query finds nearest vectors through approximate search on indexed |
| 9 | +dense_vectors. The preferred way to do approximate kNN search is through the |
| 10 | +<<knn-search,top level knn section>> of a search request. _knn_ query is reserved for |
| 11 | +expert cases, where there is a need to combine this query with other queries. |
| 12 | + |
| 13 | +[[knn-query-ex-request]] |
| 14 | +==== Example request |
| 15 | + |
| 16 | +[source,console] |
| 17 | +---- |
| 18 | +PUT my-image-index |
| 19 | +{ |
| 20 | + "mappings": { |
| 21 | + "properties": { |
| 22 | + "image-vector": { |
| 23 | + "type": "dense_vector", |
| 24 | + "dims": 3, |
| 25 | + "index": true, |
| 26 | + "similarity": "l2_norm" |
| 27 | + }, |
| 28 | + "file-type": { |
| 29 | + "type": "keyword" |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | +} |
| 34 | +---- |
| 35 | + |
| 36 | +. Index your data. |
| 37 | ++ |
| 38 | +[source,console] |
| 39 | +---- |
| 40 | +POST my-image-index/_bulk?refresh=true |
| 41 | +{ "index": { "_id": "1" } } |
| 42 | +{ "image-vector": [1, 5, -20], "file-type": "jpg" } |
| 43 | +{ "index": { "_id": "2" } } |
| 44 | +{ "image-vector": [42, 8, -15], "file-type": "png" } |
| 45 | +{ "index": { "_id": "3" } } |
| 46 | +{ "image-vector": [15, 11, 23], "file-type": "jpg" } |
| 47 | +---- |
| 48 | +//TEST[continued] |
| 49 | + |
| 50 | +. Run the search using the `knn` query, asking for the top 3 nearest vectors. |
| 51 | ++ |
| 52 | +[source,console] |
| 53 | +---- |
| 54 | +POST my-image-index/_search |
| 55 | +{ |
| 56 | + "size" : 3, |
| 57 | + "query" : { |
| 58 | + "knn": { |
| 59 | + "field": "image-vector", |
| 60 | + "query_vector": [-5, 9, -12], |
| 61 | + "num_candidates": 10 |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | +---- |
| 66 | +//TEST[continued] |
| 67 | + |
| 68 | +NOTE: `knn` query doesn't have a separate `k` parameter. `k` is defined by |
| 69 | +`size` parameter of a search request similar to other queries. `knn` query |
| 70 | +collects `num_candidates` results from each shard, then merges them to get |
| 71 | +the top `size` results. |
| 72 | + |
| 73 | + |
| 74 | +[[knn-query-top-level-parameters]] |
| 75 | +==== Top-level parameters for `knn` |
| 76 | + |
| 77 | +`field`:: |
| 78 | ++ |
| 79 | +-- |
| 80 | +(Required, string) The name of the vector field to search against. Must be a |
| 81 | +<<index-vectors-knn-search, `dense_vector` field with indexing enabled>>. |
| 82 | +-- |
| 83 | + |
| 84 | +`query_vector`:: |
| 85 | ++ |
| 86 | +-- |
| 87 | +(Required, array of floats) Query vector. Must have the same number of dimensions |
| 88 | +as the vector field you are searching against. |
| 89 | +-- |
| 90 | + |
| 91 | +`num_candidates`:: |
| 92 | ++ |
| 93 | +-- |
| 94 | +(Required, integer) The number of nearest neighbor candidates to consider per shard. |
| 95 | +Cannot exceed 10,000. {es} collects `num_candidates` results from each shard, then |
| 96 | +merges them to find the top results. Increasing `num_candidates` tends to improve the |
| 97 | +accuracy of the final results. |
| 98 | +-- |
| 99 | + |
| 100 | +`filter`:: |
| 101 | ++ |
| 102 | +-- |
| 103 | +(Optional, query object) Query to filter the documents that can match. |
| 104 | +The kNN search will return the top documents that also match this filter. |
| 105 | +The value can be a single query or a list of queries. If `filter` is not provided, |
| 106 | +all documents are allowed to match. |
| 107 | + |
| 108 | +The filter is a pre-filter, meaning that it is applied **during** the approximate |
| 109 | +kNN search to ensure that `num_candidates` matching documents are returned. |
| 110 | +-- |
| 111 | + |
| 112 | +`similarity`:: |
| 113 | ++ |
| 114 | +-- |
| 115 | +(Optional, float) The minimum similarity required for a document to be considered |
| 116 | +a match. The similarity value calculated relates to the raw |
| 117 | +<<dense-vector-similarity, `similarity`>> used. Not the document score. The matched |
| 118 | +documents are then scored according to <<dense-vector-similarity, `similarity`>> |
| 119 | +and the provided `boost` is applied. |
| 120 | +-- |
| 121 | + |
| 122 | +`boost`:: |
| 123 | ++ |
| 124 | +-- |
| 125 | +(Optional, float) Floating point number used to multiply the |
| 126 | +scores of matched documents. This value cannot be negative. Defaults to `1.0`. |
| 127 | +-- |
| 128 | + |
| 129 | +`_name`:: |
| 130 | ++ |
| 131 | +-- |
| 132 | +(Optional, string) Name field to identify the query |
| 133 | +-- |
| 134 | + |
| 135 | +[[knn-query-filtering]] |
| 136 | +==== Pre-filters and post-filters in knn query |
| 137 | + |
| 138 | +There are two ways to filter documents that match a kNN query: |
| 139 | + |
| 140 | +. **pre-filtering** – filter is applied during the approximate kNN search |
| 141 | +to ensure that `k` matching documents are returned. |
| 142 | +. **post-filtering** – filter is applied after the approximate kNN search |
| 143 | +completes, which results in fewer than k results, even when there are enough |
| 144 | +matching documents. |
| 145 | + |
| 146 | +Pre-filtering is supported through the `filter` parameter of the `knn` query. |
| 147 | +Also filters from <<filter-alias,aliases>> are applied as pre-filters. |
| 148 | + |
| 149 | +All other filters found in the Query DSL tree are applied as post-filters. |
| 150 | +For example, `knn` query finds the top 3 documents with the nearest vectors |
| 151 | +(num_candidates=3), which are combined with `term` filter, that is |
| 152 | +post-filtered. The final set of documents will contain only a single document |
| 153 | +that passes the post-filter. |
| 154 | + |
| 155 | + |
| 156 | +[source,console] |
| 157 | +---- |
| 158 | +POST my-image-index/_search |
| 159 | +{ |
| 160 | + "size" : 10, |
| 161 | + "query" : { |
| 162 | + "bool" : { |
| 163 | + "must" : { |
| 164 | + "knn": { |
| 165 | + "field": "image-vector", |
| 166 | + "query_vector": [-5, 9, -12], |
| 167 | + "num_candidates": 3 |
| 168 | + } |
| 169 | + }, |
| 170 | + "filter" : { |
| 171 | + "term" : { "file-type" : "png" } |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | +} |
| 176 | +---- |
| 177 | +//TEST[continued] |
| 178 | + |
| 179 | +[[knn-query-with-nested-query]] |
| 180 | +==== Knn query inside a nested query |
| 181 | + |
| 182 | +`knn` query can be used inside a nested query. The behaviour here is similar |
| 183 | +to <<nested-knn-search, top level nested kNN search>>: |
| 184 | + |
| 185 | +* kNN search over nested dense_vectors diversifies the top results over |
| 186 | +the top-level document |
| 187 | +* `filter` over the top-level document metadata is supported and acts as a |
| 188 | +post-filter |
| 189 | +* `filter` over `nested` field metadata is not supported |
| 190 | + |
| 191 | +A sample query can look like below: |
| 192 | + |
| 193 | +[source,js] |
| 194 | +---- |
| 195 | +{ |
| 196 | + "query" : { |
| 197 | + "nested" : { |
| 198 | + "path" : "paragraph", |
| 199 | + "query" : { |
| 200 | + "knn": { |
| 201 | + "query_vector": [ |
| 202 | + 0.45, |
| 203 | + 45 |
| 204 | + ], |
| 205 | + "field": "paragraph.vector", |
| 206 | + "num_candidates": 2 |
| 207 | + } |
| 208 | + } |
| 209 | + } |
| 210 | + } |
| 211 | +} |
| 212 | +---- |
| 213 | +// NOTCONSOLE |
| 214 | + |
| 215 | +[[knn-query-aggregations]] |
| 216 | +==== Knn query with aggregations |
| 217 | +`knn` query calculates aggregations on `num_candidates` from each shard. |
| 218 | +Thus, the final results from aggregations contain |
| 219 | +`num_candidates * number_of_shards` documents. This is different from |
| 220 | +the <<knn-search,top level knn section>> where aggregations are |
| 221 | +calculated on the global top k nearest documents. |
| 222 | + |
0 commit comments