Skip to main content
Compatibility: Only available on Node.js.
Redis is a fast open source, in-memory data store. As part of the Redis Stack, RediSearch is the module that enables vector similarity semantic search, as well as many other types of searching. This guide provides a quick overview for getting started with Redis vector stores. For detailed documentation of all RedisVectorStore features and configurations head to the API reference.

Overview

Integration details

Setup

To use Redis vector stores, set up a Redis Stack instance with RediSearch enabled and install @langchain/redis and @langchain/core. Install the redis Node.js client when you pass your own createClient instance to RedisVectorStore. This guide uses OpenAI embeddings as an example. You can use other supported embeddings models instead.
You can set up a Redis instance locally with Docker by following these instructions.

Credentials

Set the REDIS_URL environment variable:
If you are using OpenAI embeddings for this guide, set your OpenAI key as well:
If you want to get automated tracing of your model calls you can also set your LangSmith API key by uncommenting below:

Instantiation

Manage vector store

Add items to vector store

Top-level document ids are currently not supported, but you can delete documents by providing their IDs directly to the 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:
Filtering will currently look for any metadata key containing the provided string. If you want to execute a similarity search and receive the corresponding scores you can run:

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 expects true, not boolean)
  • NOSTEM: For TEXT fields, disable stemming (Redis expects true, 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 the similaritySearchVectorWithScoreAndMetadata 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:
  1. Indexed Metadata Fields: Individual metadata fields are indexed separately, enabling fast filtering
  2. Type-Optimized Queries: Numeric and tag fields use optimized query structures
  3. Reduced Data Transfer: Only relevant fields are returned in search results
  4. 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 all RedisVectorStore features and configurations head to the API reference.