Vector search is one instance of a broader idea: similarity search—find items closest to a query under a similarity (or distance) function. In RAG that usually means dense cosine/IP, but the same framing covers sparse BM25 scores and multimodal embeddings. Next we zoom into cosine similarity as the workhorse metric.
Learning Objectives
By the end of this lesson, students should be able to:
- Define similarity search formally as argmax ranking by sim(q, x).
- Map common metrics: cosine, inner product, Euclidean, BM25.
- Explain top-k vs radius/threshold search modes.
- Relate similarity search to both dense and sparse RAG retrievers.
- Identify when similarity ≠ task relevance (popularity bias, length bias).
- Prepare for metric-specific pitfalls covered in the cosine lecture.
Similarity search retrieves the items in a collection that maximize a similarity function (or minimize a distance) with respect to a query—commonly returning the top-k highest-scoring results.
Metric Menu
| Metric | Intuition | RAG note |
|---|---|---|
| Cosine | Angle between vectors | Default for many ST models |
| Inner product | Aligned magnitude + angle | Common with normalized or dual encoders |
| L2 distance | Geometric distance | Used by some indexes / models |
| BM25 | Lexical relevance | Sparse similarity analog |
Top-k
- Always return k
- Simple packing
- May include junk
Threshold
- Score floor
- Can return none
- Needs calibration
Hybrid rank
- Fuse metrics
- RRF / weighted
- See hybrid lecture
Code: Generic Top-k by Similarity
Strengths
- Unified mental model across retrievers
- Composable with filters & fusion
- Eval-friendly ranked lists
Tradeoffs
- Similarity ≠ user utility always
- Metric mismatch silently fails
- Thresholds need careful tuning
“The nearest neighbor is always the best evidence.” Nearest may be a near-duplicate FAQ that does not answer the question, or a popular but outdated policy. Similarity search proposes candidates; task relevance still needs eval, filters, and often re-ranking.
Knowledge Check
- Short Answer: Formal goal of similarity search? Answer: Find items maximizing sim(q, x) (or minimizing distance).
- True/False: BM25 can be viewed as a sparse similarity score. Answer: True.
- Multiple Choice: Top-k search: (a) always returns k items (if N≥k), (b) returns CSS, (c) needs no scores. Answer: (a).
- Short Answer: Name two vector metrics. Answer: Cosine, inner product, L2 (any two).
- True/False: Similarity always equals business relevance. Answer: False.
- Multiple Choice: Threshold search can return: (a) zero hits, (b) only images, (c) infinite k. Answer: (a).
- Short Answer: Why unify sparse and dense under “similarity”? Answer: Both produce ranked lists you can fuse/evaluate.
- True/False: Metric choice is independent of embedding training. Answer: False—they should match.
- Multiple Choice: Next lecture: (a) Cosine Similarity, (b) Vol. 1 only, (c) printers. Answer: (a).
- Short Answer: What bias can inflate similarity? Answer: Length bias, popularity near-duplicates, or domain skew (any).
Key Takeaways
- Similarity search ranks items by a chosen sim/distance function.
- Dense and sparse RAG both produce ranked candidate lists.
- Nearest ≠ always best evidence—evaluate and re-rank.
- Next: Cosine Similarity.
Lab: Rank the same 10 docs with cosine vs a toy lexical overlap score; discuss disagreements.
Prompt: When is radius search better than fixed top-k for RAG?
Recap: Similarity search is the abstract engine behind RAG retrieval. Continue with Cosine Similarity.