
Elasticsearch vector is a powerful tool for efficient search and retrieval. It allows you to store and search vector data, which is particularly useful for applications that require similarity-based search.
Vector data can be used to represent complex data structures, such as images, audio, or text, in a compact and efficient manner. This enables fast and accurate search and retrieval of similar data points.
Elasticsearch vector is built on top of the Elasticsearch engine, which provides a scalable and fault-tolerant infrastructure for storing and querying large volumes of data. This makes it an ideal choice for applications that require high-performance search and retrieval capabilities.
What is Elasticsearch Vector?
Elasticsearch Vector is a datatype that allows for intricate vector similarity searches, expanding Elasticsearch's utility beyond traditional search capabilities. This enhancement enables Elasticsearch to accommodate vectors with hundreds or thousands of dimensions.
Storing vectors as dense float arrays within documents allows for multiple vectors per document, fostering a versatile environment for diverse AI workloads. This flexibility is a significant improvement over traditional search capabilities.
Recent benchmarks show substantial improvements in search latency and throughput when comparing vector databases to Elasticsearch. For instance, Pinecone demonstrates a remarkable 99th percentile latency reduction from 1600ms in Elasticsearch to just 7ms.
Background on Elasticsearch
Elasticsearch has a long history of supporting vectors, starting with the 7.0 release, where vectors were stored as binary doc values.
Until version 8.0, Elasticsearch did not support vector search, but the groundwork was laid for future vector search features.
The technical preview of the _knn_search API endpoint in Elasticsearch 8.0 marked a significant milestone in bringing vector search to the platform.
Elasticsearch's decision to utilize Lucene as their underlying search engine also led to the choice of Lucene as the vector engine.
This decision was made in a transparent manner, with the rationale behind it explained in detail.
The support for scalar quantization added in 8.12 was a significant update, building on the compression technique discussed in a previous article.
Similarly, the addition of better binary quantization (BBQ) in 8.18 introduced a new groundbreaking algorithm that improved vector search capabilities.
You might like: Elasticsearch Lucene
Introduction to
Elasticsearch has evolved significantly, and it now supports vector search through the dense_vector datatype. This enhancement allows for intricate vector similarity searches, expanding Elasticsearch's utility beyond traditional search capabilities.
Initially, Elasticsearch didn't support vector search, but it now accommodates vectors with hundreds or thousands of dimensions by storing them as dense float arrays within documents. This flexibility enables multiple vectors per document.
Recent benchmarks show substantial improvements in search latency and throughput when comparing vector databases to Elasticsearch. For instance, Pinecone demonstrates a remarkable 99th percentile latency reduction from 1600ms in Elasticsearch to just 7ms.
Benefits and Features
Elasticsearch vector databases offer efficiency at scale by enabling seamless data migration across on-premises and cloud environments.
With vector databases, you can perform similarity search, which returns the right results even when the words don't quite match. This is perfect for recommendation systems, image search, and content discovery.
Similarity search can be combined with keyword search to bring both precision and nuance to your search results. This is known as hybrid search, which can return the right results even when the words don't quite match.
Expand your knowledge: Match Query in Elasticsearch
Elasticsearch offers built-in security, regulatory compliance, and high availability, making it a top choice for vector databases. With over a decade of expertise in search, Elasticsearch ensures top-tier search relevance and flexible deployment options.
Elasticsearch can handle large-scale vector data and integrate seamlessly with other tools in the data pipeline. It's also versatile, supporting search through text, numerical data, geospatial data, and yes, vectors.
Elasticsearch's hybrid search blends keywords, vectors, geo data, metadata, and more in a single API call. This allows you to rank results by meaning, precision, and context.
Some of the key features of Elasticsearch include:
- Filters and facets that run fast, even at scale
- Inference APIs that execute native inference with popular LLMs or built-in models
- Better Binary Quantization (BBQ) that reduces memory footprint up to 95%
- The semantic_text field that handles mappings, embeddings, and chunking automatically
- AI Playground that lets you test hybrid retrieval, relevance ranking, and chunking strategies in real time
Setup and Configuration
To set up k-NN, you need to create an index that defines at least one field of type dense_vector, where your vector data will be stored and/or indexed.
The mapping below shows a dense_vector field called title_vector of dimension 3, which uses the dot_product similarity function.
You can define and configure a dense_vector field by simply specifying its configuration parameters, which have been drastically simplified since version 8.11.
The table below summarizes all available configuration parameters for the dense_vector field type provided by Elasticsearch, including the dimension of the vector and the algorithm used for indexing.
How to Set Up

To set up vector search in Elasticsearch, you need to create an index that defines at least one field of type dense_vector. This field will store and index your vector data.
You can use the dot_product similarity function, which is available by default in Elasticsearch. This function is suitable for dense vectors stored and indexed in the field.
Elasticsearch relies on Apache Lucene for indexing dense vectors, and since version 8.11, the definition of vector fields has been simplified. You can now index vector data by simply defining and configuring a dense_vector field.
The table below summarizes the available configuration parameters for the dense_vector field type provided by Elasticsearch. It's worth noting that since version 8.11, the definition of vector fields has been drastically simplified.
You can use the knn search option or the knn DSL query (introduced in 8.12) to run vector search queries in Elasticsearch. To build your vector field, you need to define a mapping for your index, specifying that you have a vector field.
For another approach, see: Elasticsearch Use Cases

A good starting point is to define an index called 'my_vector_index' with two fields: 'my_vector' and 'my_text'. 'my_vector' is your vector field, and you've specified that it's a dense vector with 300 dimensions.
Elasticsearch offers two primary methods for vector search: cosine similarity and Euclidean distance. To perform a cosine similarity search, you can use the script_score query and define a custom scoring method, which in this case is cosine similarity.
The moment you've been waiting for is finally here – vector search! To get started, create an index that defines a dense_vector field, and configure it with the required parameters.
Query Store
You'll likely want to query your vector store during the running of your chain or agent. This is where filtering comes in, which we'll explore in more detail.
Once your vector store has been created and the relevant documents have been added, you can use filtering when searching.
Similarity and Retrieval
Elasticsearch supports various vector distance similarity algorithms, including cosine, euclidean, and dot_product. The cosine similarity algorithm is the default.
You can specify the similarity algorithm needed via the similarity parameter. If you need to change the similarity algorithm, you need to delete the index and recreate it with the correct distance_strategy.
The default retrieval strategy in Elasticsearch is the DenseVectorStrategy, which returns the top k most similar vectors to the query vector. The k parameter is set when the ElasticsearchStore is initialized, with a default value of 10.
Elasticsearch also supports hybrid retrieval, which combines approximate semantic search and keyword based search. To enable hybrid retrieval, you need to set hybrid=True in the DenseVectorStrategy constructor.
Here are some common vector distance similarity algorithms supported by Elasticsearch:
- cosine
- euclidean
- dot_product
Note that the similarity algorithm cannot be changed at query time, it needs to be set when creating the index mapping for field.
Retrieval Strategies
Elasticsearch has big advantages over other vector only databases from its ability to support a wide range of retrieval strategies.
By default, ElasticsearchStore uses the DenseVectorStrategy, which returns the top k most similar vectors to the query vector, with the k parameter set to 10 by default.
You can configure ElasticsearchStore to support other retrieval strategies, such as hybrid retrieval, which combines approximate semantic search and keyword based search.
To enable hybrid retrieval, set hybrid=True in the DenseVectorStrategy constructor, and note that RRF (Reciprocal Rank Fusion) is used to balance the two scores from different retrieval methods, requiring Elasticsearch 8.9.0 or above.
DenseVectorScriptScoreStrategy uses Elasticsearch's script score query to perform exact vector retrieval, also known as brute force, to retrieve the top-k results.
This strategy was previously called ExactRetrievalStrategy prior to version 0.2.0, and you can use it by specifying DenseVectorScriptScoreStrategy in ElasticsearchStore constructor.
SparseVectorStrategy, also known as ELSER, uses Elasticsearch's sparse vector retrieval to retrieve the top-k results, but you'll need to provide a model ID and ensure the ELSER model is deployed and running in Elasticsearch ml node.
Expand your knowledge: Elasticsearch Hybrid Search
Improving Performance
Tuning k-NN can significantly impact your Elasticsearch cluster's performance. You can optimize indexing and search performance of k-NN searches by checking out the options in the official documentation.
There are quite a few options available for k-NN optimization. They include various indexing and search performance optimizations.
Take a look at this: Elasticsearch Performance
Upgrading to Store
If you're already using Elasticsearch in your project, it's time to upgrade to the new ElasticsearchStore implementation, which is more flexible and easier to use.
The old implementations, ElasticVectorSearch and ElasticKNNSearch, are now deprecated, so it's essential to make the switch to improve your project's performance.
Upgrading to ElasticsearchStore will give you a more streamlined experience, allowing you to focus on other aspects of your project without worrying about outdated code.
By making this change, you'll be able to take full advantage of Elasticsearch's capabilities and optimize your project's performance to its full potential.
Related reading: Elasticsearch Full Text Search
Tuning
Tuning is a crucial step in improving performance. You can optimize the indexing and search performance of k-NN searches by checking out the options in the official documentation.

Optimizing k-NN searches requires a good understanding of the available options. We won't be reviewing them in this article, but you should definitely check them out if you're serious about implementing k-NN searches in your Elasticsearch cluster.
The vector store is another area where tuning can make a big difference. Once your vector store has been created and the relevant documents have been added, you'll likely want to query it during the running of your chain or agent.
Querying the vector store can be done with filtering, as shown in the examples. This allows you to refine your search results and get more accurate predictions.
Scaling Elasticsearch
Scaling Elasticsearch is all about handling large volumes of data efficiently. Elasticsearch can easily integrate with other tools in the Elastic Stack, like Logstash for data ingestion and Kibana for data visualization.
To make the most of Elasticsearch, you can generate vector representations of your data using machine learning models like Word2Vec, BERT, or Doc2Vec. These vectors can be added to your index as a field, like 'my_vector' in the example.
Adding vectors to Elasticsearch can help improve performance by enabling more efficient querying and analysis. For instance, you can use these vectors to scale your searches and get more accurate results.
Elasticsearch can handle large volumes of data, but it's essential to integrate it with other tools in the Elastic Stack to get the most out of it. This comprehensive solution enables you to process and analyze your data more efficiently.
Machine Learning Connection
The fusion of dense vector fields with machine learning algorithms empowers developers and data scientists to delve into the intricacies of high-dimensional data representations.
Elasticsearch's evolution into a versatile vector search tool underscores its adaptability in catering to diverse machine learning requirements.
By leveraging dense vectors for semantic understanding and similarity computations, machine learning models can now navigate complex datasets with precision and efficiency.
The synergy between dense vectors and machine learning algorithms revolutionizes how data scientists extract insights and build robust predictive models.
This collaboration heralds a new era of innovation, propelling AI-driven solutions towards unprecedented levels of accuracy and efficiency.
The connection between dense vector fields and machine learning transcends traditional search boundaries, allowing for more precise and efficient data analysis.
Limitations and Challenges
Elasticsearch vector search has its limitations and challenges, and it's essential to be aware of them before diving in.
Until Elasticsearch 8.11, k-NN searches couldn't be run on vector fields located inside nested documents. This limitation has been lifted in 8.12, but such nested knn queries don't support the specification of a filter.
The search_type is always set to dfs_query_then_fetch and can't be changed dynamically.
k-NN searches have another limitation: the ccs_minimize_roundtrips option isn't supported when searching across different clusters with cross-cluster search.
One thing to keep in mind is that due to the nature of the HNSW algorithm used by Lucene, the k nearest neighbors returned by k-NN searches are not always the true ones. This is because "approximate" really means approximate, and you shouldn't rely solely on these results.
Here are some key limitations to keep in mind:
- Up until 8.11, k-NN searches can't be run on vector fields inside nested documents.
- The search_type is always set to dfs_query_then_fetch.
- The ccs_minimize_roundtrips option isn't supported with cross-cluster search.
- k-NN searches return approximate results due to the HNSW algorithm.
Getting Started
Elasticsearch Vector is a powerful tool for indexing and searching data, but getting started can be overwhelming.
Elasticsearch Vector requires a good understanding of its architecture, which consists of the Elasticsearch engine, the vector index, and the vector search module.
To start using Elasticsearch Vector, you'll need to install the Elasticsearch engine, which can be done using a package manager like apt or yum.
Elasticsearch Vector can be used with various programming languages, including Python and Java.
The first step in using Elasticsearch Vector is to create a vector index, which can be done using the Elasticsearch API.
You can create a vector index by sending a POST request to the Elasticsearch API endpoint, specifying the index name and the data type.
Elasticsearch Vector supports various data types, including dense vectors and sparse vectors.
Dense vectors are represented as a list of numbers, while sparse vectors are represented as a dictionary of key-value pairs.
To create a dense vector, you can use the Elasticsearch API to create a new index and specify the data type as 'dense_vector'.
To create a sparse vector, you can use the Elasticsearch API to create a new index and specify the data type as 'sparse_vector'.
On a similar theme: Elasticsearch Create Index with Mapping
Frequently Asked Questions
What is the difference between search and vector search?
Vector search goes beyond traditional keyword searches by considering the entire data context, offering more precise results. This approach is particularly effective for complex applications like image recognition and recommendation systems
Featured Images: pexels.com


