pub struct RedisVectorStore<M>where
M: EmbeddingModel,{ /* private fields */ }Expand description
Redis vector store implementation using RediSearch vector similarity search.
Uses Redis’s FT.SEARCH command with KNN vector queries for similarity search.
Internally holds a ConnectionManager for automatic reconnection on transient failures.
§Key Prefix
If your RediSearch index uses a PREFIX configuration (e.g., PREFIX 1 doc:),
you must call RedisVectorStore::with_key_prefix with the matching prefix
so that inserted documents are discoverable by the index.
§Metadata Fields
Configure metadata fields via RedisVectorStore::with_metadata_fields to enable
filtering. During insertion, these fields are extracted from the serialized document
and stored as separate hash fields that RediSearch can index and filter on.
Implementations§
Source§impl<M> RedisVectorStore<M>where
M: EmbeddingModel,
impl<M> RedisVectorStore<M>where
M: EmbeddingModel,
Sourcepub async fn new(
model: M,
client: Client,
index_name: String,
vector_field: String,
) -> Result<Self, VectorStoreError>
pub async fn new( model: M, client: Client, index_name: String, vector_field: String, ) -> Result<Self, VectorStoreError>
Creates a new Redis vector store instance.
Establishes a ConnectionManager from the provided client for automatic
reconnection on transient network failures.
§Arguments
model- Embedding model for query vectorizationclient- Redis client instanceindex_name- Name of the RediSearch index to queryvector_field- Name of the vector field in the index
§Errors
Returns an error if the initial connection to Redis cannot be established.
Sourcepub fn with_distance_metric(self, metric: DistanceMetric) -> Self
pub fn with_distance_metric(self, metric: DistanceMetric) -> Self
Sets the distance metric the index uses (default DistanceMetric::Cosine).
This must match the DISTANCE_METRIC of the RediSearch index so that
returned distances are converted to similarity scores correctly. Use
Self::validate_index to verify the index agrees.
Sourcepub fn with_key_prefix(self, prefix: String) -> Self
pub fn with_key_prefix(self, prefix: String) -> Self
Sets a key prefix for document keys.
Documents stored via InsertDocuments will be keyed as {prefix}{uuid}.
This prefix must match the index’s PREFIX configuration for documents
to be indexed and discoverable by FT.SEARCH.
Sourcepub fn with_metadata_fields(self, fields: Vec<String>) -> Self
pub fn with_metadata_fields(self, fields: Vec<String>) -> Self
Configures metadata fields to extract from documents during insertion.
When documents are inserted, the specified fields are extracted from the serialized JSON representation and written as separate hash fields, making them available for RediSearch filter queries (TAG, NUMERIC, TEXT). The field names must match top-level keys in the serialized document JSON and be declared in the RediSearch index schema. Calling this method replaces any previously configured field list.
Fields that are missing from a document or have null/complex values are
skipped with a warning log. Reserved field names (document, embedded_text,
and the configured vector field) are filtered out with a warning to prevent
data corruption.
Note: RediSearch TAG fields split stored values on a separator (, by
default). Extracted string values containing the separator will be indexed
as multiple tags; create the TAG field with a different SEPARATOR if your
values may contain commas.
Sourcepub async fn validate_index(&self) -> Result<(), VectorStoreError>
pub async fn validate_index(&self) -> Result<(), VectorStoreError>
Validates that the configured index exists and is compatible with this store.
Checks, via FT.INFO, that:
- the index exists,
- every vector field uses the store’s configured distance metric, and
- if a key prefix is configured, the index is defined with that prefix (otherwise inserted documents would never be indexed).
Call this after building the store to fail fast on schema mismatches.
Sourcepub async fn create_index(
&self,
dimensions: usize,
metadata_fields: &[(String, MetadataFieldType)],
) -> Result<(), VectorStoreError>
pub async fn create_index( &self, dimensions: usize, metadata_fields: &[(String, MetadataFieldType)], ) -> Result<(), VectorStoreError>
Creates the RediSearch index for this store (HASH, FLAT, FLOAT32, COSINE).
Uses the store’s index name, vector field, and (if set) key prefix, plus the
document and embedded_text TEXT fields. Add any metadata fields you intend
to filter on. This is a convenience for setups that manage the index in code;
production deployments may prefer to create the index out of band.
Sourcepub async fn delete(&self, ids: &[String]) -> Result<u64, VectorStoreError>
pub async fn delete(&self, ids: &[String]) -> Result<u64, VectorStoreError>
Deletes documents by their hash keys (the IDs returned by Self::top_n_ids).
Uses UNLINK (non-blocking delete). Returns the number of keys removed.
Trait Implementations§
Source§impl<Model> InsertDocuments for RedisVectorStore<Model>
impl<Model> InsertDocuments for RedisVectorStore<Model>
Source§async fn insert_documents<Doc: Serialize + Embed + Send>(
&self,
documents: Vec<(Doc, OneOrMany<Embedding>)>,
) -> Result<(), VectorStoreError>
async fn insert_documents<Doc: Serialize + Embed + Send>( &self, documents: Vec<(Doc, OneOrMany<Embedding>)>, ) -> Result<(), VectorStoreError>
Inserts documents with their precomputed embeddings into Redis.
Each embedding in OneOrMany<Embedding> produces a separate Redis hash
keyed by {prefix}{uuid}. All hashes for a document share the same serialized
JSON in the document field but have distinct embedded_text values.
Source§impl<M> VectorStoreIndex for RedisVectorStore<M>
impl<M> VectorStoreIndex for RedisVectorStore<M>
Source§async fn top_n<T: for<'a> Deserialize<'a> + Send>(
&self,
req: VectorSearchRequest<Self::Filter>,
) -> Result<Vec<(f64, String, T)>, VectorStoreError>
async fn top_n<T: for<'a> Deserialize<'a> + Send>( &self, req: VectorSearchRequest<Self::Filter>, ) -> Result<Vec<(f64, String, T)>, VectorStoreError>
(score, id, document) tuples.Source§async fn top_n_ids(
&self,
req: VectorSearchRequest<Self::Filter>,
) -> Result<Vec<(f64, String)>, VectorStoreError>
async fn top_n_ids( &self, req: VectorSearchRequest<Self::Filter>, ) -> Result<Vec<(f64, String)>, VectorStoreError>
(score, id) tuples.