Class ElasticsearchVectorStore
java.lang.Object
org.springframework.ai.vectorstore.observation.AbstractObservationVectorStore
org.springframework.ai.vectorstore.elasticsearch.ElasticsearchVectorStore
- All Implemented Interfaces:
Consumer<List<Document>>
,DocumentWriter
,VectorStore
,org.springframework.beans.factory.InitializingBean
public class ElasticsearchVectorStore
extends AbstractObservationVectorStore
implements org.springframework.beans.factory.InitializingBean
Elasticsearch-based vector store implementation using the dense_vector field type.
The store uses an Elasticsearch index to persist vector embeddings along with their associated document content and metadata. The implementation leverages Elasticsearch's k-NN search capabilities for efficient similarity search operations.
Features:
- Automatic schema initialization with configurable index creation
- Support for multiple similarity functions: Cosine, L2 Norm, and Dot Product
- Metadata filtering using Elasticsearch query strings
- Configurable similarity thresholds for search results
- Batch processing support with configurable strategies
- Observation and metrics support through Micrometer
Basic usage example:
ElasticsearchVectorStore vectorStore = ElasticsearchVectorStore.builder(restClient, embeddingModel)
.initializeSchema(true)
.build();
// Add documents
vectorStore.add(List.of(
new Document("content1", Map.of("key1", "value1")),
new Document("content2", Map.of("key2", "value2"))
));
// Search with filters
List<Document> results = vectorStore.similaritySearch(
SearchRequest.query("search text")
.withTopK(5)
.withSimilarityThreshold(0.7)
.withFilterExpression("key1 == 'value1'")
);
Advanced configuration example:
ElasticsearchVectorStoreOptions options = new ElasticsearchVectorStoreOptions();
options.setIndexName("custom_vectors");
options.setSimilarity(SimilarityFunction.dot_product);
options.setDimensions(1536);
ElasticsearchVectorStore vectorStore = ElasticsearchVectorStore.builder(restClient, embeddingModel)
.options(options)
.initializeSchema(true)
.batchingStrategy(new TokenCountBatchingStrategy())
.build();
Requirements:
- Elasticsearch 8.0 or later
- Index mapping with id (string), content (text), metadata (object), and embedding (dense_vector) fields
Similarity Functions:
- cosine: Default, suitable for most use cases. Measures cosine similarity between vectors.
- l2_norm: Euclidean distance between vectors. Lower values indicate higher similarity.
- dot_product: Best performance for normalized vectors (e.g., OpenAI embeddings).
- Since:
- 1.0.0
- Author:
- Jemin Huh, Wei Jiang, Laura Trotta, Soby Chacko, Christian Tzolov, Thomas Vitale, Ilayaperumal Gopinathan, Jonghoon Park
-
Nested Class Summary
Nested Classes -
Field Summary
Fields inherited from class org.springframework.ai.vectorstore.observation.AbstractObservationVectorStore
batchingStrategy, embeddingModel
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
-
Method Summary
Modifier and TypeMethodDescriptionvoid
builder
(org.elasticsearch.client.RestClient restClient, EmbeddingModel embeddingModel) Creates a new builder instance for ElasticsearchVectorStore.createObservationContextBuilder
(String operationName) Create a newVectorStoreObservationContext.Builder
instance.void
Perform the actual add operation.void
Perform the actual delete operation.void
doDelete
(Filter.Expression filterExpression) Template method for concrete implementations to provide filter-based deletion logic.doSimilaritySearch
(SearchRequest searchRequest) Perform the actual similarity search operation.<T> Optional<T>
Returns the native client if available in this vector store implementation.boolean
Methods inherited from class org.springframework.ai.vectorstore.observation.AbstractObservationVectorStore
add, delete, delete, similaritySearch
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.springframework.ai.document.DocumentWriter
write
Methods inherited from interface org.springframework.ai.vectorstore.VectorStore
accept, delete, getName, similaritySearch
-
Constructor Details
-
ElasticsearchVectorStore
-
-
Method Details
-
doAdd
Description copied from class:AbstractObservationVectorStore
Perform the actual add operation.- Specified by:
doAdd
in classAbstractObservationVectorStore
- Parameters:
documents
- the documents to add
-
doDelete
Description copied from class:AbstractObservationVectorStore
Perform the actual delete operation.- Specified by:
doDelete
in classAbstractObservationVectorStore
- Parameters:
idList
- the list of document IDs to delete
-
doDelete
Description copied from class:AbstractObservationVectorStore
Template method for concrete implementations to provide filter-based deletion logic.- Overrides:
doDelete
in classAbstractObservationVectorStore
- Parameters:
filterExpression
- Filter expression to identify documents to delete
-
doSimilaritySearch
Description copied from class:AbstractObservationVectorStore
Perform the actual similarity search operation.- Specified by:
doSimilaritySearch
in classAbstractObservationVectorStore
- Parameters:
searchRequest
- the search request- Returns:
- the list of documents that match the query request conditions
-
indexExists
public boolean indexExists() -
afterPropertiesSet
public void afterPropertiesSet()- Specified by:
afterPropertiesSet
in interfaceorg.springframework.beans.factory.InitializingBean
-
createObservationContextBuilder
Description copied from class:AbstractObservationVectorStore
Create a newVectorStoreObservationContext.Builder
instance.- Specified by:
createObservationContextBuilder
in classAbstractObservationVectorStore
- Parameters:
operationName
- the operation name- Returns:
- the observation context builder
-
getNativeClient
Description copied from interface:VectorStore
Returns the native client if available in this vector store implementation. Note on usage: 1. Returns empty Optional when no native client is available 2. Due to Java type erasure, runtime type checking is not possible Example usage: When working with implementation with known native client: Optionalclient = vectorStore.getNativeClient(); Note: Using Optional<?> will return the native client if one exists, rather than an empty Optional. For type safety, prefer using the specific client type. - Specified by:
getNativeClient
in interfaceVectorStore
- Type Parameters:
T
- The type of the native client- Returns:
- Optional containing native client if available, empty Optional otherwise
-
builder
public static ElasticsearchVectorStore.Builder builder(org.elasticsearch.client.RestClient restClient, EmbeddingModel embeddingModel) Creates a new builder instance for ElasticsearchVectorStore.- Returns:
- a new ElasticsearchBuilder instance
-