Notes

Approximate Nearest Neighbour Search

Finding the closest vector to a query is easy when you can compare it against every vector you hold. At ten million documents and a twenty-millisecond latency budget you cannot — so you stop insisting on the exact answer. Approximate nearest neighbour search gives up a small, measurable amount of correctness in exchange for a search that finishes.

How the shortcut works

Every method narrows the candidate set before measuring distances. Graph indexes (HNSW) link each vector to its neighbours and walk greedily toward the query from an entry point, touching a few thousand candidates instead of ten million. Clustering indexes (IVF) partition the space and scan only the partitions nearest the query. Product quantization compresses each vector into a short code so distances come from a lookup table and the index fits in memory.

The knob you actually turn

Each index exposes one parameter — HNSW's efSearch, IVF's nprobe — that buys accuracy with latency. Quality is reported as recall@k: the fraction of the true top-k results the approximate search actually returned. Ninety-five percent recall at a fraction of the exact-search cost is a normal operating point.

Why retrieval depends on it

A grounded answer can only be as good as the passages that reached the context window. A missed neighbour looks like a model failure: the generator answers confidently from the wrong chunks, and no amount of prompt work recovers a passage the index never returned. Faiss, hnswlib and ScaNN implement these indexes directly; Pinecone, Milvus and Weaviate are services built around them.

Approximate nearest neighbour search finds vectors close to a query vector without comparing it against every vector in the store, trading a small and measurable recall loss for a search that returns in milliseconds at million-document scale. Indexes such as HNSW and IVF narrow the candidates first, and one tunable parameter buys accuracy with latency. Every retrieval step rests on it: passages the index misses are passages the model never sees.

Imagine a library of ten million books, and you want the handful most similar to the one in your hand. Checking every book is the only way to be certain — and far too slow. Approximate nearest neighbour search is the librarian's trick: arrange the shelves in advance so you only have to look in the most promising corner, and accept that you might miss one good book in twenty. For an AI assistant answering questions from a company's documents, this is the step that finds the right pages. It is adjustable too — search harder for better results, or less hard for faster ones.