how to use redis search in python
- User
how to use redis search in python
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
how to use redis search in python
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
To use Redis Search in Python, you'll typically be working with the RediSearch module (also known as RedisJSON + RediSearch), which allows full-text search, secondary indexing, and querying capabilities on Redis data.
Here's a step-by-step guide to using Redis Search in Python:
Redis must be running with RediSearch module loaded
docker run -p 6379:6379 redislabs/redisearch:latest
Install redis-py and redisearch-py
pip install redis redisearch
from redis import Redis
from redisearch import Client, Document, Query
# Connect to Redis server
client = Redis(host='localhost', port=6379, db=0)
# Create a RediSearch client for an index called "my-index"
rs_client = Client('my-index', conn=client)
You need to define a schema before adding documents.
from redisearch import TextField, NumericField
try:
rs_client.create_index([
TextField('title', weight=5.0),
TextField('body'),
NumericField('views')
])
except Exception as e:
print("Index might already exist:", e)
⚠️ Note: Creating an index is a one-time operation unless you're dropping it or starting fresh.
# Add a document to the index
doc = Document(
'doc1',
title='Hello World',
body='This is a test document.',
views=100
)
rs_client.add_document(doc)
# Search for documents containing "hello world"
query = Query('hello world').with_scores()
results = rs_client.search(query)
print(f"Found {results.total} results:")
for doc in results.docs:
print(f"ID: {doc.id}, Title: {doc.title}, Body: {doc.body}, Views: {doc.views}")
Filter by numeric fields or other conditions:
# Search with filter (e.g., views > 50)
query = Query('hello world').add_filter(NumericField('views') > 50).with_scores()
results = rs_client.search(query)
print(f"Filtered Results ({results.total}):")
for doc in results.docs:
print(f"{doc.title} | Views: {doc.views}")
Drop the index if needed:
rs_client.drop_index()
If you're storing complex JSON objects, consider using redisjson-py with RediSearch.
Install:
pip install redis redisjson redisearch
Example:
from redis import Redis
from redisjson import RedisJSON
from redisearch import Client, Query
# Redis connection
redis_client = Redis(host='localhost', port=6379)
# JSON handler
rj = RedisJSON(redis=redis_client)
# RediSearch client
rs_client = Client('json-index', conn=redis_client)
# Create index on JSON field
try:
rs_client.create_index([
TextField('$.title', as_name='title'),
TextField('$.content', as_name='content')
])
except Exception as e:
print("Index error:", e)
# Store JSON document
rj.set('doc:1', '$', {
'title': 'Redis Search Guide',
'content': 'How to use Redis Search with Python'
})
# Search
query = Query('Redis Search').return_fields('title', 'content')
result = rs_client.search(query)
for doc in result.docs:
print(doc.title, "=>", doc.content)
Let me know if you want to see how to integrate this with FastAPI, Django, or any ORM!