RedisVectorStore
features and configurations head to the API reference.
Overview
Integration details
Class | Package | PY support | Version |
---|---|---|---|
RedisVectorStore | @langchain/redis | ✅ |
Setup
To use Redis vector stores, you’ll need to set up a Redis instance and install the@langchain/redis
integration package. You can also install the node-redis
package to initialize the vector store with a specific client instance.
This guide will also use OpenAI embeddings, which require you to install the @langchain/openai
integration package. You can also use other supported embeddings models if you wish.
Credentials
Once you’ve set up an instance, set theREDIS_URL
environment variable:
Instantiation
Manage vector store
Add items to vector store
Query vector store
Once your vector store has been created and the relevant documents have been added you will most likely wish to query it during the running of your chain or agent.Query directly
Performing a simple similarity search can be done as follows:Query by turning into retriever
You can also transform the vector store into a retriever for easier usage in your chains.Usage for retrieval-augmented generation
For guides on how to use this vector store for retrieval-augmented generation (RAG), see the following sections:Deleting documents
You can delete documents from the vector store in two ways:Delete all documents
You can delete an entire index and all its documents with the following command:Delete specific documents by ID
You can also delete specific documents by providing their IDs. Note that the configured key prefix will be automatically added to the IDs you provide:Closing connections
Make sure you close the client connection when you are finished to avoid excessive resource consumption:Advanced Features
Custom Schema and Metadata Filtering
The Redis vector store supports custom schema definitions for metadata fields, enabling more efficient filtering and searching. This feature allows you to define specific field types and validation rules for your metadata.Defining a Custom Schema
You can define a custom schema when creating your vector store to specify field types, validation rules, and indexing options:Schema Field Types
The custom schema supports three main field types:- TEXT: Full-text searchable fields with optional stemming, weighting, and sorting
- TAG: Categorical fields for exact matching, with support for multiple values and custom separators
- NUMERIC: Numeric fields supporting range queries and sorting
Field Configuration Options
Each field can be configured with various options:required
: Whether the field must be present in metadata (default: false)SORTABLE
: Enable sorting on this field (default: undefined)SEPARATOR
: For TAG fields, specify the separator for multiple values (default: ”,”)CASESENSITIVE
: For TAG fields, enable case-sensitive matching (Redis expectstrue
, not boolean)NOSTEM
: For TEXT fields, disable stemming (Redis expectstrue
, not boolean)WEIGHT
: For TEXT fields, specify search weight (default: 1.0)
Adding Documents with Schema Validation
When using a custom schema, documents are automatically validated against the defined schema:Advanced Similarity Search with Metadata Filtering
The custom schema enables powerful metadata filtering capabilities using thesimilaritySearchVectorWithScoreAndMetadata
method:
Numeric Range Query Options
For numeric fields, you can specify various range queries:Error Handling and Validation
The custom schema provides automatic validation with helpful error messages:Performance Benefits
Using custom schema provides several performance advantages:- Indexed Metadata Fields: Individual metadata fields are indexed separately, enabling fast filtering
- Type-Optimized Queries: Numeric and tag fields use optimized query structures
- Reduced Data Transfer: Only relevant fields are returned in search results
- Better Query Planning: Redis can optimize queries based on field types and indexes
Backward Compatibility
The custom schema feature is fully backward compatible. Existing Redis vector stores without custom schemas will continue to work exactly as before. You can gradually migrate to custom schemas for new indexes or when rebuilding existing ones.API reference
For detailed documentation of allRedisVectorSearch
features and configurations head to the API reference.