You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Aug 16, 2022. It is now read-only.
Copy file name to clipboardExpand all lines: docs/knn/knn-score-script.md
+105-3Lines changed: 105 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,7 +10,7 @@ has_math: true
10
10
# Exact k-NN with Scoring Script
11
11
The k-NN plugin implements the Elasticsearch score script plugin that you can use to find the exact k-nearest neighbors to a given query point. Using the k-NN score script, you can apply a filter on an index before executing the nearest neighbor search. This is useful for dynamic search cases where the index body may vary based on other conditions. Because this approach executes a brute force search, it does not scale as well as the [Approximate approach](../approximate-knn). In some cases, it may be better to think about refactoring your workflow or index structure to use the Approximate approach instead of this approach.
12
12
13
-
## Getting started with the score script
13
+
## Getting started with the score script for vectors
14
14
15
15
Similar to approximate nearest neighbor search, in order to use the score script on a body of vectors, you must first create an index with one or more `knn_vector` fields. If you intend to just use the script score approach (and not the approximate approach) `index.knn` can be set to `false` and `index.knn.space_type` does not need to be set. The space type can be chosen during search. See the [spaces section](#spaces) to see what spaces the k-NN score script suppports. Here is an example that creates an index with two `knn_vector` fields:
16
16
@@ -32,8 +32,6 @@ PUT my-knn-index-1
32
32
}
33
33
```
34
34
35
-
*Note* -- For binary spaces, such as the Hamming bit space, `type` needs to be either `binary` or `long`. The binary data then needs to be encoded either as a base64 string or as a long (if the data is 64 bits or less).
36
-
37
35
If you *only* want to use the score script, you can omit `"index.knn": true`. The benefit of this approach is faster indexing speed and lower memory usage, but you lose the ability to perform standard k-NN queries on the index.
38
36
{: .tip}
39
37
@@ -172,6 +170,110 @@ GET my-knn-index-2/_search
172
170
}
173
171
```
174
172
173
+
## Getting started with the score script for binary data
174
+
The k-NN score script also allows you to run k-NN search on your binary data with the Hamming distance space.
175
+
In order to use Hamming distance, the field of interest must have either a `binary` or `long` field type. If you're using `binary` type, the data must be a base64-encoded string.
176
+
177
+
This example shows how to use the Hamming distance space with a `binary` field type:
Similarly, you can encode your data with the `long` field and run a search:
247
+
248
+
```json
249
+
GET my-long-index/_search
250
+
{
251
+
"size": 2,
252
+
"query": {
253
+
"script_score": {
254
+
"query": {
255
+
"bool": {
256
+
"filter": {
257
+
"term": {
258
+
"color": "BLUE"
259
+
}
260
+
}
261
+
}
262
+
},
263
+
"script": {
264
+
"lang": "knn",
265
+
"source": "knn_score",
266
+
"params": {
267
+
"field": "my_long",
268
+
"query_value": 23,
269
+
"space_type": "hammingbit"
270
+
}
271
+
}
272
+
}
273
+
}
274
+
}
275
+
```
276
+
175
277
## Spaces
176
278
177
279
A space corresponds to the function used to measure the distance between 2 points in order to determine the k-nearest neighbors. From the k-NN perspective, a lower score equates to a closer and better result. This is the opposite of how Elasticsearch scores results, where a greater score equates to a better result. We include the conversions to Elasticsearch scores in the table below:
0 commit comments