Class RedisVectorStore

java.lang.Object
org.springframework.ai.vectorstore.observation.AbstractObservationVectorStore
org.springframework.ai.vectorstore.redis.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

public class RedisVectorStore extends org.springframework.ai.vectorstore.observation.AbstractObservationVectorStore implements org.springframework.beans.factory.InitializingBean
Redis-based vector store implementation using Redis Stack with RediSearch and RedisJSON.

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:
  • VectorStore
  • EmbeddingModel
  • Field Details

  • Constructor Details

  • Method Details

    • getJedis

      public redis.clients.jedis.JedisPooled getJedis()
    • doAdd

      public void doAdd(List<org.springframework.ai.document.Document> documents)
      Specified by:
      doAdd in class org.springframework.ai.vectorstore.observation.AbstractObservationVectorStore
    • doDelete

      public void doDelete(List<String> idList)
      Specified by:
      doDelete in class org.springframework.ai.vectorstore.observation.AbstractObservationVectorStore
    • doDelete

      protected void doDelete(org.springframework.ai.vectorstore.filter.Filter.Expression filterExpression)
      Overrides:
      doDelete in class org.springframework.ai.vectorstore.observation.AbstractObservationVectorStore
    • doSimilaritySearch

      public List<org.springframework.ai.document.Document> doSimilaritySearch(org.springframework.ai.vectorstore.SearchRequest request)
      Specified by:
      doSimilaritySearch in class org.springframework.ai.vectorstore.observation.AbstractObservationVectorStore
    • afterPropertiesSet

      public void afterPropertiesSet()
      Specified by:
      afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean
    • createObservationContextBuilder

      public org.springframework.ai.vectorstore.observation.VectorStoreObservationContext.Builder createObservationContextBuilder(String operationName)
      Specified by:
      createObservationContextBuilder in class org.springframework.ai.vectorstore.observation.AbstractObservationVectorStore
    • getNativeClient

      public <T> Optional<T> getNativeClient()
      Specified by:
      getNativeClient in interface org.springframework.ai.vectorstore.VectorStore
    • builder

      public static RedisVectorStore.Builder builder(redis.clients.jedis.JedisPooled jedis, org.springframework.ai.embedding.EmbeddingModel embeddingModel)