Skip to main content

RedisVectorStore

Struct RedisVectorStore 

Source
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,

Source

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 vectorization
  • client - Redis client instance
  • index_name - Name of the RediSearch index to query
  • vector_field - Name of the vector field in the index
§Errors

Returns an error if the initial connection to Redis cannot be established.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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>
where Model: EmbeddingModel + Send + Sync,

Source§

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>
where M: EmbeddingModel + Send + Sync,

Source§

type Filter = Filter

The filter type for this backend.
Source§

async fn top_n<T: for<'a> Deserialize<'a> + Send>( &self, req: VectorSearchRequest<Self::Filter>, ) -> Result<Vec<(f64, String, T)>, VectorStoreError>

Returns the top N most similar documents as (score, id, document) tuples.
Source§

async fn top_n_ids( &self, req: VectorSearchRequest<Self::Filter>, ) -> Result<Vec<(f64, String)>, VectorStoreError>

Returns the top N most similar document IDs as (score, id) tuples.
Source§

impl<M> VectorStoreIndexDyn for RedisVectorStore<M>
where M: EmbeddingModel + Sync + Send,

Source§

fn top_n<'a>( &'a self, req: VectorSearchRequest<CoreFilter<Value>>, ) -> WasmBoxedFuture<'a, TopNResults>

Returns the top N documents for a JSON-serializable request.
Source§

fn top_n_ids<'a>( &'a self, req: VectorSearchRequest<CoreFilter<Value>>, ) -> WasmBoxedFuture<'a, Result<Vec<(f64, String)>, VectorStoreError>>

Returns only the top N document IDs for a JSON-serializable request.

Auto Trait Implementations§

§

impl<M> !RefUnwindSafe for RedisVectorStore<M>

§

impl<M> !UnwindSafe for RedisVectorStore<M>

§

impl<M> Freeze for RedisVectorStore<M>
where M: Freeze,

§

impl<M> Send for RedisVectorStore<M>

§

impl<M> Sync for RedisVectorStore<M>

§

impl<M> Unpin for RedisVectorStore<M>
where M: Unpin,

§

impl<M> UnsafeUnpin for RedisVectorStore<M>
where M: UnsafeUnpin,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<I, F> VectorStoreIndexDyn for I
where I: VectorStoreIndex<Filter = F>, F: Debug + Clone + SearchFilter<Value = Value> + WasmCompatSend + WasmCompatSync + Serialize + for<'de> Deserialize<'de> + 'static,

Source§

fn top_n<'a>( &'a self, req: VectorSearchRequest, ) -> Pin<Box<dyn Future<Output = Result<Vec<(f64, String, Value)>, VectorStoreError>> + Send + 'a>>

Returns the top N documents for a JSON-serializable request.
Source§

fn top_n_ids<'a>( &'a self, req: VectorSearchRequest, ) -> Pin<Box<dyn Future<Output = Result<Vec<(f64, String)>, VectorStoreError>> + Send + 'a>>

Returns only the top N document IDs for a JSON-serializable request.
Source§

impl<T> WasmCompatSend for T
where T: Send,

Source§

impl<T> WasmCompatSync for T
where T: Sync,

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more