Retrieval-Augmented Generation104 · Module II · Lesson 05 of 13
Article · 12 min

Similarity search

Cosine, dot product, and the distance you actually want.

Summary

Similarity search: given a query vector, return the k nearest vectors from a corpus. The math is old (nearest neighbors), but at 10M+ vectors, exact search is too slow. Approximate nearest neighbor (ANN) algorithms — HNSW, IVF, DiskANN — trade a little recall for a lot of speed.

Objectives
  • 01State the three distance metrics and when to use each.
  • 02Explain the recall-vs-latency tradeoff in ANN.
  • 03Describe HNSW at a high level.
The Lesson

Distance metrics

Cosine similarity for normalized embeddings (the common case). Dot product when magnitudes matter. Euclidean when the embedding was trained with Euclidean loss. Use whatever the embedding model was trained with — cosine for OpenAI's text-embedding-3, for example.

ANN algorithms

HNSW builds a multi-layer graph; queries traverse from top to bottom. Fast, memory-hungry, the modern default. IVF partitions the space with k-means; queries probe a few partitions. DiskANN stays on disk for corpuses that do not fit in RAM.

Key Ideas
  • Use the distance the embedding was trained with.
  • HNSW is the default; DiskANN when corpuses exceed RAM.