SD System Design In Depth
0%
lesson system-designsearchranking

Vector search and hybrid retrieval

Module 5.4: Information Retrieval & Search Engines Updated Sep 17, 2026
📄 Cheat Sheet
Vector Database Search - Hierarchical Navigable Small Worlds (HNSW) Explained
DataMListic Vector Database Search - Hierarchical Navigable Small Worlds (HNSW) Explained Watch on YouTube ↗

Clear, focused visual explanation of HNSW layers and greedy search, the core index this unit teaches.

0:00 / --:--

The Semantic Revolution: Lexical vs Vector Search

BM25 matches literal keywords. If a user searches for "canine wellness clinic", BM25 fails to match a document titled "dog veterinarian hospital" because zero vocabulary terms overlap. Vector Search maps text into dense high-dimensional semantic vector spaces (e.g. 1536-dimensional embeddings generated by neural models) where semantically similar concepts cluster together.

HNSW (Hierarchical Navigable Small World) Graph Architecture

flowchart TD subgraph HNSWGraph ["Hierarchical Navigable Small World Graph (Skip-List for Vectors)"] Layer2["Layer 2: Sparse Long-Range Highway Links (Rapid Skip)"] Layer1["Layer 1: Medium Density Links"] Layer0["Layer 0: Dense Base Graph (All Vectors)"] Layer2 --> Layer1 --> Layer0 end QueryVec["Query Vector"] -->|"1. Greedy Entry at Layer 2"| Layer2 Layer0 -->|"2. Return K-Nearest Neighbors"| TopK["Top K Semantic Matches"]

Under the Hood: Hybrid Search Fusion (RRF)

Vector search excels at broad semantic concept matching but frequently fails on exact keyword identifiers (e.g. part numbers like "GTX-4090-TI" or specific product codes). The industry gold standard is Hybrid Search, combining lexical BM25 and dense vector search using Reciprocal Rank Fusion (RRF):

RRF_Score(d) = sum_{m in Models} ( 1 / (60 + Rank_m(d)) )

RRF normalizes disparate scoring scales into rank positions, ensuring that documents ranking well across both keyword matching and semantic embedding models float to the top.

Worked example: memory for 100 million vectors

ComponentCalculationSize
Raw float32 vectors, 768 dimensions100,000,000 x 768 x 4 bytesabout 307 GB
HNSW base-layer links, M = 162M = 32 neighbours x 4 bytes x 100,000,000about 12.8 GB, upper layers add a little more
Product-quantised codes, 96 bytes per vector100,000,000 x 96 bytesabout 9.6 GB
int8 scalar quantisation100,000,000 x 768 x 1 byteabout 77 GB

This is why large deployments shard vectors across many nodes and use quantised vectors for the graph search, then re-score the top few hundred candidates with full-precision vectors.

Worked example: reciprocal rank fusion

RRF score = sum over rankers of 1 divided by (60 + rank).

DocBM25 rankVector rankRRF score
X1201/61 + 1/80 = 0.0289
Y531/65 + 1/63 = 0.0313
Z2not returned1/62 = 0.0161

Y, which both systems like, wins over X, which only BM25 loves. The constant 60 comes from the original RRF paper (Cormack, Clarke and Buettcher, 2009) and dampens the advantage of a single first-place rank.

ANN index trade-offs

IndexSpeed and recallMemoryUpdates
Flat, brute forceExact, slow at scaleVectors onlyTrivial
HNSWExcellent recall at low latencyVectors plus graph, highestInserts fine; deletes are usually tombstoned
IVF with PQGood, tunable via nprobeVery compactNeeds training; periodic retraining
DiskANN-style graphsGood, SSD-residentSmall RAM footprintMore complex

Filtering pitfall: applying a restrictive filter such as tenant or in-stock after ANN search can leave too few results; engines such as Elasticsearch, OpenSearch, Vespa and Weaviate implement filtered HNSW traversal or fall back to exact search when the filter is very selective.

Tuning knobs that matter

For HNSW, M controls graph degree (memory and recall), ef_construction controls build quality, and ef_search controls the query-time candidate list. Raising ef_search from 40 to 200 commonly lifts recall at 10 from around 0.9 into the high 0.9s at a few times the latency. Measure recall against exact brute-force results on a sample of real queries, not synthetic ones.

Check yourself Question 1 of 3
Why rank fusion recall
From: Under the Hood: Hybrid Search Fusion (RRF)

A hybrid search returns BM25 scores in the range 0 to 40 and cosine similarities in the range 0.6 to 0.9. Why do most systems fuse them with Reciprocal Rank Fusion instead of adding the raw scores?

Key Takeaways

Vector search captures conceptual meaning, overcoming the vocabulary mismatch limitations of lexical search.
HNSW trades memory and build cost for approximate-neighbor latency and recall; validate both under the deployed corpus, filters, and concurrency.
Hybrid search combines lexical BM25 and vector retrieval using Reciprocal Rank Fusion (RRF) for optimal accuracy.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue