k-NN vector field type

The k-NN plugin introduces a custom data type, the knn_vector, that allows users to ingest their k-NN vectors 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.

Example

For example, to map my_vector1 as a knn_vector, use the following request:

  1. PUT test-index
  2. {
  3. "settings": {
  4. "index": {
  5. "knn": true,
  6. "knn.algo_param.ef_search": 100
  7. }
  8. },
  9. "mappings": {
  10. "properties": {
  11. "my_vector1": {
  12. "type": "knn_vector",
  13. "dimension": 3,
  14. "method": {
  15. "name": "hnsw",
  16. "space_type": "l2",
  17. "engine": "lucene",
  18. "parameters": {
  19. "ef_construction": 128,
  20. "m": 24
  21. }
  22. }
  23. }
  24. }
  25. }
  26. }

copy

Method definitions

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.

  1. "my_vector": {
  2. "type": "knn_vector",
  3. "dimension": 4,
  4. "method": {
  5. "name": "hnsw",
  6. "space_type": "l2",
  7. "engine": "nmslib",
  8. "parameters": {
  9. "ef_construction": 128,
  10. "m": 24
  11. }
  12. }
  13. }

Model IDs

Model IDs are used when the underlying Approximate k-NN algorithm requires a training step. As a prerequisite, the model has to be created with the Train API. The model contains the information needed to initialize the native library segment files.

  1. "type": "knn_vector",
  2. "model_id": "my-model"
  3. }

However, if you intend to use Painless scripting or a k-NN score script, you only need to pass the dimension.

  1. "type": "knn_vector",
  2. "dimension": 128
  3. }

Lucene byte vector

By default, k-NN vectors are float vectors, where each dimension is 4 bytes. If you want to save storage space, you can use byte vectors with the lucene engine. In a byte vector, each dimension is a signed 8-bit integer in the [-128, 127] range.

Byte vectors are supported only for the lucene engine. They are not supported for the nmslib and faiss engines.

In k-NN benchmarking tests, 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 and data distribution).

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.

Introduced in k-NN plugin version 2.9, the optional data_type parameter defines the data type of a vector. The default value of this parameter is float.

To use a byte vector, set the data_type parameter to byte when creating mappings for an index:

  1. PUT test-index
  2. {
  3. "settings": {
  4. "index": {
  5. "knn": true,
  6. "knn.algo_param.ef_search": 100
  7. }
  8. },
  9. "mappings": {
  10. "properties": {
  11. "my_vector1": {
  12. "type": "knn_vector",
  13. "dimension": 3,
  14. "data_type": "byte",
  15. "method": {
  16. "name": "hnsw",
  17. "space_type": "l2",
  18. "engine": "lucene",
  19. "parameters": {
  20. "ef_construction": 128,
  21. "m": 24
  22. }
  23. }
  24. }
  25. }
  26. }
  27. }

copy

Then ingest documents as usual. Make sure each dimension in the vector is in the supported [-128, 127] range:

  1. PUT test-index/_doc/1
  2. {
  3. "my_vector1": [-126, 28, 127]
  4. }

copy

  1. PUT test-index/_doc/2
  2. {
  3. "my_vector1": [100, -128, 0]
  4. }

copy

When querying, be sure to use a byte vector:

  1. GET test-index/_search
  2. {
  3. "size": 2,
  4. "query": {
  5. "knn": {
  6. "my_vector1": {
  7. "vector": [26, -120, 99],
  8. "k": 2
  9. }
  10. }
  11. }
  12. }

copy

Quantization techniques

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 data for the L2 and cosine similarity space types. The provided pseudocode is for illustration purposes only.

Scalar quantization for the L2 space type

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\)).

  1. # Random dataset (Example to create a random dataset)
  2. dataset = np.random.uniform(-300, 300, (100, 10))
  3. # Random query set (Example to create a random queryset)
  4. queryset = np.random.uniform(-350, 350, (100, 10))
  5. # Number of values
  6. B = 256
  7. # INDEXING:
  8. # Get min and max
  9. dataset_min = np.min(dataset)
  10. dataset_max = np.max(dataset)
  11. # Shift coordinates to be non-negative
  12. dataset -= dataset_min
  13. # Normalize into [0, 1]
  14. dataset *= 1. / (dataset_max - dataset_min)
  15. # Bucket into 256 values
  16. dataset = np.floor(dataset * (B - 1)) - int(B / 2)
  17. # QUERYING:
  18. # Clip (if queryset range is out of datset range)
  19. queryset = queryset.clip(dataset_min, dataset_max)
  20. # Shift coordinates to be non-negative
  21. queryset -= dataset_min
  22. # Normalize
  23. queryset *= 1. / (dataset_max - dataset_min)
  24. # Bucket into 256 values
  25. queryset = np.floor(queryset * (B - 1)) - int(B / 2)

copy

Scalar quantization for the cosine similarity space type

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)\)).

The following pseudocode is for positive numbers:

  1. # For Positive Numbers
  2. # INDEXING and QUERYING:
  3. # Get Max of train dataset
  4. max = np.max(dataset)
  5. min = 0
  6. B = 127
  7. # Normalize into [0,1]
  8. val = (val - min) / (max - min)
  9. val = (val * B)
  10. # Get int and fraction values
  11. int_part = floor(val)
  12. frac_part = val - int_part
  13. if 0.5 < frac_part:
  14. bval = int_part + 1
  15. else:
  16. bval = int_part
  17. return Byte(bval)

copy

The following pseudocode is for negative numbers:

  1. # For Negative Numbers
  2. # INDEXING and QUERYING:
  3. # Get Min of train dataset
  4. min = 0
  5. max = -np.min(dataset)
  6. B = 128
  7. # Normalize into [0,1]
  8. val = (val - min) / (max - min)
  9. val = (val * B)
  10. # Get int and fraction values
  11. int_part = floor(var)
  12. frac_part = val - int_part
  13. if 0.5 < frac_part:
  14. bval = int_part + 1
  15. else:
  16. bval = int_part
  17. return Byte(bval)

copy