Vector search has become an important part of modern applications that use semantic search, recommendation systems, image retrieval, and retrieval-augmented generation (RAG). Instead of matching only exact words, vector search compares numerical representations of data called embeddings. These embeddings help applications find documents, products, images, or other content based on meaning and similarity.
However, vector search introduces a storage and performance challenge. Embedding models commonly generate vectors containing hundreds or thousands of floating-point values. When an Elasticsearch cluster stores millions of these vectors, memory consumption can become significant. Searching large collections of full-precision vectors can also increase infrastructure costs and affect query latency.
Elasticsearch addresses this challenge through vector quantization. Quantization reduces the precision of vector values so that Elasticsearch can store and search more vectors using less memory. Techniques such as int8, int4, and Better Binary Quantization (BBQ) provide different trade-offs between memory usage, search speed, and relevance.
This article explains how vector quantization works in Elasticsearch, how to configure quantized vector fields, how to improve search accuracy through oversampling and rescoring, and how to choose a suitable strategy for production applications.
Understanding Vector Search in Elasticsearch
A vector is an array of numerical values that represents information in a mathematical form. An embedding model converts text, images, or other data into vectors so that similar content can be compared using mathematical similarity calculations.
For example, consider these three search queries:
- How can I secure an ASP.NET Core API?
- Best practices for protecting a .NET Web API.
- How do I prepare a chocolate cake?
The first two queries discuss similar subjects, even though they use different words. An embedding model can generate vectors that place these queries relatively close together in vector space. The third query produces a vector that is likely to be farther away.
Elasticsearch stores these embeddings using the dense_vector field type.
A basic index mapping looks like this:
PUT products
{
"mappings": {
"properties": {
"productName": {
"type": "text"
},
"description": {
"type": "text"
},
"embedding": {
"type": "dense_vector",
"dims": 384,
"index": true,
"similarity": "cosine"
}
}
}
}
In this example, the embedding field stores a vector containing 384 dimensions. The index setting enables vector indexing, while cosine similarity determines how Elasticsearch compares vectors.
The number of dimensions must match the output size of the embedding model. If the model generates 768-dimensional embeddings, the mapping must use 768 dimensions.
Applications can generate embeddings outside Elasticsearch and send them with documents. They can also use supported Elasticsearch inference capabilities to generate embeddings as part of an indexing or search workflow.
Why Vector Quantization Is Important
Traditional embeddings often use 32-bit floating-point values. Each dimension therefore requires approximately four bytes of storage before accounting for indexing structures and other overhead.
Consider an embedding containing 768 dimensions:
768 dimensions × 4 bytes = 3,072 bytes
One vector requires approximately 3 KB for its raw float values.
For one million vectors:
3,072 bytes × 1,000,000
= 3,072,000,000 bytes
This is approximately 3.07 GB of raw vector data. Actual infrastructure requirements can be higher because Elasticsearch also maintains indexing structures, document metadata, and other data.
The situation becomes more challenging when an application stores tens or hundreds of millions of embeddings.
Vector quantization reduces the number of bits used to represent each dimension. Instead of retaining every original floating-point value in the search representation, Elasticsearch stores a compressed approximation.
This provides several benefits:
- Lower memory consumption for vector search.
- More vectors available within the same memory capacity.
- Reduced infrastructure requirements for large collections.
- Faster similarity calculations in supported search configurations.
- Better scalability for semantic search and RAG applications.
Quantization is a lossy compression technique. The compressed representation is an approximation of the original vector, so search accuracy can change. The goal is to reduce resource consumption while keeping the search results sufficiently relevant.
Vector Quantization Options in Elasticsearch
Elasticsearch supports multiple quantization strategies for dense vector indexing. The main options include int8, int4, and Better Binary Quantization.
Int8 quantization
Int8 quantization represents each vector dimension using an 8-bit integer instead of a 32-bit floating-point value.
This reduces the vector representation from four bytes per dimension to one byte per dimension. It provides approximately four times the memory reduction for the quantized representation.
For a 768-dimensional vector:
Original float32 representation:
768 × 4 bytes = 3,072 bytes
Int8 representation:
768 × 1 byte = 768 bytes
Int8 generally provides a relatively conservative compression strategy. It is useful when an application needs to reduce memory consumption while retaining strong search quality.
Elasticsearch uses the int8_hnsw index type for HNSW-based vector search with int8 quantization.
Int4 quantization
Int4 quantization uses four bits per dimension. Two dimensions can be represented using one byte.
This provides approximately eight times the memory reduction compared with a 32-bit floating-point representation.
For a 768-dimensional vector:
Original float32 representation:
3,072 bytes
Int4 representation:
768 × 0.5 bytes = 384 bytes
Int4 provides stronger compression than int8, but it can introduce a greater loss of precision.
It is suitable for workloads where memory reduction is important and the application can tolerate some additional approximation in search results.
Int4 quantization requires an even number of vector dimensions.
Better Binary Quantization (BBQ)
Better Binary Quantization is an advanced compression technique designed for large-scale vector search.
BBQ represents each dimension using a single bit in its compressed representation. Compared with 32-bit floating-point values, this can reduce the vector representation by approximately 32 times, or 96%.
For a 768-dimensional vector, the approximate compressed representation is:
768 bits ÷ 8 = 96 bytes
Elasticsearch uses additional corrective information and optimized calculations to improve search quality with this compact representation.
BBQ is particularly useful for high-dimensional embeddings and large vector collections. It is designed to work with oversampling and rescoring so that the search engine can retrieve candidates efficiently and improve their final ranking using original vector values.
Elasticsearch provides multiple BBQ indexing options, including bbq_hnsw, bbq_flat, and bbq_disk.
Comparing Quantization Strategies
The following table summarizes the main differences.
Strategy | Approximate memory reduction | Main use case | Trade-off |
|---|
int8 | 4x | High search quality with moderate compression | Lower compression |
int4 | 8x | Balanced memory and accuracy requirements | Greater precision loss |
BBQ | 32x | Large-scale vector search | Requires careful accuracy tuning |
These reduction figures describe the quantized vector representation, not the total disk usage of an Elasticsearch index.
Elasticsearch can retain the original floating-point vectors on disk for rescoring, reindexing, and other operations. Consequently, the total disk reduction is different from the reduction in the in-memory quantized representation.
The correct strategy depends on vector dimensions, collection size, available memory, search latency requirements, and acceptable relevance loss.
Configuring Quantized Vector Search
Elasticsearch allows developers to configure quantization through the index mapping.
The following examples demonstrate how to create an index using different quantization strategies.
Creating an int8 vector index
The following mapping creates a dense vector field using int8 HNSW indexing:
PUT products-int8
{
"mappings": {
"properties": {
"embedding": {
"type": "dense_vector",
"dims": 384,
"index": true,
"similarity": "cosine",
"index_options": {
"type": "int8_hnsw"
}
}
}
}
}
The int8_hnsw option enables HNSW-based approximate nearest-neighbor search with int8 quantization.
Creating an int4 vector index
For stronger compression, use int4_hnsw:
PUT products-int4
{
"mappings": {
"properties": {
"embedding": {
"type": "dense_vector",
"dims": 384,
"index": true,
"similarity": "cosine",
"index_options": {
"type": "int4_hnsw"
}
}
}
}
}
This configuration reduces the memory footprint further than int8, but developers should measure the impact on search relevance.
Creating a BBQ vector index
For high-dimensional embeddings, BBQ can provide substantial memory savings:
PUT products-bbq
{
"mappings": {
"properties": {
"embedding": {
"type": "dense_vector",
"dims": 384,
"index": true,
"similarity": "cosine",
"index_options": {
"type": "bbq_hnsw"
}
}
}
}
}
BBQ supports vector dimensions greater than 64. The 384-dimensional example is suitable for a common class of text embedding models.
The best BBQ configuration depends on the collection size and search requirements. For large datasets, HNSW provides an approximate nearest-neighbor structure that avoids comparing the query vector against every document.
Using BBQ flat search
Not every vector search workload requires an HNSW graph. For smaller collections or workloads where filters reduce the number of candidate documents, a flat search approach may be appropriate.
PUT products-bbq-flat
{
"mappings": {
"properties": {
"embedding": {
"type": "dense_vector",
"dims": 384,
"index": true,
"similarity": "cosine",
"index_options": {
"type": "bbq_flat"
}
}
}
}
}
BBQ flat uses compressed vectors without the HNSW graph structure. It can be useful when the number of vectors being compared is relatively small. For larger collections, benchmark it against HNSW and other available options.
Performing Vector Search With Quantized Embeddings
After creating the index and storing embeddings, an application can execute a kNN query.
The following example searches for the nearest products using a query embedding:
POST products-bbq/_search
{
"knn": {
"field": "embedding",
"query_vector": [
0.12,
0.34,
0.56,
0.78
],
"k": 10,
"num_candidates": 100
}
}
The vector in this example contains only four values to keep the example readable. A real query vector must contain exactly the number of dimensions defined in the index mapping.
The important parameters are:
- field: Identifies the dense vector field.
- query_vector: Contains the embedding generated for the search input.
- k: Specifies the number of nearest-neighbor results to return.
- num_candidates: Controls the number of candidates considered per shard during approximate nearest-neighbor search.
A larger num_candidates value can improve recall because Elasticsearch considers more candidates. However, it can also increase query processing work and latency.
Tune this value using actual application queries rather than selecting it based only on a theoretical recommendation.
Improving Search Accuracy With Oversampling and Rescoring
Quantization reduces memory requirements, but it can also change the ranking of similar vectors. A document that ranks highly using the compressed representation might not rank as highly when compared using the original floating-point values.
Elasticsearch addresses this challenge through oversampling and rescoring.
What is oversampling?
Oversampling retrieves more candidates than the number of results the application ultimately needs.
For example, an application requests the top 10 results. Instead of initially retrieving only 10 candidates, the search process gathers a larger candidate set.
This gives Elasticsearch additional documents to evaluate during the next stage.
What is rescoring?
Rescoring evaluates selected candidates again using the original, full-precision vector values.
The process works as follows:
- Elasticsearch searches the quantized vector index.
- It retrieves a larger set of candidate documents.
- It accesses the original vector values for selected candidates.
- It recalculates their similarity scores using the original vectors.
- It returns the final top-ranked documents.
This approach combines the memory efficiency of quantized search with the ranking accuracy of full-precision vectors.
A query can configure rescoring through rescore_vector:
POST products-bbq/_search
{
"knn": {
"field": "embedding",
"query_vector": [
0.12,
0.34,
0.56,
0.78
],
"k": 10,
"num_candidates": 100,
"rescore_vector": {
"oversample": 3.0
}
}
}
The example uses an oversampling factor of 3.0. The actual number of candidates used for rescoring depends on the requested result count and Elasticsearch's search execution.
BBQ HNSW configurations already apply a default mapping-level oversampling factor in supported configurations. Developers can override it when a workload requires different behavior.
Higher oversampling can improve recall, but it also increases the amount of rescoring work. The right value must be measured against real search queries and datasets.
Choosing Between HNSW, BBQ Flat, and BBQ Disk
Quantization and vector indexing are related but separate decisions. Developers must also select an appropriate search algorithm.
HNSW
Hierarchical Navigable Small World is a graph-based approximate nearest-neighbor algorithm.
It creates connections between vectors so that Elasticsearch can navigate toward similar candidates without comparing every vector in the collection.
HNSW is suitable for workloads that require fast similarity search across large collections and have sufficient memory for the vector index.
The BBQ HNSW configuration combines this graph-based approach with binary quantization.
BBQ flat
BBQ flat uses compressed vectors without an HNSW graph. It can be useful for smaller collections or workloads where filtering significantly reduces the number of vectors that need to be compared.
It also avoids the additional graph construction and maintenance associated with HNSW.
BBQ disk
BBQ disk uses a disk-based approach for large vector collections where keeping the entire search working set in memory is expensive.
It can help applications operate with constrained RAM, although disk access introduces different performance characteristics.
BBQ disk availability depends on the Elasticsearch subscription and deployment configuration. Verify licensing and version requirements before selecting it.
Best Practices for Production Applications
Quantization should be introduced as part of a measured search optimization strategy. The following practices help maintain a balance between performance, relevance, and operational cost.
Benchmark with real embeddings
Do not select a quantization strategy using only synthetic vectors. Use embeddings generated by the actual model and representative application data.
Different embedding models produce different vector distributions. A configuration that performs well for one model may not provide the same recall for another.
Measure recall before and after quantization
Recall measures how many relevant results are retrieved compared with a reference result set.
Create a baseline using full-precision vector search. Then compare quantized search results against that baseline.
Measure recall at different values of k, such as 5, 10, and 20. This helps identify whether compression changes the results that matter to users.
Monitor latency and memory together
A quantized index may reduce memory usage without producing the expected improvement in end-to-end query latency.
Measure query latency, CPU utilization, memory consumption, disk activity, indexing throughput, and cluster health. Evaluate both average latency and tail latency, especially p95 and p99.
Tune oversampling carefully
Oversampling can recover search quality, but excessive rescoring can increase latency and resource usage.
Test multiple oversampling factors and compare the resulting recall and latency. Do not assume that a higher value always provides a worthwhile improvement.
Keep embedding dimensions consistent
The dimensions of stored vectors and query vectors must match the dense_vector mapping.
When changing embedding models, check whether the new model produces the same number of dimensions and whether its vector space is compatible with the existing data.
If the embedding model changes, generate new embeddings and plan an index migration rather than mixing incompatible vectors in the same search field.
Combine vector search with metadata filtering
Semantic similarity is not always enough. Real applications often need additional conditions such as category, tenant, availability, language, or publication status.
Combine vector search with appropriate metadata filters so that the returned results satisfy business requirements as well as semantic relevance.
Plan index migrations
Quantization settings are configured as part of vector indexing. Changing the indexing strategy may require creating a new index and reindexing documents.
Use versioned index names and aliases to migrate safely. Build the replacement index, validate search quality, and switch the application alias only after testing is complete.
Common Mistakes to Avoid
One common mistake is assuming that a 32x reduction in quantized vector memory means the entire Elasticsearch index will use 32 times less disk space. Elasticsearch can retain original vectors for rescoring and other operations, so disk usage must be evaluated separately.
Another mistake is using BBQ without checking search relevance. Binary quantization provides significant compression, but the resulting approximation can affect ranking. Benchmark recall and use rescoring when appropriate.
Developers also sometimes set num_candidates too low. This can prevent approximate search from discovering relevant documents, even when the vector representation is configured correctly.
A further mistake is comparing search results from different embedding models as though their vector spaces were interchangeable. Embeddings should be generated using compatible models and preprocessing configurations.
Finally, do not optimize only for memory. A production search system must balance recall, latency, throughput, indexing cost, storage, and operational complexity.
Advantages and Disadvantages of Quantized Vector Search
Quantized vector search provides several advantages:
- Reduces the memory footprint of vector representations.
- Makes large embedding collections more practical to operate.
- Can improve search efficiency through compressed similarity calculations.
- Supports multiple compression levels for different workloads.
- Works with oversampling and rescoring to improve ranking quality.
- Helps reduce infrastructure costs for large-scale semantic search.
However, it also introduces trade-offs:
- Compression can reduce search accuracy.
- More aggressive quantization requires careful recall testing.
- Rescoring introduces additional processing.
- Retaining original vectors increases disk usage.
- Different indexing strategies have different memory and latency requirements.
- Rebuilding an index may be necessary when changing the quantization strategy.
Conclusion
Quantized embeddings help Elasticsearch handle large vector collections without requiring the same memory resources as full-precision vector search. Int8, int4, and Better Binary Quantization provide different levels of compression, allowing developers to choose a strategy based on their application requirements.
For large-scale semantic search, BBQ HNSW is a useful starting point, while int8 can be appropriate when preserving search quality is a higher priority. BBQ flat and BBQ disk address different workload and infrastructure requirements.
The most important part of a production implementation is measurement. Compare quantized results with full-precision search, monitor recall and latency, tune oversampling, and validate the impact of metadata filters. With the right configuration, Elasticsearch quantization can make vector search more scalable while maintaining useful search relevance.
Join the conversation! Your thoughts help the community grow.