Cosine similarity helps a semantic search system find documents related to a query. It measures the cosine of the angle between two vectors, comparing their direction regardless of magnitude. This makes it useful for text embeddings when direction matters more than vector length. section 8.3 section 7.4
Similarity ranges from −1 for opposite directions to 1 for the same direction. Cosine similarity is commonly used with normalized embeddings; for normalized vectors, the dot product is equivalent. Cosine is less suitable when vector magnitude is important. Choose a metric that fits the data and query needs, and use the metric associated with the embedding model. section 7.5.2
In a retrieval-augmented generation workflow, documents can be split into chunks, embedded, and stored with metadata. A retriever compares a query embedding with stored embeddings to find relevant content. A vector index supports fast nearest-neighbor lookup; its design balances speed, accuracy, and memory consumption. section 7.4 section 7.5.1
Find related posts
A Redis search application converts “Tell me about Longhorn” into a vector, then uses a K-nearest-neighbors query to retrieve the three most similar posts. The results include fields such as the URL, title, content, and vector score. section 8.5
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 Redis schema configures an HNSW embedding field with FLOAT32 values, 1,536 dimensions, and the COSINE distance metric. The dimensions must match the embedding model. section 8.3















