Skip to main content
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 the Elasticsearch vector search you must install the langchain-elasticsearch package.

Credentials

There are two main ways to setup an Elasticsearch instance for use with:
  1. Elastic Cloud: Elastic Cloud is a managed Elasticsearch service. Signup for a free trial.
To connect to an Elasticsearch instance that does not require login credentials (starting the docker instance with security enabled), pass the Elasticsearch URL and index name along with the embedding object to the constructor.
  1. 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 locally

The easiest way to run Elasticsearch locally for development and testing is using the start-local script. This script sets up Elasticsearch (and optionally Kibana) using Docker with a simple one-line command.
This creates an elastic-start-local folder containing configuration files and startup scripts. To start Elasticsearch:
Elasticsearch will be available at http://localhost:9200. The password for the elastic user and API key are automatically generated and stored in the .env file in the elastic-start-local folder. If you only need Elasticsearch without Kibana, you can use the --esonly option:
The start-local setup is for local testing only and should not be used in production. For production installations, refer to the official Elasticsearch documentation.

Running with authentication

For production, we recommend you run with security enabled. To connect with login credentials, you can use the parameters es_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:
  1. Log in to the Elastic Cloud console at cloud.elastic.co
  2. Go to “Security” > “Users”
  3. Locate the “elastic” user and click “Edit”
  4. Click “Reset password”
  5. Follow the prompts to reset the password

How to obtain an API key?

To obtain an API key:
  1. Log in to the Elastic Cloud console at cloud.elastic.co
  2. Open Kibana and go to Stack Management > API Keys
  3. Click “Create API key”
  4. Enter a name for the API key and click “Create”
  5. 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 the es_cloud_id parameter or es_url.
If you want to get best in-class automated tracing of your model calls you can also set your LangSmith API key by uncommenting below:

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

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
The cosine similarity algorithm is the default. You can specify the similarity Algorithm needed via the similarity parameter. NOTE: Depending on the retrieval strategy, the similarity algorithm cannot be changed at query time. It is needed to be set when creating the index mapping for field. If you need to change the similarity algorithm, you need to delete the index and recreate it with the correct distance_strategy.

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 configure ElasticsearchStore 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. The k parameter is set when the ElasticsearchStore is initialized. The default value is 10.
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 set hybrid=True in the DenseVectorStrategy constructor.
When hybrid is enabled, the query performed will be a combination of approximate semantic search and keyword based search. It will use rrf (Reciprocal Rank Fusion) to balance the two scores from different retrieval methods. Note: RRF requires Elasticsearch 8.9.0 or above.

Example: Dense vector search with embedding model in Elasticsearch

This example will show how to configure ElasticsearchStore 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, specify SparseVectorStrategy (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 called ExactRetrievalStrategy 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, specify BM25Strategy in ElasticsearchStore constructor.

BM25RetrievalStrategy

This strategy allows the user to perform searches using pure BM25 without vector search. To use this, specify BM25RetrievalStrategy 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

With custom_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

With doc_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: 500
  • max_chunk_bytes: 100MB
To adjust these, you can pass in the 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 called ElasticsearchStore 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

I am using ElasticVectorSearch


API reference

For detailed documentation of all ElasticSearchStore features and configurations head to the API reference.