Home › Embeddings & Vector Search › kNN / ANN search
🧭 · Models

kNN / ANN search

The algorithms that find the closest vectors quickly without comparing every pair.

In one line

ANN search trades a sliver of accuracy for orders-of-magnitude faster nearest-neighbor lookups.

ConceptWhat it is

kNN (k-nearest neighbors) finds the k closest vectors to a query by exact distance comparison, while ANN (approximate nearest neighbor) trades a small amount of accuracy for massive speed gains. ANN exists because exact kNN requires comparing a query against every stored vector, which becomes too slow past a few hundred thousand items.

ANN algorithms build index structures, like graphs or clusters, that let a search skip most of the dataset while still finding neighbors that are very likely the true closest ones.

How it worksThe mechanics

Common ANN methods include HNSW, which builds a navigable small-world graph traversed greedily toward the query, and IVF, which clusters vectors and only searches the nearest clusters; both return an approximate top-k ranked by distance, with a tunable parameter trading recall against latency.

At a glanceSee it

kNN / ANN search diagram
kNN / ANN search diagram 1

The choice the query-time view already assumes was made — exact kNN wins for small corpora or zero-miss needs, and ANN earns its place only past both scale and recall tolerance.

kNN / ANN search diagram 2

An ANN index is built once but fragments as vectors churn, so serving must loop through scheduled rebuilds — skip them and recall drifts down silently, raising no error.

When to use itWhere it fits

  • Large-scale semantic search where exact kNN is too slow.
  • Real-time recommendation serving under strict latency budgets.
  • Any RAG system retrieving from millions of embedded chunks.
  • Deduplication across very large datasets.

When NOT to use itLimits & anti-patterns

  • Small collections where exact kNN is fast enough and more accurate.
  • Applications requiring guaranteed exact nearest neighbors, like scientific matching.
  • Very high-dimensional sparse vectors where some ANN indexes perform poorly.

Trade-offsAdvantages & costs

Advantages
  • Orders-of-magnitude faster than brute-force exact search.
  • Scales to billions of vectors with tunable recall.
  • Widely supported across all major vector databases.
  • Index build time is amortized across many queries.
Trade-offs & costs
  • Approximate results occasionally miss the true nearest neighbor.
  • Index build and memory overhead can be significant.
  • Recall and speed require ongoing tuning as data grows.
  • Updates and deletes can be costlier than with flat indexes.

ExampleIn the real world

Spotify's music recommendation system uses ANN search, specifically an HNSW-style index, to match user taste vectors against hundreds of millions of track embeddings in real time.

ToolsHow to implement it

  • FAISSMeta's library implementing HNSW, IVF, and other ANN algorithms.
  • HNSWliblightweight, fast standalone HNSW implementation.
  • ScaNNGoogle's ANN library tuned for high recall at scale.
  • Pinecone and Weaviatemanaged databases with ANN indexing built in.

Cost & effortWhat it takes

Compute cost is mostly index build time and memory footprint, roughly proportional to vector count; queries themselves are millisecond-scale.

A living map of modern AI — kept current every morning