advanced

Similarity search

Retrieve nearest vectors with distance metrics, filtering, reranking, and relevance evaluation.

Similarity search retrieves the k nearest vectors to a query embedding using a distance or similarity function. Common metrics: cosine similarity (angle, scale-invariant when normalized), dot product (fast when vectors are normalized), Euclidean (L2) distance.

					query q → index.findNearest(q, k=10, filter={ tenant: "acme" })
→ candidates → optional reranker (cross-encoder) → final top-k
				

Production pipelines often combine ANN retrieval (fast, approximate) with a reranking stage (slower, more accurate) and metadata pre-filters for tenancy, date, or document type.

On interviews: define the metric and why it matches the model training; explain recall@k evaluation; describe filtering before vs after ANN and latency impact.

Common pitfalls: wrong metric for unnormalized vectors; no rerank when ANN confuses near-duplicates; returning chunks without source attribution; fixed k too small for RAG context assembly; ignoring cold-start for new documents.

The trade-off is latency and index size (ANN approximation) versus recall quality and the extra cost of reranking and human evaluation.

Checklist:

  • Name metric: cosine, dot, L2 and when each fits.
  • Describe ANN → filter → rerank pipeline.
  • Explain recall@k and offline eval sets.
  • State metadata filter strategy.
  • Connect results to downstream context limits.