Class RedisVectorStore
- All Implemented Interfaces:
Consumer<List<Document>>,DocumentWriter,VectorStore,VectorStoreRetriever,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:
-
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, EmbeddingModel embeddingModel) createObservationContextBuilder(String operationName) Create a newVectorStoreObservationContext.Builderinstance.voidPerform the actual add operation.voidPerform the actual delete operation.protected voiddoDelete(Filter.Expression filterExpression) Template method for concrete implementations to provide filter-based deletion logic.doSimilaritySearch(SearchRequest request) Perform the actual similarity search operation.redis.clients.jedis.JedisPooledgetJedis()<T> Optional<T> Returns the native client if available in this vector store implementation.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, getNameMethods inherited from interface org.springframework.ai.vectorstore.VectorStoreRetriever
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
Description copied from class:AbstractObservationVectorStorePerform the actual add operation.- Specified by:
doAddin classAbstractObservationVectorStore- Parameters:
documents- the documents to add
-
doDelete
Description copied from class:AbstractObservationVectorStorePerform the actual delete operation.- Specified by:
doDeletein classAbstractObservationVectorStore- Parameters:
idList- the list of document IDs to delete
-
doDelete
Description copied from class:AbstractObservationVectorStoreTemplate method for concrete implementations to provide filter-based deletion logic.- Overrides:
doDeletein classAbstractObservationVectorStore- Parameters:
filterExpression- Filter expression to identify documents to delete
-
doSimilaritySearch
Description copied from class:AbstractObservationVectorStorePerform the actual similarity search operation.- Specified by:
doSimilaritySearchin classAbstractObservationVectorStore- Parameters:
request- the search request- Returns:
- the list of documents that match the query request conditions
-
afterPropertiesSet
public void afterPropertiesSet()- Specified by:
afterPropertiesSetin interfaceorg.springframework.beans.factory.InitializingBean
-
createObservationContextBuilder
Description copied from class:AbstractObservationVectorStoreCreate a newVectorStoreObservationContext.Builderinstance.- Specified by:
createObservationContextBuilderin classAbstractObservationVectorStore- Parameters:
operationName- the operation name- Returns:
- the observation context builder
-
getNativeClient
Description copied from interface:VectorStoreReturns 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 Optionalinvalid input: '<'?> will return the native client if one exists, rather than an empty Optional. For type safety, prefer using the specific client type. - Specified by:
getNativeClientin interfaceVectorStore- Type Parameters:
T- The type of the native client- Returns:
- Optional containing native client if available, empty Optional otherwise
-
builder
public static RedisVectorStore.Builder builder(redis.clients.jedis.JedisPooled jedis, EmbeddingModel embeddingModel)
-