← AI glossary

AI glossary · Advanced · Language models · Efficiency & infra

ANN search

ANN search (Approximate Nearest Neighbor search) is a method for finding the items most similar to a query among a very large amount of data, not "exactly" but "almost exactly", in exchange for much higher speed. It is a core technology of vector databases and RAG retrieval.

About this term
Level
Advanced · Specialist terms that need the inner workings
Added
2026-10-08
In plain words It is like finding a book in a large library by following the call number straight to the right shelf and looking only at nearby books, instead of flipping through every book one by one. It is also similar in that you may sometimes miss a better-matching book shelved elsewhere.

How it works

First, documents or images are converted into lists of numbers (vectors) with an embedding model, and an index is built in advance so these vectors can be found quickly. Common indexing methods include IVF, which groups similar vectors into clusters, and HNSW, which links nearby vectors in a multi-layer network. When a query comes in, it is converted into a vector the same way, and the search follows the index to pick out and compare only the most likely candidates. Because it does not compare against all the data, it is fast, but the results may differ slightly from the true nearest items.

The two stages of ANN searchDocuments, images → Embedding model; Embedding model → ANN index(Stored in advance); Query → Embedding model; Embedding model → Traverse the index; ANN index → Traverse the index(Reference); Traverse the index → k nearest items① Done in advance · Indexing② At search timeDocuments, imagesEmbedding modelConvert to vectorsANN indexHNSW, IVF, etc.QueryEmbedding modelQuery to vector tooTraverse the indexCompare candidatesonlyk nearest itemsNear-exact resultsStored in advanceReference
The two stages of ANN search The top row is index building, done in advance; the bottom row is the search when a query comes in. The search follows the index, compares only candidates and returns roughly the k nearest items.

Why it matters

Exact nearest neighbor search (KNN) becomes far too slow once data grows to millions or billions of items, because even a single query requires computing distances to all the data. ANN search gives up a little accuracy to greatly reduce search time, making real-time search possible in large-scale services. Retrieving document chunks relevant to a question in RAG, semantic search, and recommending similar images or products all depend on this technology. The core feature that vector databases advertise is, in the end, fast ANN search.

Things to keep in mind

ANN search involves a trade-off between speed and accuracy. Setting it to examine more candidates raises accuracy but slows it down; the opposite makes it faster but misses more results. How well it finds the truly nearest items is usually measured by recall. Building and maintaining an index also takes memory and time, so you need to choose a method that fits your data size and update frequency. This is why a reranker is sometimes added afterward to further improve search quality.

Example

Suppose you build an internal document chatbot with RAG. If you chunk and embed internal documents in advance and load them into a vector database, the vector database builds an ANN index such as HNSW. When an employee asks a question, the question is embedded, ANN search instantly finds the few most similar document chunks, and these are passed to the LLM along with the question. The LLM then writes an answer based on that content.

Related terms