HNSW RECALL
The index got worse and the latency graph got better
Ojaline's product index is an HNSW graph searched at ef_search 5. At launch, recall@5 against brute force is 1.00. Three nightly syncs each delete and re-insert 20% of the products, so the index ends up holding exactly the same 60 vectors. What do you measure now?
An HNSW index (Malkov and Yashunin, arXiv 1603.09320) is a stack of proximity graphs. Every vector sits on layer 0; a random, thinning subset also sits on the layers above, as in a skip list. A node links to M near neighbours on each layer when it is inserted, and keeps at most M links on the upper layers and 2M on layer 0, where the paper’s Mmax0 = 2·M. A query descends greedily from the top, then runs a beam search on layer 0 that keeps the ef_search best candidates and stops when no candidate can improve them.
The graph is the index. A delete takes a node out of other nodes’ routes. The sim deletes the way FreshDiskANN’s “Delete Policy A” does: remove the node and every edge into and out of it, add nothing back. The re-insert links the new node well, and the neighbours that lost a link keep the loss.
# hnswlib-style index, ef_search tuned once at launch
index.set_ef(5)
for product in changed_listings: # every night
index.delete(product.id) # node and its edges gone, no repair
index.add(product.id, product.vec)
# latency dashboard: flat. Nobody measures recall.# every night, after the sync
sample = random_queries(200)
truth = [brute_force(q, k=5) for q in sample] # exact top 5 over live rows
for ef in (5, 10, 20, 40, 80):
if recall(index, sample, truth, ef) >= 0.95:
index.set_ef(ef); break
else:
rebuild_index() # REINDEX: ef cannot reach what no edge points to Watch the dashboard column. In THREE SYNCS the links per node fall from 6.35 to 4.75 and nothing turns red until the monitor measures: 0.85, at fewer distance computations than the fresh index. RAISE EF_SEARCH gets 0.99 back for 47.3 computations per query. REBUILD gets 1.00 at the old cost. In FOUR SYNCS 8 products have no path to them at all, ef_search 20 still measures 0.79, and only the rebuild brings it back.
What is simplified: 60 products in 2-D instead of millions in hundreds of dimensions, M = 4 instead of pgvector’s default of 16, nearest-M neighbour selection without the paper’s heuristic, and one delete policy. Libraries differ. hnswlib’s mark_deleted marks a node so that it “will be omitted from search results”; pgvector leaves dead tuples in the index until VACUUM, and its FAQ warns of fewer results “due to dead tuples”. The measurement that catches all of them is the same.
The sync job runs three catalogue syncs. The monitor measures recall@5 twice at ef_search 5. Order the steps so that both measurements read ≥ 0.9, yet the index you end with has recall below 0.9.
GOAL: both measurements ≥ 0.9, final recall < 0.9
After three syncs, recall@5 at ef_search 5 is below 0.9. Find the smallest ef_search that brings the final measurement back to ≥ 0.9, and note what it costs in distance computations.
GOAL: final recall ≥ 0.9 at the smallest such ef_search
Write tune(index, queries, k, efs, target). index.live() returns the live products as { id, x, y }; index.search(q, k, ef) returns up to k ids from the HNSW graph for a query { x, y }. Recall for one query is the share of its true k nearest live products (Euclidean distance, by brute force over index.live()) that the search returned; average it over all queries. efs is sorted ascending. Return the smallest ef in efs whose average recall is ≥ target, or the string 'rebuild' if none is. The grader runs it on a fresh graph, a graph after three syncs, one after four, and with a shorter efs list.
The vector indexes chapter runs the same measurement on pgvector with real embeddings, sweeps hnsw.ef_search, and covers the README’s advice for slow vacuums on HNSW: REINDEX INDEX CONCURRENTLY, then VACUUM. In a RAG pipeline this is the second place retrieval can fail without an error; the first is chunking, which decides what the index can return at all.
Vector search in pgvector, hnswlib, FAISS, OpenSearch and the hosted vector databases: product search, RAG retrieval and recommendation over data that is updated every day. Symptoms: searches that miss items you can find by id, fewer results than the LIMIT, recall that drifts down over weeks while p99 latency holds or improves. See the vector indexes chapter for the pgvector knobs.
“Our vector search has got worse over time but nothing changed. What do you check?” Say recall first: a brute-force ground truth on sampled queries, compared with what the index returns. Then explain the graph: deletes remove routes, the beam stalls in local minima, some nodes become unreachable. Give the two fixes and their costs: raise ef_search (latency, no rebuild) or rebuild the index (build time and memory), and say you would schedule the rebuild after heavy churn and alert on recall, not only on latency.
USED IN
Chapters that build on this lesson.
- Shipping a retrieval or agent featureAI Engineer