Using RedisNum Filter Fields in LangChain

Posted: Feb 10, 2025.

Redis vector store offers powerful filtering capabilities when performing similarity searches. In this guide, we'll explore how to use the RedisNum filter field to handle numeric data in your vector store.

What is RedisNum?

RedisNum is a filter field class in LangChain that represents numeric fields in a Redis index. It allows you to define numeric fields that can be used for filtering vector search results based on numerical values. This is particularly useful when you need to filter documents based on numeric metadata like timestamps, prices, or any other numerical attributes.

Reference

MethodDescription
init(field: str)Initializes a RedisNum filter field with the given field name
equals(other: RedisFilterField) -> boolCompares this RedisNum field with another RedisFilterField for equality

How to use RedisNum

Basic Initialization

To use RedisNum, you first need to define it as part of your Redis index schema:

from langchain_redis import RedisConfig
from langchain_community.vectorstores.redis.filters import RedisNum

# Define config with numeric fields
config = RedisConfig(
    index_name="documents",
    redis_url="redis://localhost:6379",
    metadata_schema=[
        {
            "name": "price",  
            "type": RedisNum,
        }
    ]
)

Using with Vector Store Filtering

You can use RedisNum fields to filter your vector searches:

from redisvl.query.filter import Num

# Create a numeric filter condition
price_filter = Num("price") > 100

# Perform similarity search with numeric filter
results = vector_store.similarity_search(
    "query text",
    k=5,
    filter=price_filter
)

Complex Numeric Filters

RedisNum supports various numeric operations for filtering:

from redisvl.query.filter import Num

# Greater than or equal
filter_gte = Num("price") >= 50

# Less than
filter_lt = Num("price") < 1000

# Between range (combining filters)
filter_range = (Num("price") >= 50) & (Num("price") <= 1000)

# Multiple numeric fields
filter_complex = (Num("price") < 100) & (Num("quantity") > 0)

# Use filters in similarity search
results = vector_store.similarity_search(
    "query text",
    k=5,
    filter=filter_range
)

Using with Metadata

When adding documents to your vector store, you can include numeric metadata that corresponds to your RedisNum fields:

texts = [
    "Product A description",
    "Product B description"
]

metadata = [
    {"price": 99.99, "quantity": 50},
    {"price": 149.99, "quantity": 30}
]

vector_store.add_texts(
    texts=texts,
    metadatas=metadata
)

RedisNum is a powerful tool for handling numeric data in your Redis vector store indexes. By properly defining numeric fields and using them in filters, you can create sophisticated search queries that combine semantic similarity with numeric constraints.

An alternative to LangSmith

Open-source LangChain monitoring, prompt management, and magic. Get started in 2 minutes.

LangChain Docs

Join 10,000+ subscribers

Every 2 weeks, latest model releases and industry news.

An alternative to LangSmith

Open-source LangChain monitoring, prompt management, and magic. Get started in 2 minutes.

LangChain Docs