Nearest-neighbor search

Nearest-neighbour search is the problem of finding the items in a collection that are closest to a query item under some measure of distance, the core operation behind vector databases and semantic search.

Nearest-neighbour search asks a simple question: given a point, which stored points are closest to it? Donald Knuth called it the post-office problem in 1973. In AI the points are usually embeddings, long lists of numbers that represent a piece of text, an image or a product, so that similar things sit close together. Finding the nearest embeddings to a query is how a system finds the most similar documents.

The exact answer is easy to describe: measure the distance from the query to every stored item and keep the closest. That brute-force scan is fine for thousands of items but slow for billions. Tree structures that split up the space help in a few dimensions, but they lose their advantage as the number of dimensions grows, a problem known as the curse of dimensionality. Embeddings often have hundreds or thousands of dimensions.

So most large systems use approximate nearest-neighbour search, trading a little accuracy for a great deal of speed. One family hashes nearby points into the same buckets. Another compresses vectors so more fit in memory. Graph-based methods are regarded as the state of the art: HNSW, published by Malkov and Yashunin in 2016, links stored items into layered graphs and searches by hopping greedily towards the query. A 2017 paper by Jeff Johnson, Matthijs Douze and Hervé Jégou showed what graphics chips can do at this scale, building a neighbour graph over 1 billion vectors in under 12 hours on four GPUs.

This is the machinery underneath retrieval-augmented generation, semantic search, recommendation and duplicate detection. When a chatbot fetches passages from a company's documents before answering, an approximate nearest-neighbour index usually chooses them. The approximation means a relevant passage can occasionally be missed, which is one reason retrieval systems are tuned and tested on how often they find the right material.

Common questions

What is approximate nearest neighbour search?

It is nearest-neighbour search that accepts a small chance of missing the true closest items in return for much faster answers. Indexes such as HNSW graphs or compressed vector schemes can search very large collections far faster than an exact scan.

How is nearest-neighbour search used in AI?

Text, images and other items are turned into embeddings, and a query is turned into an embedding the same way. The nearest stored embeddings are treated as the most relevant results. This powers semantic search, recommendations and the retrieval step in retrieval-augmented generation.

What is the difference between nearest-neighbour search and k-nearest neighbours?

Nearest-neighbour search is the retrieval task: find the closest stored items to a query. The k-nearest neighbours algorithm is a classification or prediction method that uses such a search, then predicts an answer from the labels of the k closest items.

What is HNSW?

HNSW stands for Hierarchical Navigable Small World, a graph-based index for approximate nearest-neighbour search published in 2016. It builds layers of linked graphs and searches from a sparse top layer down to a dense bottom one, and the paper reports that its search time grows only logarithmically with the size of the collection.

Finding more

Search for "approximate nearest neighbor search", "HNSW index" or "vector similarity search benchmarks". Reliable sources include the original papers on arXiv, documentation for open-source vector search libraries, and independent benchmark projects that publish their test code. Be careful with speed comparisons from database vendors, which usually pick settings that suit their own product.

Working out what this means for your business?

Aileron Shift builds the systems behind this site: AI newsrooms that research, write and publish on their own, marketing automation and WordPress hosting for companies that need to move faster than their competitors.