Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,13 @@

## Ensure data nodes have enough memory [_ensure_data_nodes_have_enough_memory]

{{es}} uses the [HNSW](https://arxiv.org/abs/1603.09320) algorithm for approximate kNN search. HNSW is a graph-based algorithm which only works efficiently when most vector data is held in memory. You should ensure that data nodes have at least enough RAM to hold the vector data and index structures. To check the size of the vector data, you can use the [Analyze index disk usage](https://www.elastic.co/docs/api/doc/elasticsearch/operation/operation-indices-disk-usage) API.
{{es}} uses either the [HNSW](https://arxiv.org/abs/1603.09320) algorithm or the [DiskBBQ](https://www.elastic.co/search-labs/blog/diskbbq-elasticsearch-introduction) algorithm for approximate kNN search.

Check notice on line 49 in deploy-manage/production-guidance/optimize-performance/approximate-knn-search.md

View workflow job for this annotation

GitHub Actions / vale

Elastic.Acronyms: 'HNSW' has no definition.

HNSW is a graph-based algorithm which only works efficiently when most vector data is held in memory. You should ensure that data nodes have at least enough RAM to hold the vector data and index structures.

Check notice on line 51 in deploy-manage/production-guidance/optimize-performance/approximate-knn-search.md

View workflow job for this annotation

GitHub Actions / vale

Elastic.Acronyms: 'HNSW' has no definition.

DiskBBQ is a clustering algorithm which can scale effeciently on a fraction of the total memory. You can start with enough RAM to hold the vector data and index structures but it's likely you will be able to use signifigantly less than this and still maintain good performance. In testing we find this will be between 1-5% of the index structure size (centroids and quantized vector data) per unique query where unique queries access non-overlapping clusters.

Check notice on line 53 in deploy-manage/production-guidance/optimize-performance/approximate-knn-search.md

View workflow job for this annotation

GitHub Actions / vale

Elastic.FutureTense: 'will be' might be in future tense. Write in the present tense to describe the state of the product as it is now.

Check notice on line 53 in deploy-manage/production-guidance/optimize-performance/approximate-knn-search.md

View workflow job for this annotation

GitHub Actions / vale

Elastic.FutureTense: 'will be' might be in future tense. Write in the present tense to describe the state of the product as it is now.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's be careful here, obviously, going to disk is always slower than just reading things in memory. We need to clarify that the performance degrades more linearly than with HNSW, which degrades exponentially.

I think calling about these percentages is OK.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense I'll reword to clarify

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reworded this a bit let me know what you think now


To check the size of the vector data, you can use the [Analyze index disk usage](https://www.elastic.co/docs/api/doc/elasticsearch/operation/operation-indices-disk-usage) API.

Here are estimates for different element types and quantization levels:

Expand All @@ -59,6 +65,8 @@

If utilizing HNSW, the graph must also be in memory, to estimate the required bytes use `num_vectors * 4 * HNSW.m`. The default value for `HNSW.m` is 16, so by default `num_vectors * 4 * 16`.

If utilizing DiskBBQ, a fraction of the clusters and centroids will need to be in memory. When doing this estimation it makes more sense to include both the index structure and the quantized vectors together as the structures are dependent. To estimate the total bytes we compute the cost of the centroids as `num_clusters * num_dimensions * 2 * 4 + num_clusters * (num_dimensions + 14)` plus the cost of the quantized vectors within the clusters as `num_vectors * ((num_dimensions/8 + 14 + 2) * 2)` where `num_clusters` is defined as `num_vectors / vectors_per_cluster` which by default will be `num_vectors / 384`

Check notice on line 68 in deploy-manage/production-guidance/optimize-performance/approximate-knn-search.md

View workflow job for this annotation

GitHub Actions / vale

Elastic.FutureTense: 'will be' might be in future tense. Write in the present tense to describe the state of the product as it is now.

Check notice on line 68 in deploy-manage/production-guidance/optimize-performance/approximate-knn-search.md

View workflow job for this annotation

GitHub Actions / vale

Elastic.FutureTense: 'will need' might be in future tense. Write in the present tense to describe the state of the product as it is now.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems pretty complicated...

num_clusters * num_dimensions * 2 * 4 you need two floating point representations of the clusters?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That 2 is definitely not right. Removing that.

Went back and double checked what bytes are there.

posting list w/o vectors = num_clusters * (centroid_bytes + dotProd(centroid,centroid) + clusterSize + encoding)
posting list w/o vectors = num_clusters * ((dims * 4) + 4 + 4 + 1)

I dont think it's worth it to describe the additional bytes. That would just make this even more complicated.

Suggestions for simplifying it are welcome. It probably seems more complex because it includes both the "index structure" for the centroids and the vectors because of SOAR. Maybe I can reference the bbq quantized calculation from above and somewhat simplify the centroids figures. So something like this instead. Thoughts?

the cost of the centroids as `num_clusters * (num_dimensions * 4  + (num_dimensions + 14))` 
plus the cost of the quantized vectors within the clusters as `bbq_quantizated_vectors * 2` 

The downside is this ignores the doc id (2 byte) cost per vector. I almost feel like it makes it more complicated to mention this rather than not if we break it up for instance like this:

plus the cost of the quantized vectors within the clusters as `bbq_quantizated_vectors * 2` + `num_vectors * 2 * 2` 

I'll think about it some more.


Note that the required RAM is for the filesystem cache, which is separate from the Java heap.

The data nodes should also leave a buffer for other ways that RAM is needed. For example your index might also include text fields and numerics, which also benefit from using filesystem cache. It’s recommended to run benchmarks with your specific dataset to ensure there’s a sufficient amount of memory to give good search performance. You can find [here](https://elasticsearch-benchmarks.elastic.co/#tracks/so_vector) and [here](https://elasticsearch-benchmarks.elastic.co/#tracks/dense_vector) some examples of datasets and configurations that we use for our nightly benchmarks.
Expand All @@ -72,16 +80,53 @@
Loading data into the filesystem cache eagerly on too many indices or too many files will make search *slower* if the filesystem cache is not large enough to hold all the data. Use with caution.
::::


The following file extensions are used for the approximate kNN search: Each extension is broken down by the quantization types.

* {applies_to}`stack: ga 9.3` `cenivf` for DiskBBQ to store centroids
* {applies_to}`stack: ga 9.3` `clivf` for DiskBBQ to store clusters of quantized vectors
* `vex` for the HNSW graph
* `vec` for all non-quantized vector values. This includes all element types: `float`, `byte`, and `bit`.
* `veq` for quantized vectors indexed with [`quantization`](elasticsearch://reference/elasticsearch/mapping-reference/dense-vector.md#dense-vector-quantization): `int4` or `int8`
* `veb` for binary vectors indexed with [`quantization`](elasticsearch://reference/elasticsearch/mapping-reference/dense-vector.md#dense-vector-quantization): `bbq`
* `vem`, `vemf`, `vemq`, and `vemb` for metadata, usually small and not a concern for preloading

Generally, if you are using a quantized index, you should only preload the relevant quantized values and the HNSW graph. Preloading the raw vectors is not necessary and might be counterproductive.
Generally, if you are using a quantized index, you should only preload the relevant quantized values and index structures such as the HNSW graph. Preloading the raw vectors is not necessary and might be counterproductive.

Check notice on line 93 in deploy-manage/production-guidance/optimize-performance/approximate-knn-search.md

View workflow job for this annotation

GitHub Actions / vale

Elastic.Acronyms: 'HNSW' has no definition.

Additional detail can be gleened on the specific files by using the [stats endpoint](https://www.elastic.co/docs/api/doc/elasticsearch/operation/operation-indices-stats) which will display information about the index and fields for example for DiskBBQ you might see something like this:

Check notice on line 95 in deploy-manage/production-guidance/optimize-performance/approximate-knn-search.md

View workflow job for this annotation

GitHub Actions / vale

Elastic.WordChoice: Consider using 'refer to (if it's a document), view (if it's a UI element)' instead of 'see', unless the term is in the UI.

Check notice on line 95 in deploy-manage/production-guidance/optimize-performance/approximate-knn-search.md

View workflow job for this annotation

GitHub Actions / vale

Elastic.FutureTense: 'will display' might be in future tense. Write in the present tense to describe the state of the product as it is now.

[source,console]

Check notice on line 97 in deploy-manage/production-guidance/optimize-performance/approximate-knn-search.md

View workflow job for this annotation

GitHub Actions / vale

Elastic.Capitalization: '[source,console]' should use sentence-style capitalization.
----
GET my_index/_stats?filter_path=indices.my_index.primaries.dense_vector

Example Response:
{
"indices": {
"my_index": {
"primaries": {
"dense_vector": {
"value_count": 3,
"off_heap": {
"total_size_bytes": 249,
"total_veb_size_bytes": 0,
"total_vec_size_bytes": 36,
"total_veq_size_bytes": 0,
"total_vex_size_bytes": 0,
"total_cenivf_size_bytes": 111,
"total_clivf_size_bytes": 102,
"fielddata": {
"my_vector": {
"cenivf_size_bytes": 111,
"clivf_size_bytes": 102,
"vec_size_bytes": 36
}
}
}
}
}
}
}
}
----


## Reduce the number of index segments [_reduce_the_number_of_index_segments]
Expand Down
11 changes: 8 additions & 3 deletions solutions/search/vector/knn.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Approximate kNN offers low latency and good accuracy, while exact kNN guarantees
## Approximate kNN search [approximate-knn]

::::{warning}
Approximate kNN search has specific resource requirements. All vector data must fit in the node’s page cache for efficient performance. Refer to the [approximate kNN tuning guide](/deploy-manage/production-guidance/optimize-performance/approximate-knn-search.md) for configuration tips.
Approximate kNN search has specific resource requirements. For instance, for HNSW all vector data must fit in the node’s page cache for efficient performance. Refer to the [approximate kNN tuning guide](/deploy-manage/production-guidance/optimize-performance/approximate-knn-search.md) for configuration tips.
::::

To run an approximate kNN search:
Expand Down Expand Up @@ -132,9 +132,10 @@ Support for approximate kNN search was added in version 8.0. Before 8.0, `dense_

### Indexing considerations for approximate kNN search [knn-indexing-considerations]

For approximate kNN, {{es}} stores dense vector values per segment as an [HNSW graph](https://arxiv.org/abs/1603.09320). Building HNSW graphs is compute-intensive, so indexing vectors can take time; you may need to increase client request timeouts for index and bulk operations. The [approximate kNN tuning guide](/deploy-manage/production-guidance/optimize-performance/approximate-knn-search.md) covers indexing performance, sizing, and configuration trade-offs that affect search performance.

In addition to search-time parameters, HNSW exposes index-time settings that balance graph build cost, search speed, and accuracy. When defining your `dense_vector` mapping, use [`index_options`](elasticsearch://reference/elasticsearch/mapping-reference/dense-vector.md#dense-vector-index-options) to set these parameters:
For approximate kNN, {{es}} stores dense vector values per segment as an [HNSW graph](https://arxiv.org/abs/1603.09320) or per segment as clusters using [DiskBBQ](https://www.elastic.co/search-labs/blog/diskbbq-elasticsearch-introduction). Building these approximate kNN structures is compute-intensive, so indexing vectors can take time; you may need to increase client request timeouts for index and bulk operations. The [approximate kNN tuning guide](/deploy-manage/production-guidance/optimize-performance/approximate-knn-search.md) covers indexing performance, sizing, and configuration trade-offs that affect search performance.

In addition to search-time parameters, HNSW and DiskBBQ expose index-time settings that balance graph build cost, search speed, and accuracy. When defining your `dense_vector` mapping, use [`index_options`](elasticsearch://reference/elasticsearch/mapping-reference/dense-vector.md#dense-vector-index-options) to set these parameters:

```console
PUT image-index
Expand All @@ -156,6 +157,10 @@ PUT image-index
}
```

::::{note}
Support for DisKBBQ was introduced in version 9.2.0
::::

### Tune approximate kNN for speed or accuracy [tune-approximate-knn-for-speed-accuracy]

To gather results, the kNN API first finds a `num_candidates` number of approximate neighbors per shard, computes similarity to the query vector, selects the top `k` per shard, and merges them into the global top `k` nearest neighbors.
Expand Down
Loading