Class RedisVectorStore
- All Implemented Interfaces:
Consumer<List<org.springframework.ai.document.Document>>,org.springframework.ai.document.DocumentWriter,org.springframework.ai.vectorstore.VectorStore,org.springframework.beans.factory.InitializingBean
The store uses Redis JSON documents to persist vector embeddings along with their associated document content and metadata. It leverages RediSearch for creating and querying vector similarity indexes. The RedisVectorStore manages and queries vector data, offering functionalities like adding, deleting, and performing similarity searches on documents.
The store utilizes RedisJSON and RedisSearch to handle JSON documents and to index and search vector data. It supports various vector algorithms (e.g., FLAT, HNSW) for efficient similarity searches. Additionally, it allows for custom metadata fields in the documents to be stored alongside the vector and content data.
Features:
- Automatic schema initialization with configurable index creation
- Support for HNSW and FLAT vector indexing algorithms
- Cosine similarity metric for vector comparisons
- Flexible metadata field types (TEXT, TAG, NUMERIC) for advanced filtering
- Configurable similarity thresholds for search results
- Batch processing support with configurable batching strategies
Basic usage example:
RedisVectorStore vectorStore = RedisVectorStore.builder(jedisPooled, embeddingModel)
.indexName("custom-index") // Optional: defaults to "spring-ai-index"
.prefix("custom-prefix") // Optional: defaults to "embedding:"
.vectorAlgorithm(Algorithm.HNSW)
.build();
// Add documents
vectorStore.add(List.of(
new Document("content1", Map.of("meta1", "value1")),
new Document("content2", Map.of("meta2", "value2"))
));
// Search with filters
List<Document> results = vectorStore.similaritySearch(
SearchRequest.query("search text")
.withTopK(5)
.withSimilarityThreshold(0.7)
.withFilterExpression("meta1 == 'value1'")
);
Advanced configuration example:
RedisVectorStore vectorStore = RedisVectorStore.builder()
.jedis(jedisPooled)
.embeddingModel(embeddingModel)
.indexName("custom-index")
.prefix("custom-prefix")
.contentFieldName("custom_content")
.embeddingFieldName("custom_embedding")
.vectorAlgorithm(Algorithm.FLAT)
.metadataFields(
MetadataField.tag("category"),
MetadataField.numeric("year"),
MetadataField.text("description"))
.initializeSchema(true)
.batchingStrategy(new TokenCountBatchingStrategy())
.build();
Database Requirements:
- Redis Stack with RediSearch and RedisJSON modules
- Redis version 7.0 or higher
- Sufficient memory for storing vectors and indexes
Vector Algorithms:
- HNSW: Default algorithm, provides better search performance with slightly higher memory usage
- FLAT: Brute force algorithm, provides exact results but slower for large datasets
Metadata Field Types:
- TAG: For exact match filtering on categorical data
- TEXT: For full-text search capabilities
- NUMERIC: For range queries on numerical data
- Since:
- 1.0.0
- Author:
- Julien Ruaux, Christian Tzolov, EddĂș MelĂ©ndez, Thomas Vitale, Soby Chacko, Jihoon Kim
- See Also:
-
VectorStoreEmbeddingModel
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumstatic classstatic final record -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final RedisVectorStore.Algorithmstatic final StringFields inherited from class org.springframework.ai.vectorstore.observation.AbstractObservationVectorStore
batchingStrategy, embeddingModel -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidstatic RedisVectorStore.Builderbuilder(redis.clients.jedis.JedisPooled jedis, org.springframework.ai.embedding.EmbeddingModel embeddingModel) org.springframework.ai.vectorstore.observation.VectorStoreObservationContext.BuildercreateObservationContextBuilder(String operationName) voidvoidprotected voiddoDelete(org.springframework.ai.vectorstore.filter.Filter.Expression filterExpression) List<org.springframework.ai.document.Document>doSimilaritySearch(org.springframework.ai.vectorstore.SearchRequest request) redis.clients.jedis.JedisPooledgetJedis()<T> Optional<T>Methods inherited from class org.springframework.ai.vectorstore.observation.AbstractObservationVectorStore
add, delete, delete, similaritySearchMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.ai.document.DocumentWriter
writeMethods inherited from interface org.springframework.ai.vectorstore.VectorStore
accept, delete, getName, similaritySearch
-
Field Details
-
DEFAULT_INDEX_NAME
- See Also:
-
DEFAULT_CONTENT_FIELD_NAME
- See Also:
-
DEFAULT_EMBEDDING_FIELD_NAME
- See Also:
-
DEFAULT_PREFIX
- See Also:
-
DEFAULT_VECTOR_ALGORITHM
-
DISTANCE_FIELD_NAME
- See Also:
-
-
Constructor Details
-
RedisVectorStore
-
-
Method Details
-
getJedis
public redis.clients.jedis.JedisPooled getJedis() -
doAdd
- Specified by:
doAddin classorg.springframework.ai.vectorstore.observation.AbstractObservationVectorStore
-
doDelete
- Specified by:
doDeletein classorg.springframework.ai.vectorstore.observation.AbstractObservationVectorStore
-
doDelete
protected void doDelete(org.springframework.ai.vectorstore.filter.Filter.Expression filterExpression) - Overrides:
doDeletein classorg.springframework.ai.vectorstore.observation.AbstractObservationVectorStore
-
doSimilaritySearch
public List<org.springframework.ai.document.Document> doSimilaritySearch(org.springframework.ai.vectorstore.SearchRequest request) - Specified by:
doSimilaritySearchin classorg.springframework.ai.vectorstore.observation.AbstractObservationVectorStore
-
afterPropertiesSet
public void afterPropertiesSet()- Specified by:
afterPropertiesSetin interfaceorg.springframework.beans.factory.InitializingBean
-
createObservationContextBuilder
public org.springframework.ai.vectorstore.observation.VectorStoreObservationContext.Builder createObservationContextBuilder(String operationName) - Specified by:
createObservationContextBuilderin classorg.springframework.ai.vectorstore.observation.AbstractObservationVectorStore
-
getNativeClient
- Specified by:
getNativeClientin interfaceorg.springframework.ai.vectorstore.VectorStore
-
builder
public static RedisVectorStore.Builder builder(redis.clients.jedis.JedisPooled jedis, org.springframework.ai.embedding.EmbeddingModel embeddingModel)
-