← Master Index
Vol. 14 Module 14.1 Lecture

Similarity Search

RAG Core Concepts

How This Lesson Fits the Module & Volume

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.
Definition

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

MetricIntuitionRAG note
CosineAngle between vectorsDefault for many ST models
Inner productAligned magnitude + angleCommon with normalized or dual encoders
L2 distanceGeometric distanceUsed by some indexes / models
BM25Lexical relevanceSparse 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

import numpy as np def cosine(a: np.ndarray, b: np.ndarray) -> float: return float(a @ b) / (np.linalg.norm(a) * np.linalg.norm(b) + 1e-12) def similarity_search(query_vec, doc_vecs, k=3, metric=cosine): scores = [metric(query_vec, v) for v in doc_vecs] order = np.argsort(scores)[::-1][:k] return [(int(i), float(scores[i])) for i in order] # Example with random unit-ish vectors rng = np.random.default_rng(0) docs = rng.normal(size=(5, 8)) q = rng.normal(size=8) print(similarity_search(q, docs, k=2))

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
Common Misconception

“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

  1. Short Answer: Formal goal of similarity search? Answer: Find items maximizing sim(q, x) (or minimizing distance).
  2. True/False: BM25 can be viewed as a sparse similarity score. Answer: True.
  3. Multiple Choice: Top-k search: (a) always returns k items (if N≥k), (b) returns CSS, (c) needs no scores. Answer: (a).
  4. Short Answer: Name two vector metrics. Answer: Cosine, inner product, L2 (any two).
  5. True/False: Similarity always equals business relevance. Answer: False.
  6. Multiple Choice: Threshold search can return: (a) zero hits, (b) only images, (c) infinite k. Answer: (a).
  7. Short Answer: Why unify sparse and dense under “similarity”? Answer: Both produce ranked lists you can fuse/evaluate.
  8. True/False: Metric choice is independent of embedding training. Answer: False—they should match.
  9. Multiple Choice: Next lecture: (a) Cosine Similarity, (b) Vol. 1 only, (c) printers. Answer: (a).
  10. 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.
Trainer’s Guide

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.