Skip to content

approximate nearest neighbor (ANN)

Approximate nearest neighbor (ANN) search is a family of algorithms that find points close to a query vector without comparing the query against every stored point, accepting an occasional miss in exchange for much faster lookups.

An exact nearest neighbor search has to measure the distance from the query to every item, which gets slow once a collection holds millions of high-dimensional embeddings. An ANN algorithm builds an index ahead of time and then inspects only a small, promising part of the data for each query. The answer it returns is usually the true nearest neighbor, or one almost as close.

Common ANN index families include:

  • Graph-based indexes, such as Hierarchical Navigable Small World (HNSW), which link each point to nearby points and walk the graph toward the query.
  • Inverted file (IVF) indexes, which cluster the vectors into cells and search only the cells closest to the query.
  • Product quantization (PQ), which compresses vectors into short codes so more of them fit in memory and compare quickly.
  • Locality-sensitive hashing (LSH), which hashes similar points into the same buckets with high probability.

Quality is usually measured as recall, the share of the true nearest neighbors that a query returns. Index settings, such as how many IVF cells to probe or how wide an HNSW search runs, trade recall against latency and memory.

In the interactive IVF index below, drag the slider to probe more cells, or click the plot to move the query, and watch recall climb as the number of points compared grows:

Interactive diagram — enable JavaScript to view.

ANN search is the core operation behind vector databases, semantic search, and the retrieval step in retrieval-augmented generation (RAG). Libraries such as Meta’s Faiss and hnswlib implement these indexes.

Embeddings and Vector Databases With ChromaDB

Tutorial

ChromaDB: Embeddings and Vector Databases in Python

Learn what embeddings and vector databases are, then store, query, and filter text with ChromaDB in Python and use it to give an LLM context.

advanced ai databases data-science machine-learning

For additional information on related topics, take a look at the following resources:

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Martin Breuss • Updated Nov. 18, 2026