Class RedisVectorStore

All Implemented Interfaces:
Consumer<List<Document>>, DocumentWriter, VectorStore, VectorStoreRetriever, org.springframework.beans.factory.InitializingBean

public class RedisVectorStore extends 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: