Franck PachotDatabase Developer Advocate Minibook 25 · Database field guides All minibooks

Database field guide · 25

Vector Search

Embeddings, ANN indexes, filters, and hybrid retrieval

Build and evaluate semantic retrieval with vector representations, approximate indexes, metadata filtering, and full-text hybrid ranking.

Franck Pachot7 chaptersembeddings · ANN · HNSW · hybrid search

Build the mental model before choosing the mechanism.

This minibook was AI-generated from Franck Pachot's archived blog posts. Links to the original articles are included for source context and verification.

01

Embeddings turn meaning into geometry

An embedding model maps content into fixed-length vectors where a distance function approximates semantic similarity. Query and corpus vectors must share the same model and normalization contract.

  • Model version is part of stored-data provenance.
  • Cosine, inner product, and Euclidean distance are not interchangeable by accident.
  • Chunking determines what one result can mean.

02

Exact search defines the baseline

Brute-force distance over every eligible vector provides a correctness reference. Approximate nearest-neighbor indexes reduce latency by exploring only a promising subset.

  • Measure ANN recall against exact top-k results.
  • Dataset size and dimensionality shape the crossover point.
  • Index build time and memory belong in the cost model.

03

ANN indexes expose quality controls

Graph and partition-based indexes tune search breadth, construction effort, memory, and recall. Higher search effort usually improves quality while increasing latency.

  • HNSW navigates a layered proximity graph.
  • DiskANN-style designs optimize graph access for secondary storage.
  • Defaults are starting points, not workload guarantees.

04

Filtering changes the search space

Metadata predicates can run before, during, or after ANN traversal. Post-filtering may return fewer than k rows and reduce recall when many visited candidates are ineligible.

  • Evaluate recall after applying the business filter.
  • Selective filters need index-aware traversal or oversampling.
  • Partitioning by metadata can improve one filter and fragment others.

05

Hybrid search combines distinct evidence

Full-text ranking captures lexical precision while vector similarity captures semantic proximity. Hybrid retrieval normalizes and fuses ranks rather than pretending both scores share one scale.

  • Reciprocal-rank fusion is robust without score calibration.
  • Retain component ranks for explanation and tuning.
  • Candidate generation and reranking are separate stages.

06

Evaluation starts with relevant judgments

Latency and throughput cannot establish retrieval quality. A representative query set with expected relevant documents supports recall, precision, ranking, and regression measurements.

  • Measure at the final requested k.
  • Include rare filters and difficult negatives.
  • Re-embed or version indexes when the model changes.

07

Field manual

Concrete mechanics, diagnostic evidence, and executable patterns to carry into a real system.

01

Create an exact quality baseline

Keep a representative query set and compute exact nearest neighbors before tuning ANN. The baseline lets recall changes be measured rather than inferred from plausible-looking results.

SELECT id, embedding <=> $1 AS cosine_distance
FROM article
WHERE tenant_id = $2
ORDER BY embedding <=> $1
LIMIT 10;
-- Run without an ANN path for the labeled evaluation sample.
02

Measure recall at k

Recall compares relevant exact neighbors recovered by ANN. Report its distribution across queries and filters alongside latency percentiles.

recall_at_k = len(set(exact_top_k) & set(ann_top_k)) / k
# Report mean, p5, and worst cohorts by filter selectivity.
03

Test filtering explicitly

A selective predicate can starve post-filtered ANN results. Compare returned row count and recall while increasing traversal effort or using an index that incorporates filtering.

SET LOCAL hnsw.ef_search = 100;
SELECT id FROM article
WHERE category = $1
ORDER BY embedding <=> $2
LIMIT 20;
-- Sweep ef_search and category selectivity; record recall and latency.
04

Fuse lexical and semantic ranks

Reciprocal-rank fusion combines independent ranked lists without assuming text and vector scores have comparable scales.

rrf_score = 1 / (60 + text_rank) + 1 / (60 + vector_rank)
# Union candidate IDs, assign missing ranks no contribution,
# sort by rrf_score, and retain both ranks for diagnosis.

08

Source articles

Optional deep dives with the complete experiments and product-version context behind this guide.