Elasticsearch is a distributed, RESTful search and analytics engine, capable of performing both vector and lexical search. It is built on top of the Apache Lucene library.This notebook shows how to use functionality related to the
Elasticsearch
vector store.
Setup
In order to use theElasticsearch
vector search you must install the langchain-elasticsearch
package.
Credentials
There are two main ways to setup an Elasticsearch instance for use with:- Elastic Cloud: Elastic Cloud is a managed Elasticsearch service. Signup for a free trial.
- Local Install Elasticsearch: Get started with Elasticsearch by running it locally. The easiest way is to use the official Elasticsearch Docker image. See the Elasticsearch Docker documentation for more information.
Running Elasticsearch via Docker
Example: Run a single-node Elasticsearch instance with security disabled. This is not recommended for production use.Running with Authentication
For production, we recommend you run with security enabled. To connect with login credentials, you can use the parameterses_api_key
or es_user
and es_password
.
How to obtain a password for the default “elastic” user?
To obtain your Elastic Cloud password for the default “elastic” user:- Log in to the Elastic Cloud console at cloud.elastic.co
- Go to “Security” > “Users”
- Locate the “elastic” user and click “Edit”
- Click “Reset password”
- Follow the prompts to reset the password
How to obtain an API key?
To obtain an API key:- Log in to the Elastic Cloud console at cloud.elastic.co
- Open Kibana and go to Stack Management > API Keys
- Click “Create API key”
- Enter a name for the API key and click “Create”
- Copy the API key and paste it into the
api_key
parameter
Elastic Cloud
To connect to an Elasticsearch instance on Elastic Cloud, you can use either thees_cloud_id
parameter or es_url
.
Initialization
Elasticsearch is running locally on localhost:9200 with docker. For more details on how to connect to Elasticsearch from Elastic Cloud, see connecting with authentication above.Manage vector store
Add items to vector store
Delete items from vector store
Query vector store
Once your vector store has been created and the relevant documents have been added you will most likely wish to query it during the running of your chain or agent. These examples also show how to use filtering when searching.Query directly
Similarity search
Performing a simple similarity search with filtering on metadata can be done as follows:Similarity search with score
If you want to execute a similarity search and receive the corresponding scores you can run:Query by turning into retriever
You can also transform the vector store into a retriever for easier usage in your chains.Distance Similarity Algorithm
Elasticsearch supports the following vector distance similarity algorithms:- cosine
- euclidean
- dot_product
Retrieval Strategies
Elasticsearch has big advantages over other vector only databases from its ability to support a wide range of retrieval strategies. In this notebook we will configureElasticsearchStore
to support some of the most common retrieval strategies.
By default, ElasticsearchStore
uses the DenseVectorStrategy
(was called ApproxRetrievalStrategy
prior to version 0.2.0).
DenseVectorStrategy
This will return the top k most similar vectors to the query vector. Thek
parameter is set when the ElasticsearchStore
is initialized. The default value is 10.
Example: Hybrid retrieval with dense vector and keyword search
This example will show how to configure ElasticsearchStore to perform a hybrid retrieval, using a combination of approximate semantic search and keyword based search. We use RRF to balance the two scores from different retrieval methods. To enable hybrid retrieval, we need to sethybrid=True
in the DenseVectorStrategy
constructor.
Example: Dense vector search with Embedding Model in Elasticsearch
This example will show how to configureElasticsearchStore
to use the embedding model deployed in Elasticsearch for dense vector retrieval.
To use this, specify the model_id in DenseVectorStrategy
constructor via the query_model_id
argument.
NOTE: This requires the model to be deployed and running in Elasticsearch ML node. See notebook example on how to deploy the model with eland
.
SparseVectorStrategy (ELSER)
This strategy uses Elasticsearch’s sparse vector retrieval to retrieve the top-k results. We only support our own “ELSER” embedding model for now. NOTE: This requires the ELSER model to be deployed and running in Elasticsearch ml node. To use this, specifySparseVectorStrategy
(was called SparseVectorRetrievalStrategy
prior to version 0.2.0) in the ElasticsearchStore
constructor. You will need to provide a model ID.
DenseVectorScriptScoreStrategy
This strategy 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 calledExactRetrievalStrategy
prior to version 0.2.0.)
To use this, specify DenseVectorScriptScoreStrategy
in ElasticsearchStore
constructor.
BM25Strategy
Finally, you can use full-text keyword search. To use this, specifyBM25Strategy
in ElasticsearchStore
constructor.
BM25RetrievalStrategy
This strategy allows the user to perform searches using pure BM25 without vector search. To use this, specifyBM25RetrievalStrategy
in ElasticsearchStore
constructor.
Note that in the example below, the embedding option is not specified, indicating that the search is conducted without using embeddings.
Customise the Query
Withcustom_query
parameter at search, you are able to adjust the query that is used to retrieve documents from Elasticsearch. This is useful if you want to use a more complex query, to support linear boosting of fields.
Customize the Document Builder
Withdoc_builder
parameter at search, you are able to adjust how a Document is being built using data retrieved from Elasticsearch. This is especially useful if you have indices which were not created using LangChain.
Usage for retrieval-augmented generation
For guides on how to use this vector store for retrieval-augmented generation (RAG), see the following sections:FAQ
Question: Im getting timeout errors when indexing documents into Elasticsearch. How do I fix this?
One possible issue is your documents might take longer to index into Elasticsearch. ElasticsearchStore uses the Elasticsearch bulk API which has a few defaults that you can adjust to reduce the chance of timeout errors. This is also a good idea when you’re using SparseVectorRetrievalStrategy. The defaults are:chunk_size
: 500max_chunk_bytes
: 100MB
chunk_size
and max_chunk_bytes
parameters to the ElasticsearchStore add_texts
method.
Upgrading to ElasticsearchStore
If you’re already using Elasticsearch in your langchain based project, you may be using the old implementations:ElasticVectorSearch
and ElasticKNNSearch
which are now deprecated. We’ve introduced a new implementation called ElasticsearchStore
which is more flexible and easier to use. This notebook will guide you through the process of upgrading to the new implementation.
What’s new?
The new implementation is now one class calledElasticsearchStore
which can be used for approximate dense vector, exact dense vector, sparse vector (ELSER), BM25 retrieval and hybrid retrieval, via strategies.
I am using ElasticKNNSearch
Old implementation:I am using ElasticVectorSearch
Old implementation:API reference
For detailed documentation of allElasticSearchStore
features and configurations head to the API reference: python.langchain.com/api_reference/elasticsearch/vectorstores/langchain_elasticsearch.vectorstores.ElasticsearchStore.html