liveBook · Manning

Redis Search

As explained in Generative AI in Action

Redis Search is a Redis module that provides text search, secondary indexing, and vector similarity search capabilities.

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

How Generative AI in Action covers it

How deep it goes
Shows local setup and index creation with field types and vector settings, without developing the search algorithms mathematically.
Examples
Uses a Redis Stack Docker container and a Python-defined blog-post index with tag, text, and HNSW vector fields.
What it assumes
Requires Docker installed and running locally, plus redis-py for the Python connection; does not teach Docker installation.
What you can do after
Build it from scratch: create and inspect a Redis Search index for blog-post text, metadata, and embeddings using redis-py.
How it connects
Connects Redis modules, RAG, embeddings, full-text search, secondary indexing, and nearest-neighbor search.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Generative AI in Action. The section links point to the parts of the book it draws on.