RU
Home · RAG: Retrieval + Generation · Updated

Vector Search and ANN Indexes

Vector search for RAG: brute-force vs ANN (HNSW, IVF), the recall/latency trade-off and typical AI interview questions.

Intuition

Once texts are turned into embeddings, “find similar by meaning” = “find the nearest points in a high-dimensional space”. Closeness is measured three ways: cosine similarity (the angle between vectors), the dot product and Euclidean distance. They sound like three different worlds, but if the vectors are normalized to unit length, all three metrics order neighbors almost the same way — so the “cosine or Euclidean” debate on normalized embeddings is nearly moot.

Exact search — compare the query with each of N vectors of dimension d:

cost ≈ O(N × d) per query

For 10 thousand vectors — instant. For 100 million at d = 1024 — already hundreds of billions of operations per query. Does not scale.

How it works: ANN

ANN (Approximate Nearest Neighbors) is a deliberate trade: we search not for guaranteed nearest neighbors, but for “almost certainly nearest” ones, orders of magnitude faster. Index quality is measured by recall@k — what fraction of the true top-k neighbors was found. Recall 0.95 means: on average 19 of 20 true neighbors are present.

A separate pain is metadata filters (“search only among department X documents”). Naively: find top-k, then filter — but the filter can knock out almost everyone, leaving two results out of k. So mature engines support filtering inside the index walk (filter-aware search).

💡 Key idea ANN is always a trade: recall against speed and memory. Knobs like nprobe in IVF move the system along that curve; they do not give a “free” speedup.

What to choose in practice

Choosing an engine is a question of scale and stack, not fashion:

A typical design mistake is dragging a dedicated vector DB into a 50-thousand-document project “for growth”: the operational complexity is real today, and the scale may never arrive.

⚠️ Caution Index recall is measured against exact search on your data and your queries. Default settings that give 0.99 on a benchmark may give 0.7 on your distribution — especially with metadata filters. Measure yourself: take a sample of queries, compute an honest top-k by brute force and compare.
🎤 Interview
  • “Cosine or Euclidean?” — on normalized vectors the ranking nearly coincides; what matters more is which metric the embedding model was trained with and the index was built with.
  • “Explain HNSW in plain words” — a multilayer graph: “highways” on top for long jumps, “streets” below for fine tuning; greedy descent through the layers.
  • “What do you tweak in IVF if recall is low?” — raise nprobe (slower, more complete) or the number/quality of centroids; check queries near cell borders.
  • “How does ANN coexist with metadata filters?” — post-filtering can leave fewer than k results; you need an engine with filtering inside the index walk.

All interview questions in this section: RAG: Retrieval + Generation — interview questions