A question-answering application needs to find relevant passages before it can answer from stored data. Redis Search adds text search, secondary indexes, and vector similarity search to Redis, making it useful for retrieving content by words, metadata, or embeddings in a retrieval-augmented generation (RAG) workflow. section 8.2
Start with a local Redis Stack container and a running Docker installation. Redis Stack includes the modules needed for this setup; Python can connect through redis-py. This is a quick development environment for learning the retrieval steps, not a deployment plan for production. section 8.2
Listing 8.1 docker-compose file for redis-stack
version: '3.7'
services:
vector-db:
image: redis/redis-stack:latest
ports:
- 6379:6379
- 8001:8001
environment:
- REDISEARCH_ARGS=CONCURRENT_WRITE_MODE
volumes:
- vector-db:/var/lib/redis
- ./redis.conf:/usr/local/etc/redis/redis.conf
healthcheck:
test: ["CMD", "redis-cli", "-h", "localhost", "-p", "6379", "ping"]
interval: 2s
timeout: 1m30s
retries: 5
start_period: 5s
volumes:
vector-db:
The Compose configuration starts Redis Stack, exposes Redis and Redis Insight ports, mounts a volume, and checks that Redis responds to ping. section 8.2
An index schema determines how each field can be searched. For blog posts, a tag field holds the URL, text fields hold the title, description, publication date, and content, and a vector field holds the embedding. Text fields support word and phrase searches; vector similarity supports searches for nearby embeddings. The example chooses HNSW, a graph-based nearest-neighbor approach, rather than the more straightforward brute-force FLAT approach. section 8.3
Listing 8.2 Redis index schema
SCHEMA = [
TagField("url"),
TextField("title"),
TextField("description"),
TextField("publish_date"),
TextField("content"),
VectorField("embedding", "HNSW",
{"TYPE": "FLOAT32",
"DIM": 1536,
"DISTANCE_METRIC": "COSINE"}),
]
The schema specifies an HNSW embedding field with FLOAT32 values, 1,536 dimensions, and COSINE distance alongside the searchable blog-post fields. section 8.3
Create the blog-post index
After connecting with redis-py, call `conn.ft(index_name).create_index(fields=schema, definition=IndexDefinition(prefix=["post:"], index_type=IndexType.HASH))`. This creates an index over hash keys beginning with `post:` using the chosen schema. Inspect the index details to check its definition and document count. section 8.3
Choose fields for the questions the application must answer, and configure the vector field for the embeddings it will store. Keep the local container setup distinct from decisions about hosting and access credentials. section 8.2 section 8.3
