You want to find the K most similar items to a query embedding, optionally filtered by metadata.
- An index with a
VECTORfield, e.g.:FT.CREATE products ON HASH PREFIX 1 product: SCHEMA title TEXT category TAG embedding VECTOR FLAT 6 TYPE FLOAT32 DIM 1536 DISTANCE_METRIC COSINE - Embeddings stored as
FLOAT32byte arrays in theembeddingfield.
import struct
query_vec = struct.pack(f"{len(embedding)}f", *embedding)
result = executor.execute(
"""
SELECT title, vector_distance(embedding, :vec) AS score
FROM products
LIMIT 5
""",
params={"vec": query_vec},
)vector_distance(field, :param) is the function that triggers a KNN search. The LIMIT becomes the K value.
Combine a WHERE clause with vector_distance:
result = executor.execute(
"""
SELECT title, vector_distance(embedding, :vec) AS score
FROM products
WHERE category = 'electronics' AND price < 1000
LIMIT 5
""",
params={"vec": query_vec},
)The filter narrows the candidate set; the KNN runs over what survives.
vector_distance(...) AS alias is required for the score to come back as a column. The result rows include the alias as a key.