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
Copy file name to clipboardExpand all lines: _field-types/supported-field-types/knn-vector.md
+108-4Lines changed: 108 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,11 +4,12 @@ title: k-NN vector
4
4
nav_order: 58
5
5
has_children: false
6
6
parent: Supported field types
7
+
has_math: true
7
8
---
8
9
9
10
# k-NN vector
10
11
11
-
The k-NN plugin introduces a custom data type, the `knn_vector`, that allows users to ingest their k-NN vectors
12
+
The [k-NN plugin]({{site.url}}{{site.baseurl}}/search-plugins/knn/index/) introduces a custom data type, the `knn_vector`, that allows users to ingest their k-NN vectors.
12
13
into an OpenSearch index and perform different kinds of k-NN search. The `knn_vector` field is highly configurable and can serve many different k-NN workloads. In general, a `knn_vector` field can be built either by providing a method definition or specifying a model id.
13
14
14
15
## Example
@@ -47,7 +48,7 @@ PUT test-index
47
48
48
49
## Method definitions
49
50
50
-
Method definitions are used when the underlying Approximate k-NN algorithm does not require training. For example, the following `knn_vector` field specifies that *nmslib*'s implementation of *hnsw* should be used for Approximate k-NN search. During indexing, *nmslib* will build the corresponding *hnsw* segment files.
51
+
[Method definitions]({{site.url}}{{site.baseurl}}/search-plugins/knn/knn-index#method-definitions) are used when the underlying [approximate k-NN]({{site.url}}{{site.baseurl}}/search-plugins/knn/approximate-knn/) algorithm does not require training. For example, the following `knn_vector` field specifies that *nmslib*'s implementation of *hnsw* should be used for approximate k-NN search. During indexing, *nmslib* will build the corresponding *hnsw* segment files.
51
52
52
53
```json
53
54
"my_vector": {
@@ -77,7 +78,7 @@ model contains the information needed to initialize the native library segment f
77
78
}
78
79
```
79
80
80
-
However, if you intend to just use painless scripting or a k-NN score script, you only need to pass the dimension.
81
+
However, if you intend to use Painless scripting or a k-NN score script, you only need to pass the dimension.
81
82
```json
82
83
"type": "knn_vector",
83
84
"dimension": 128
@@ -91,6 +92,8 @@ By default, k-NN vectors are `float` vectors, where each dimension is 4 bytes. I
91
92
Byte vectors are supported only for the `lucene` engine. They are not supported for the `nmslib` and `faiss` engines.
92
93
{: .note}
93
94
95
+
In [k-NN benchmarking tests](https://github.com/opensearch-project/k-NN/tree/main/benchmarks/perf-tool), the use of `byte` rather than `float` vectors resulted in a significant reduction in storage and memory usage as well as improved indexing throughput and reduced query latency. Additionally, precision on recall was not greatly affected (note that recall can depend on various factors, such as the [quantization technique](#quantization-techniques) and data distribution).
96
+
94
97
When using `byte` vectors, expect some loss of precision in the recall compared to using `float` vectors. Byte vectors are useful in large-scale applications and use cases that prioritize a reduced memory footprint in exchange for a minimal loss of recall.
95
98
{: .important}
96
99
@@ -163,4 +166,105 @@ GET test-index/_search
163
166
}
164
167
}
165
168
```
166
-
{% include copy-curl.html %}
169
+
{% include copy-curl.html %}
170
+
171
+
### Quantization techniques
172
+
173
+
If your vectors are of the type `float`, you need to first convert them to the `byte` type before ingesting the documents. This conversion is accomplished by _quantizing the dataset_---reducing the precision of its vectors. There are many quantization techniques, such as scalar quantization or product quantization (PQ), which is used in the Faiss engine. The choice of quantization technique depends on the type of data you're using and can affect the accuracy of recall values. The following sections describe the scalar quantization algorithms that were used to quantize the [k-NN benchmarking test](https://github.com/opensearch-project/k-NN/tree/main/benchmarks/perf-tool) data for the [L2](#scalar-quantization-for-the-l2-space-type) and [cosine similarity](#scalar-quantization-for-the-cosine-similarity-space-type) space types. The provided pseudocode is for illustration purposes only.
174
+
175
+
#### Scalar quantization for the L2 space type
176
+
177
+
The following example pseudocode illustrates the scalar quantization technique used for the benchmarking tests on Euclidean datasets with the L2 space type. Euclidean distance is shift invariant. If you shift both $$x$$ and $$y$$ by the same $$z$$, then the distance remains the same ($$\lVert x-y\rVert =\lVert (x-z)-(y-z)\rVert$$).
178
+
179
+
```python
180
+
# Random dataset (Example to create a random dataset)
181
+
dataset = np.random.uniform(-300, 300, (100, 10))
182
+
# Random query set (Example to create a random queryset)
#### Scalar quantization for the cosine similarity space type
211
+
212
+
The following example pseudocode illustrates the scalar quantization technique used for the benchmarking tests on angular datasets with the cosine similarity space type. Cosine similarity is not shift invariant ($$cos(x, y) \neq cos(x-z, y-z)$$).
Copy file name to clipboardExpand all lines: _search-plugins/knn/knn-index.md
+4Lines changed: 4 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,6 +11,10 @@ has_children: false
11
11
The k-NN plugin introduces a custom data type, the `knn_vector`, that allows users to ingest their k-NN vectors
12
12
into an OpenSearch index and perform different kinds of k-NN search. The `knn_vector` field is highly configurable and can serve many different k-NN workloads. For more information, see [k-NN vector]({{site.url}}{{site.baseurl}}/field-types/supported-field-types/knn-vector/).
13
13
14
+
## Lucene byte vector
15
+
16
+
Starting with k-NN plugin version 2.9, you can use `byte` vectors with the `lucene` engine in order to reduce the amount of storage space needed. For more information, see [Lucene byte vector]({{site.url}}{{site.baseurl}}/field-types/supported-field-types/knn-vector#lucene-byte-vector).
17
+
14
18
## Method definitions
15
19
16
20
A method definition refers to the underlying configuration of the Approximate k-NN algorithm you want to use. Method definitions are used to either create a `knn_vector` field (when the method does not require training) or [create a model during training]({{site.url}}{{site.baseurl}}/search-plugins/knn/api#train-model) that can then be used to [create a `knn_vector` field]({{site.url}}{{site.baseurl}}/search-plugins/knn/approximate-knn/#building-a-k-nn-index-from-a-model).
0 commit comments