AI systemsANNapproximate nearest neighborHNSWIVFrecall

Approximate nearest neighbour

Definition

Approximate nearest-neighbour (ANN) search returns vectors that are probably among the true k nearest to a query, using an index such as HNSW (a layered neighbour graph) or IVF (clustered lists) instead of a linear scan.

In practice

Exact search compares the query with every vector, which is linear in the data and too slow at tens of millions of vectors. HNSW walks a layered graph from coarse to fine and touches a tiny fraction of the vectors; IVF searches only the clusters closest to the query. Both make search sub-linear at the cost of occasionally missing a true neighbour.

Recall, the share of true nearest neighbours returned, and latency are one dial: in HNSW, a higher efSearch explores more of the graph and raises both. Measure recall on your own data after every index or model change rather than trusting defaults. HNSW is memory-hungry, so large indexes need sharding or quantisation.

Example

Raising efSearch from 40 to 200 lifts recall@10 from 0.91 to 0.99 on an evaluation set, while P95 query latency grows from 4 ms to 12 ms.