advanced33 min studyLesson 49 in Engineering

Vector indexes and retrieval

Define a distance metric, establish an exact baseline, and evaluate approximate retrieval under filters and changing data.

On this page

What you’ll understand

  • Relate cosine and Euclidean ranking under an explicit normalization assumption.
  • Calculate recall against exact neighbors while separating it from relevance.
  • Evaluate approximation, filtering, index freshness, memory, and tail latency together.

A retrieval service maps documents and queries into vectors, then asks which stored vectors are nearest. That question becomes useful only after specifying the embedding version, distance function, candidate population, and access policy. A fast approximate index can miss the right neighbors, while perfect nearest-neighbor search can return semantically irrelevant documents. Treat geometric accuracy and application usefulness as separate measurements, and keep an exact baseline small enough to inspect.

Define the geometric question first

An embedding is a coordinate representation produced by a particular model and preprocessing pipeline. Mixing incompatible model versions in one index can make distances meaningless even if dimensions match. Cosine similarity divides the dot product by both vector norms; Euclidean distance measures coordinate displacement. For nonzero unit-normalized vectors, maximizing cosine similarity is equivalent to minimizing squared Euclidean distance. Without that normalization assumption, norm differences can change the ranking substantially.

An exact scan computes the chosen distance against every eligible vector and selects the best k. It supplies a reference neighbor set for index accuracy, with a documented tie policy. Estimate its arithmetic and memory traffic before dismissing it: a small filtered tenant subset may be cheap enough for exact search. Conversely, a large unfiltered corpus can be dominated by reading vectors even when each distance calculation is simple. Quantization changes numerical representation and introduces another approximation to evaluate.

∥u−v∥22=2−2u⊤vif ∥u∥2=∥v∥2=1\|u-v\|_2^2=2-2u^\top v\quad\text{if }\|u\|_2=\|v\|_2=1
For unit vectors the squared Euclidean distance decreases as their dot product, which equals cosine similarity, increases. Nonzero vectors must be normalized consistently before relying on this ranking equivalence.↔ Scroll wide equations horizontally.

Approximate search trades work for missed neighbors

Graph-based HNSW search navigates a layered proximity graph rather than scanning all vectors. Its search breadth and construction choices affect recall, memory, build cost, and latency. There is no general guarantee that the first approximate answer is the exact nearest point. Other families partition the space or compress vectors, spending different resources. Compare configurations at matched accuracy and workload.

Define recall at k against exact top-k neighbors for the same metric and eligible corpus. That geometric recall is not user relevance, answer faithfulness, or permission correctness. Measure those downstream outcomes separately with appropriate labels. Stratify by rare topics, new documents, and challenging filters rather than reporting one mean. A retrieval system can improve average recall while missing a small tenant's important documents, especially if the benchmark mostly reflects the largest and easiest populations.

Make filters and lifecycle part of evaluation

Filtering after an approximate candidate search can leave fewer than k eligible results, even when many eligible documents exist elsewhere. Increasing search effort, iterating the scan, prefiltering with a suitable plan, or partitioning the index may help depending on the engine. Authorization must still be enforced on returned documents; an embedding similarity score never grants access. Evaluate the actual query plan and parameter settings because merely creating a vector index does not ensure every query uses it.

A production index must represent inserts, updates, and deletions within a declared freshness bound. Record embedding version, source revision, deletion status, and rebuild generation. Account for vector bytes and graph overhead as well as database tuples and build scratch space. During a model upgrade, compare old and new retrieval quality in isolated generations and establish how readers switch. Test update-heavy operation and post-filter tail latency, not only a static build benchmark with a warm cache.

Recall⁡@k=∣Ak∩Ek∣k,Mvectors=Ndb\operatorname{Recall}@k=\frac{|A_k\cap E_k|}{k},\qquad M_{\mathrm{vectors}}=Ndb
Ek is an exact top-k set and Ak an approximate result set for the same eligible corpus and tie convention. N vectors of dimension d with b bytes per component require Ndb raw bytes, excluding graph and storage overhead.↔ Scroll wide equations horizontally.

Read a retrieval benchmark honestly

For one query, an approximate top-10 contains seven of the exact top-10 neighbors. The corpus holds one million 768-dimensional float32 vectors. Use four bytes per component and decimal units.

  1. Geometric recall@10 is 7/10 = 0.7, or 70%, using identical filtering, metric, and tie handling for both searches.

  2. Raw vector bytes are 1,000,000 × 768 × 4 = 3,072,000,000 bytes, or 3.072 GB.

  3. The actual index needs additional graph links, tuple metadata, and storage overhead; 3.072 GB is not its full memory requirement.

  4. If only three returned neighbors are judged useful, that is a different relevance measurement. Improve and evaluate the embedding or ranking separately from approximate-neighbor recall.

The result

This run establishes 70% geometric recall for one query and a 3.072 GB raw-vector lower accounting component, not end-to-end retrieval quality.

Explore the model

Interactive lab

Interactive lab · vectors

Inspect direction and magnitude separately

Adjust angle and magnitude for one 2D vector and inspect its x/y projections. Use direction changes to reason about angular similarity and magnitude changes to question normalization assumptions. The lab has no query vector, nearest-neighbor corpus, cosine-distance readout, or HNSW implementation.

Inspect direction and magnitude separatelyPlayback traces a fixed vector and its projections. Angle and magnitude set the geometry; the moving marker does not change them. Current readings: x component: 1.393; y component: 0.975; Magnitude: 1.7. All parameters are adjustable with the labeled controls below.−7.46−3.7303.737.46−3.5−1.7501.753.5xyvvₓ = 1.39
Vectorx / y projectionsTrace marker
x component
1.393
y component
0.975
Magnitude
1.7
t = 0 s

Playback traces a fixed vector and its projections. Angle and magnitude set the geometry; the moving marker does not change them. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.

Look forDirection and length are distinct geometric properties; a retrieval metric must say which properties affect ranking.

Check your understanding

An approximate search returns ten vectors, but a tenant filter leaves only two. What is the sound interpretation?

Try it yourself

Two unit vectors have dot product 0.8. Find their squared Euclidean distance. A top-5 approximate result shares four items with the exact top-5: find recall and state what it does not establish.

Take a moment to make a prediction or work it out on paper. Open a hint when you need one.

Give me a hint

Use the unit-vector identity and count set intersection, keeping geometric quality separate from human relevance.

Compare with the solution

Squared Euclidean distance is 2 − 2(0.8) = 0.4. Recall@5 is 4/5 = 80% under the same eligible corpus and tie policy. That does not establish that users find the documents relevant, that generated answers are supported, that authorization is enforced, or that other queries achieve the same recall. Evaluate those requirements separately and across a representative query distribution.

Take it with you

  • Embedding identity, normalization, metric, and eligible population define the search problem.
  • Approximate-neighbor recall is measured against exact neighbors and differs from relevance.
  • Filters, fresh updates, deletion, memory overhead, and latency belong in the benchmark.

Read further

Original sources and deeper explanations to follow at your own pace.

Optional progress, saved in this browser when available. Revisit any lesson at any time.

Lesson not marked complete.