---
layout: default
title: Methods and engines
parent: k-NN vector
grand_parent: Supported field types
redirect_from:
- /field-types/supported-field-types/knn-methods-engines/
nav_order: 20
---
# Methods and engines
A _method_ defines the algorithm used for organizing vector data at indexing time and searching it at search time in [approximate k-NN search]({{site.url}}{{site.baseurl}}/vector-search/vector-search-techniques/approximate-knn/).
OpenSearch supports the following methods:
- **Hierarchical Navigable Small World (HNSW)** creates a hierarchical graph structure of connections between vectors. For more information about the algorithm, see [Efficient and robust approximate nearest neighbor search using Hierarchical Navigable Small World graphs](https://arxiv.org/abs/1603.09320).
- **Inverted File Index (IVF)** organizes vectors into buckets based on clustering and, during search, searches only a subset of the buckets.
An _engine_ is the library that implements these methods. Different engines can implement the same method, sometimes with varying optimizations or characteristics. For example, HNSW is implemented by all supported engines, each with its own advantages.
OpenSearch supports the following engines:
- [**Lucene**](#lucene-engine): The native search library, offering an HNSW implementation with efficient filtering capabilities
- [**Faiss**](#faiss-engine) (Facebook AI Similarity Search): A comprehensive library implementing both the HNSW and IVF methods, with additional vector compression options
- [**NMSLIB**](#nmslib-engine-deprecated) (Non-Metric Space Library): A legacy implementation of HNSW (now deprecated)
- [**JVector**](#jvector-engine): A pure-Java implementation of DiskANN-style search using the `disk_ann` method, with concurrent inserts, incremental merges, and native product quantization (PQ), available through the [`opensearch-jvector` plugin]({{site.url}}{{site.baseurl}}/install-and-configure/additional-plugins/opensearch-jvector/)
## Method definition example
A method definition contains the following components:
- The `name` of the method (for example, `hnsw` or `ivf`)
- The `space_type` for which the method is built (for example, `l2` or `cosinesimil`)
- The `engine` that will implement the method (for example, `faiss` or `lucene`)
- A map of `parameters` specific to that implementation
The following example configures an `hnsw` method with the `l2` space type, the `faiss` engine, and the method-specific parameters:
```json
PUT test-index
{
"settings": {
"index": {
"knn": true,
"knn.algo_param.ef_search": 100
}
},
"mappings": {
"properties": {
"my_vector1": {
"type": "knn_vector",
"dimension": 1024,
"method": {
"name": "hnsw",
"space_type": "l2",
"engine": "faiss",
"parameters": {
"ef_construction": 128,
"m": 24
}
}
}
}
}
}
```
{% include copy-curl.html %}
Not every method/engine combination supports each of the spaces. For a list of supported spaces, see the section for a specific engine.
{: .note}
## Common parameters
The following parameters are common to all method definitions.
Mapping parameter | Required | Default | Updatable after index creation | Description
:--- | :--- | :--- | :--- | :---
`name` | Yes | N/A | No | The nearest neighbor method. Valid values are `hnsw`, `ivf`, `flat`, and `disk_ann`. Not every engine combination supports each of the methods. For a list of supported methods, see the section for a specific engine.
`space_type` | No | `l2` | No | The vector space used to calculate the distance between vectors. Valid values are `l1`, `l2`, `linf`, `cosinesimil`, `innerproduct`, `hamming`, and `hammingbit`. Not every method/engine combination supports each of the spaces. For a list of supported spaces, see the section for a specific engine. Note: This value can also be specified at the top level of the mapping. For more information, see [Spaces]({{site.url}}{{site.baseurl}}/mappings/supported-field-types/knn-spaces/).
`engine` | No | `faiss` | No | The approximate k-NN library to use for indexing and search. Valid values are `faiss`, `lucene`, `nmslib` (deprecated), and `jvector` (requires the [`opensearch-jvector` plugin]({{site.url}}{{site.baseurl}}/install-and-configure/additional-plugins/opensearch-jvector/)).
`parameters` | No | `null` | No | The parameters used for the nearest neighbor method. For more information, see the section for a specific engine.
## Lucene engine
The Lucene engine provides a native implementation of vector search directly within Lucene. It offers efficient filtering capabilities and is well suited for smaller deployments.
### Supported methods
The Lucene engine supports the following method.
Method name | Requires training | Supported spaces
:--- | :--- |:---
[`hnsw`](#hnsw-parameters) | No | `l2`, `cosinesimil`, `innerproduct` (supported in OpenSearch 2.13 and later)
[`flat`](#flat-parameters) | No | `l2`, `cosinesimil`, `innerproduct` (supported in OpenSearch 3.6 and later)
#### HNSW parameters
The HNSW method supports the following parameters.
Parameter name | Required | Default | Updatable after index creation | Description
:--- | :--- | :--- | :--- | :---
`ef_construction` | No | 100 | No | The size of the dynamic list used during k-NN graph creation. Higher values result in a more accurate graph but slower indexing speed.
Note: Lucene uses the term `beam_width` internally, but the OpenSearch documentation uses `ef_construction` for consistency.
`m` | No | 16 | No | The number of bidirectional links created for each new element. Impacts memory consumption significantly. Keep between `2` and `100`.
Note: Lucene uses the term `max_connections` internally, but the OpenSearch documentation uses `m` for consistency.
The Lucene HNSW implementation ignores `ef_search` and dynamically sets it to the value of "k" in the search request. There is therefore no need to configure settings for `ef_search` when using the Lucene engine.
{: .note}
An index created in OpenSearch version 2.11 or earlier will still use the previous `ef_construction` value (`512`).
{: .note}
### Example configuration
```json
"method": {
"name": "hnsw",
"engine": "lucene",
"parameters": {
"m": 2048,
"ef_construction": 245
}
}
```
#### Flat parameters
The `flat` method does not support any method-level parameters. To select the scalar quantization applied to the vectors, set the `compression_level` field on the `knn_vector` mapping. The `flat` method supports the `32x`, `16x`, and `8x` compression levels, corresponding to 1-bit, 2-bit, and 4-bit scalar quantization, respectively. If `compression_level` is not specified, `flat` defaults to `32x`.
The `flat` method is engine-agnostic and does not accept the `engine` parameter. Specifying `engine` at either the method level or the field level for a `flat` method causes index creation to fail. Indexes created in OpenSearch 3.8 or earlier are unaffected.
{: .important}
For more information, see [Exact search using scalar quantization]({{site.url}}{{site.baseurl}}/vector-search/optimizing-storage/exact-search-scalar-quantization/).
### Example configuration
```json
"method": {
"name": "flat"
}
```
## Faiss engine
The Faiss engine provides advanced vector indexing capabilities with support for multiple methods and encoding options to optimize memory usage and search performance.
### Supported methods
The Faiss engine supports the following methods.
Method name | Requires training | Supported spaces
:--- | :--- |:---
[`hnsw`](#hnsw-parameters-1) | No | `l2`, `innerproduct` (not available when [PQ](#pq-parameters) is used), `hamming`, and `cosinesimil` (supported in OpenSearch 2.19 and later).
[`ivf`](#ivf-parameters) | Yes | `l2`, `innerproduct`, `hamming` (supported for binary vectors in OpenSearch version 2.16 and later. For more information, see [Binary k-NN vectors]({{site.url}}{{site.baseurl}}/mappings/supported-field-types/knn-memory-optimized#binary-vectors), `cosinesimil` (supported in OpenSearch 2.19 and later).
When using `cosinesimil` with the Faiss engine, vectors are automatically normalized to unit length during indexing because Faiss uses inner product on normalized vectors internally. As a result, stored vector values will differ from input values.
{: .important}
#### HNSW parameters
The `hnsw` method supports the following parameters.
Parameter name | Required | Default | Updatable after index creation | Description
:--- | :--- | :--- | :--- | :---
`ef_search` | No | 100 | No | The size of the dynamic list used during k-NN searches. Higher values result in more accurate but slower searches. Default is `256` for [binary indexes]({{site.url}}{{site.baseurl}}/vector-search/optimizing-storage/binary-quantization/).
`ef_construction` | No | 100 | No | The size of the dynamic list used during k-NN graph creation. Higher values result in a more accurate graph but slower indexing speed. Default is `256` for [binary indexes]({{site.url}}{{site.baseurl}}/vector-search/optimizing-storage/binary-quantization/).
`m` | No | 16 | No | The number of bidirectional links that the plugin creates for each new element. Increasing and decreasing this value can have a large impact on memory consumption. Keep this value between `2` and `100`.
`encoder` | No | flat | No | An encoder definition for encoding vectors. Encoders can reduce the memory footprint of your index at the expense of search accuracy.
An index created in OpenSearch version 2.11 or earlier will still use the previous `ef_construction` value (`512`).
{: .note}
#### IVF parameters
The IVF method supports the following parameters.
Parameter name | Required | Default | Updatable after index creation | Description
:--- | :--- | :--- | :--- | :---
`nlist` | No | 4 | No | The number of buckets into which to partition vectors. Higher values may increase accuracy but also increase memory and training latency.
`nprobes` | No | 1 | No | The number of buckets to search during a query. Higher values result in more accurate but slower searches.
`encoder` | No | flat | No | An encoder definition for encoding vectors.
For more information about these parameters, see the [Faiss documentation](https://github.com/facebookresearch/faiss/wiki/Faiss-indexes).
### IVF training requirements
The IVF algorithm requires a training step. To create an index that uses IVF, you need to train a model with the [Train API]({{site.url}}{{site.baseurl}}/vector-search/api/knn#train-a-model), passing the IVF method definition. IVF requires, at a minimum, that there be `nlist` training data points, but we recommend [that you use more than this](https://github.com/facebookresearch/faiss/wiki/Guidelines-to-choose-an-index#how-big-is-the-dataset). Training data can be the same as the data you plan to index or come from a separate dataset.
If the method uses the `pq` encoder, the training data must also contain at least 2`code_size` vectors, for example, 256 vectors for a `code_size` of `8`. If you don't specify `code_size`, the training data must contain more than 1,000 vectors. The Train API rejects requests that don't meet the `pq` minimum. If there are fewer training vectors than `nlist`, training starts but the model enters the `failed` state.
### Supported encoders
You can use encoders to reduce the memory footprint of a vector index at the expense of search accuracy.
OpenSearch supports the following encoders in the Faiss library.
Encoder name | Requires training | Description
:--- | :--- | :---
`flat` (Default) | No | Encode vectors as floating-point arrays. This encoding does not reduce memory footprint.
[`pq`](#pq-parameters) | Yes | An abbreviation for _product quantization_, PQ is a lossy compression technique that uses clustering to encode a vector into a fixed byte size, with the goal of minimizing the drop in k-NN search accuracy. At a high level, vectors are separated into `m` subvectors, and then each subvector is represented by a `code_size` code obtained from a code book produced during training. For more information about product quantization, see [this blog post](https://medium.com/dotstar/understanding-faiss-part-2-79d90b1e5388).
[`sq`](#sq-parameters) | No | An abbreviation for _scalar quantization_. Use the `sq` encoder to quantize 32-bit floating-point vectors into lower-bit representations. For 16-bit quantization, the `fp16` type quantizes vectors into 16-bit floats using the SQFP16 Faiss encoder, and the `bf16` type quantizes them into bfloat16 values. The encoder reduces the memory footprint with a minimal loss of precision and improves performance by using SIMD optimization (using AVX2 on x86 architecture or Neon on ARM64 architecture). For more information, see [Faiss scalar quantization]({{site.url}}{{site.baseurl}}/vector-search/optimizing-storage/faiss-scalar-quantization/).
#### PQ parameters
The `pq` encoder supports the following parameters.
Parameter name | Required | Default | Updatable after index creation | Description
:--- | :--- | :--- | :--- | :---
`m` | No | `1` | No | Determines the number of subvectors into which to separate the vector. Subvectors are encoded independently of each other. This vector dimension must be divisible by `m`. Maximum value is 1,024.
`code_size` | No | `8` | No | Determines the number of bits into which to encode a subvector. Maximum value is `8`. For `ivf`, this value must be less than or equal to `8`. For `hnsw`, this value must be `8`.
The `hnsw` method supports the `pq` encoder for OpenSearch version 2.10 and later. The `code_size` parameter of a `pq` encoder with the `hnsw` method must be **8**.
{: .important}
#### SQ parameters
The `sq` encoder supports the following parameters.
Parameter name | Required | Default | Updatable after index creation | Description
:--- |:---------|:--------| :--- | :---
`type` | No | `fp16` | No | The type of scalar quantization to be used to encode 32-bit floating-point vectors into the corresponding type. Valid values are `fp16` and `bf16`. For the `fp16` encoder, vector values must be in the [-65504.0, 65504.0] range. The `bf16` encoder accepts any finite 32-bit floating-point value. Supported only for 16-bit quantization.
`clip` | No | `false` | No | For the `fp16` encoder type, if `true`, vector values outside of the supported range are rounded so that they are within the range. If `false`, the request is rejected if any vector values are outside of the supported range. Setting `clip` to `true` may decrease recall. For `bf16`, setting `clip` to `true` is rejected; setting it to `false` has no effect. Supported only for 16-bit quantization.
`bits` | Yes | None | No | The number of bits used to quantize each 32-bit floating-point vector dimension. Valid values are `1`, `2`, `4`, and `16`.
For more information and examples, see [Using Faiss scalar quantization]({{site.url}}{{site.baseurl}}/vector-search/optimizing-storage/faiss-scalar-quantization/).
### SIMD optimization
OpenSearch supports [Single Instruction Multiple Data (SIMD)](https://en.wikipedia.org/wiki/Single_instruction,_multiple_data) processing if the underlying hardware supports SIMD instructions. SIMD is supported by default on Linux machines only for the Faiss engine. SIMD architecture helps boost overall performance by improving indexing throughput and reducing search latency. OpenSearch uses the following instruction sets:
- Neon on ARM64 architecture.
- AVX2 and AVX-512 on x64 architecture.
- Advanced AVX-512 instructions on Intel Sapphire Rapids or newer-generation processors, which improve the performance of Hamming distance computation. On these processors, OpenSearch also uses AVX-512 BF16 instructions to accelerate inner product computation for vectors quantized using the [`bf16` encoder type]({{site.url}}{{site.baseurl}}/vector-search/optimizing-storage/faiss-scalar-quantization/#the-bf16-encoder) and AVX-512 FP16 instructions to accelerate cosine similarity computation for vectors quantized using the [`fp16` encoder type]({{site.url}}{{site.baseurl}}/vector-search/optimizing-storage/faiss-scalar-quantization/#the-fp16-encoder).
SIMD optimization is applicable only if the vector dimension is a multiple of 8.
{: .note}
#### x64 architecture
For x64 architecture, the following versions of the Faiss library are built and shipped with the artifact:
- `libopensearchknn_faiss_avx512_spr.so`: The Faiss library containing advanced AVX-512 SIMD instructions for newer-generation processors, available on public clouds such as AWS for c/m/r 7i or newer instances.
- `libopensearchknn_faiss_avx512.so`: The Faiss library containing AVX-512 SIMD instructions.
- `libopensearchknn_faiss_avx2.so`: The Faiss library containing AVX2 SIMD instructions.
- `libopensearchknn_faiss.so`: The non-optimized Faiss library without SIMD instructions.
When using the Faiss library, the performance ranking is as follows: advanced AVX-512 > AVX-512 > AVX2 > no optimization.
{: .note }
If your hardware supports advanced AVX-512(spr), OpenSearch loads the `libopensearchknn_faiss_avx512_spr.so` library at runtime.
If your hardware supports AVX-512, OpenSearch loads the `libopensearchknn_faiss_avx512.so` library at runtime.
If your hardware supports AVX2 but doesn't support AVX-512, OpenSearch loads the `libopensearchknn_faiss_avx2.so` library at runtime.
To disable the advanced AVX-512 (for Sapphire Rapids or newer-generation processors), AVX-512, and AVX2 SIMD instructions and load the non-optimized Faiss library (`libopensearchknn_faiss.so`), specify the `knn.faiss.avx512_spr.disabled`, `knn.faiss.avx512.disabled`, and `knn.faiss.avx2.disabled` static settings as `true` in `opensearch.yml` (by default, all of these are set to `false`).
Note that to update a static setting, you must stop the cluster, change the setting, and restart the cluster. For more information, see [Static settings]({{site.url}}{{site.baseurl}}/install-and-configure/configuring-opensearch/index/#static-settings).
#### ARM64 architecture
For the ARM64 architecture, only one performance-boosting Faiss library (`libopensearchknn_faiss.so`) is built and shipped. The library contains Neon SIMD instructions and cannot be disabled.
### Example configurations
The following example uses the `ivf` method without specifying an encoder (by default, OpenSearch uses the `flat` encoder):
```json
"method": {
"name":"ivf",
"engine":"faiss",
"parameters":{
"nlist": 4,
"nprobes": 2
}
}
```
The following example uses the `ivf` method with a `pq` encoder:
```json
"method": {
"name":"ivf",
"engine":"faiss",
"parameters":{
"encoder":{
"name":"pq",
"parameters":{
"code_size": 8,
"m": 8
}
}
}
}
```
The following example uses the `hnsw` method without specifying an encoder (by default, OpenSearch uses the `flat` encoder):
```json
"method": {
"name":"hnsw",
"engine":"faiss",
"parameters":{
"ef_construction": 256,
"m": 8
}
}
```
The following example demonstrates configuring the `ivf` method with an `sq` encoder using 16-bit quantization and clipping disabled:
```json
"method": {
"name":"ivf",
"engine":"faiss",
"parameters":{
"encoder": {
"name": "sq",
"parameters": {
"bits": 16,
"clip": false
}
},
"nprobes": 2
}
}
```
The following example demonstrates configuring the `hnsw` method with an `sq` encoder using 1-bit quantization:
```json
"method": {
"name":"hnsw",
"engine":"faiss",
"parameters":{
"encoder": {
"name": "sq",
"parameters": {
"bits": 1
}
},
"ef_construction": 512,
"m": 16
}
}
```
The following example demonstrates configuring the `hnsw` method with an `sq` encoder using 16-bit quantization and clipping enabled:
```json
"method": {
"name":"hnsw",
"engine":"faiss",
"parameters":{
"encoder": {
"name": "sq",
"parameters": {
"bits": 16,
"clip": true
}
},
"ef_construction": 256,
"m": 8
}
}
```
## NMSLIB engine (deprecated)
The Non-Metric Space Library (NMSLIB) engine was one of the first vector search implementations in OpenSearch. While still supported, it has been deprecated in favor of the Faiss and Lucene engines.
### Supported methods
The NMSLIB engine supports the following method.
Method name | Requires training | Supported spaces
:--- | :--- | :---
[`hnsw`](#hnsw-parameters-2) | No | `l2`, `innerproduct`, `cosinesimil`, `l1`, `linf`
#### HNSW parameters
The HNSW method supports the following parameters.
Parameter name | Required | Default | Updatable after index creation | Description
:--- | :--- | :--- | :--- | :---
`ef_construction` | No | 100 | No | The size of the dynamic list used during k-NN graph creation. Higher values result in a more accurate graph but slower indexing speed.
`m` | No | 16 | No | The number of bidirectional links created for each new element. Impacts memory consumption significantly. Keep between `2` and `100`.
For NMSLIB (deprecated), *ef_search* is set in the [index settings]({{site.url}}{{site.baseurl}}/vector-search/settings/#index-settings).
{: .note}
An index created in OpenSearch version 2.11 or earlier will still use the previous `ef_construction` value (`512`).
{: .note}
### Example configuration
```json
"method": {
"name": "hnsw",
"engine": "nmslib",
"space_type": "l2",
"parameters": {
"ef_construction": 100,
"m": 16
}
}
```
## JVector engine
The `jvector` engine implements DiskANN-style approximate nearest neighbor search in pure Java, with no Java Native Interface (JNI) dependency. The engine isn't included in any OpenSearch distribution; it's provided by the `opensearch-jvector` plugin, which can't be installed in the same cluster as `opensearch-knn`. For installation instructions, see [OpenSearch JVector plugin]({{site.url}}{{site.baseurl}}/install-and-configure/additional-plugins/opensearch-jvector/#installation).
### Supported methods
The `jvector` engine supports the following method.
Method name | Requires training | Supported spaces
:--- | :--- | :---
[`disk_ann`](#diskann-parameters) | No | `l2`, `cosinesimil`, `innerproduct`
#### DiskANN parameters
The `disk_ann` method supports the following parameters.
Parameter name | Required | Default | Updatable after index creation | Description
:--- | :--- | :--- | :--- | :---
`m` | No | `16` | No | The number of bidirectional links per node. Higher values improve recall but increase index size.
`ef_construction` | No | `100` | No | The size of the candidate list used during graph construction. Higher values improve recall but slow ingestion.
`advanced.alpha` | No | `1.2` | No | The diversity factor for neighbor selection.
`advanced.neighbor_overflow` | No | `1.2` | No | The overflow factor for neighbor lists.
`advanced.hierarchy_enabled` | No | `false` | No | Whether to enable a hierarchical graph structure.
`advanced.num_pq_subspaces` | No | Based on the vector dimension (see [the following table](#num-pq-subspaces)) | No | The number of PQ subspaces. Must not exceed the number of vector dimensions and works best as a divisor of the dimension. Higher values reduce compression but improve recall.
`advanced.min_batch_size_for_quantization` | No | `1024` | No | The number of documents required before quantization is trained.
`advanced.leading_segment_merge_disabled` | No | `false` | Yes | Whether to prevent the leading segment from being rebuilt during a force merge.
The following table lists the default values of `advanced.num_pq_subspaces` for each vector dimension. Vector dimension | Default number of PQ subspaces :--- | :--- `384` | `96` `768` | `192` `1536` | `192` `3072` | `384` ### Space types The `space_type` parameter determines the distance metric used to compare vectors. The following table lists the space types that the `jvector` engine supports. For more information, see [Spaces]({{site.url}}{{site.baseurl}}/mappings/supported-field-types/knn-spaces/). Space type | Distance metric | Use case :--- | :--- | :--- `l2` (default) | Euclidean distance (L2 norm) | General-purpose search on raw coordinates `cosinesimil` | Cosine similarity | Text embeddings, for which direction matters more than magnitude `innerproduct` | Dot product (inner product) | Embeddings for which magnitude carries meaning, such as those produced by bi-encoder models ### Example configuration ```json "method": { "name": "disk_ann", "engine": "jvector", "space_type": "l2", "parameters": { "m": 16, "ef_construction": 100 } } ``` ## Choosing the right method and engine There are several options to choose from when building your `knn_vector` field. To select the correct method and parameters, you should first understand the requirements of your workload and what trade-offs you are willing to make. Factors to consider are (1) query latency, (2) query quality, (3) memory limits, and (4) indexing latency. If memory is not a concern, HNSW offers a strong query latency/query quality trade-off. If you want to use less memory and increase indexing speed as compared to HNSW while maintaining similar query quality, you should evaluate IVF. If memory is a concern, consider adding a PQ encoder to your HNSW or IVF index. Because PQ is a lossy encoding, query quality will drop. If your dataset grows continuously or exceeds available memory, consider the [JVector engine](#jvector-engine), which is provided by the [`opensearch-jvector` plugin]({{site.url}}{{site.baseurl}}/install-and-configure/additional-plugins/opensearch-jvector/). It supports concurrent inserts, incremental merges without full graph rebuilds, and native PQ with better recall at higher compression ratios. You can reduce the memory footprint by a factor of 2, with a minimal loss in search quality, by using the [`fp_16` encoder]({{site.url}}{{site.baseurl}}/vector-search/optimizing-storage/faiss-scalar-quantization/#16-bit-quantization). If your vector dimensions are within the [-128, 127] byte range, we recommend using the [byte quantizer]({{site.url}}{{site.baseurl}}/mappings/supported-field-types/knn-memory-optimized/#byte-vectors) to reduce the memory footprint by a factor of 4. To learn more about vector quantization options, see [k-NN vector quantization]({{site.url}}{{site.baseurl}}/vector-search/optimizing-storage/knn-vector-quantization/). ## Engine recommendations In general, select Faiss for large-scale use cases. Lucene is a good option for smaller deployments and offers benefits like smart filtering, where the optimal filtering strategy—pre-filtering, post-filtering, or exact k-NN—is automatically applied depending on the situation. Consider JVector for high-update workloads or memory-constrained large-scale deployments. The following table summarizes the differences between each option. | | Faiss/HNSW | Faiss/IVF | Lucene/HNSW | JVector/DiskANN | |:---|:---|:---|:---|:---| | Max dimensions | 16,000 | 16,000 | 16,000 | 16,000 | | Filter | Post-filter | Post-filter | Filter during search | Filter during search