Redis Vector Search
Vector search finds items by meaning instead of exact words. A search for "cheap laptop for students" can match a page titled "Budget notebooks for college" because both carry a similar meaning. Redis stores the numbers that represent meaning and finds the closest matches quickly. Modern search, recommendations, and AI assistants use this technique.
What Is a Vector?
A vector is a list of numbers. An AI model called an embedding model reads a piece of text, an image, or a product and produces a vector for it. Similar items receive vectors that sit close together. Real embeddings hold hundreds or thousands of numbers. This topic uses four numbers to keep the examples readable.
"puppy" → [0.90, 0.10, 0.80, 0.05] "dog" → [0.88, 0.12, 0.79, 0.07] ← close to puppy "invoice" → [0.05, 0.92, 0.10, 0.85] ← far from puppy
The Map Analogy
Picture a huge map where every item has a pin. Pins for similar items cluster together. Searching means placing a new pin on the map and asking for the nearest neighbors.
●cat ●dog ▲ your query "puppy"
●puppy ◄── nearest ──────────┘
●invoice ●receipt
(a different cluster)
Measuring Closeness
Metric Meaning Common Use ──────── ─────────────────────────────────────── ────────────────── COSINE Compares the direction of two vectors Text embeddings L2 Straight-line distance Image features IP Inner product (dot product) Recommendations
Smaller cosine or L2 distance means a closer match.
Creating a Vector Index
127.0.0.1:6379> FT.CREATE idx:docs ON HASH PREFIX 1 doc: SCHEMA title TEXT embedding VECTOR HNSW 6 TYPE FLOAT32 DIM 4 DISTANCE_METRIC COSINE OK
- HNSW picks a fast approximate search method. FLAT offers exact search for small data sets.
- TYPE FLOAT32 sets each number to a 32-bit decimal.
- DIM 4 sets how many numbers each vector holds. Match this to your embedding model.
- DISTANCE_METRIC sets the closeness rule.
Storing Vectors
Redis stores a vector in a Hash field as raw bytes. Application code converts the numbers to bytes.
import numpy as np, redis
r = redis.Redis()
vec = np.array([0.90, 0.10, 0.80, 0.05], dtype=np.float32)
r.hset("doc:1", mapping={"title": "Puppy care guide", "embedding": vec.tobytes()})
Running a KNN Query
KNN stands for K nearest neighbors. The query asks for the K closest items.
query = np.array([0.88, 0.12, 0.79, 0.07], dtype=np.float32).tobytes()
r.execute_command(
"FT.SEARCH", "idx:docs",
"*=>[KNN 3 @embedding $vec AS score]",
"PARAMS", "2", "vec", query,
"SORTBY", "score",
"DIALECT", "2"
)
Redis returns the three closest documents with a score for each. A lower score means a nearer match.
Combining Filters with Meaning
Put a filter before the arrow to narrow candidates first.
"@category:{guides}=>[KNN 3 @embedding $vec AS score]"
This query searches only inside the guides category.
Vector Sets (Beta)
Redis 8 adds a data type called vector set for simple similarity search without an index definition. It is in beta, and its behavior may change.
127.0.0.1:6379> VADD pets VALUES 4 0.90 0.10 0.80 0.05 "puppy" (integer) 1 127.0.0.1:6379> VSIM pets VALUES 4 0.88 0.12 0.79 0.07 COUNT 2 1) "puppy"
Practical Example – Help Center Assistant
Setup: An embedding model converts each help article into a vector.
Redis stores the vectors with FT.CREATE and HSET.
Search: A visitor types "my payment did not go through".
The same model converts the question into a vector.
Redis runs KNN 3 and returns the three nearest articles.
Result: Articles titled "Payment failed" and "Card declined" appear,
even though none contains the visitor's exact words.
Key Points
- Embeddings turn meaning into lists of numbers.
- Redis stores vectors in Hashes or JSON and indexes them with FT.CREATE.
- KNN queries return the closest items.
- Use the same embedding model for stored data and queries.
- Vector set is a beta type, so test it carefully before relying on it.
