presentsUNAIVERSE
The UN AI timeline

Learn AI /Search, Embeddings and RAG /Nearest-neighbour search

Nearest-neighbour search

Also called: ANN, k-NN, approximate nearest neighbour.

Detail

In five seconds

What it is

A method for finding the items most similar to a query in a vector space; approximate methods often trade a little accuracy for much faster search at scale.

In plain words

Turn the query and the stored items into numbers, find which stored items are closest, and return the best few. If the collection is huge, use a shortcut index so the search is fast enough.

An everyday example

You need the cable that best fits an unfamiliar device. Checking every cable would work, but sorting first by connector shape quickly narrows the search to one compartment. You then compare the remaining cables by tip size, length and power rating, and keep the closest matches. The compartments make the search much faster, although a misfiled cable could mean that the best match is overlooked. A vector index plays the role of those compartments.

Where the story stops working

Real libraries place books by human classification rules, while vector spaces are learned from data and may capture many mixed patterns at once. In a library, nearby books are physically close on shelves; in nearest-neighbour search, “near” means mathematically similar, not geographically close. Also, an ANN index does not simply point to one shelf; it uses search structures and heuristics that can behave very differently from human browsing.

Remember thisNearest-neighbour search ranks stored vectors by closeness to a query vector. Approximate indexes search far faster at scale, with a small risk of missing the true closest items.

Understand it

Understand it

Why it matters

Many modern AI systems do not search by exact words alone. They retrieve by meaning using embeddings, and nearest-neighbour search is the mechanism that makes that practical. For a non-specialist, this matters because speed, quality and cost depend on it. If retrieval is poor, a chatbot may cite the wrong passage or miss the right one. If approximate search is tuned badly, results may be fast but incomplete. If it is tuned well, large knowledge bases become usable in real time. This affects procurement, evaluation, safety checks, multilingual access and whether a system can be trusted to ground its answers in the right source material.

How nearest-neighbour search retrieves similar items

Show how a query vector moves through an exact or approximate index to produce the top k similar items, and why approximate search is faster at large scale.

  1. 01

    1. Encode the query

    The user’s text, image or other input is converted into a vector so it can be compared mathematically with stored vectors.

    Draw an input box labelled 'Query' feeding into a box labelled 'Encoder', then a small numeric vector icon such as [0.12, -0.44, ...].

  2. 02

    2. Search the index

    The system uses an index over stored vectors to find promising candidates. In exact search, it may compare against all stored vectors. In approximate search, it visits only selected parts of the index.

    Show a large container labelled 'Stored vectors + index'. Add two subtle paths: 'exact: scan all' and 'approximate: scan likely regions'.

  3. 03

    3. Score candidates by closeness

    Candidate vectors are compared with the query using a similarity or distance measure, such as cosine similarity. The system computes which candidates are closest.

    Display 6–8 candidate dots with score labels, with some dots highlighted as more similar to the query vector.

  4. 04

    4. Return top k items

    The closest items are returned as a ranked list, often followed by reranking or use in retrieval-augmented generation.

    Show a ranked panel labelled 'Top k results' with entries 1 to k and optional arrows to 'reranker' and 'answer generation'.

A left-to-right flow with four stages: query text becomes a vector; the vector enters a search index over many stored vectors; the system checks candidate neighbours; then it outputs the top k results. A branch note contrasts exact search with approximate search: exact checks all items, approximate checks fewer candidates for speed.

Hover or tap the exact/approximate branch to highlight the trade-off: more comparisons and higher recall versus fewer comparisons and faster response.

Describe this without the diagram

The diagram explains nearest-neighbour search as a four-stage flow. First, a user query is encoded into a vector, a list of numbers that represents the input. Second, that vector is sent to a collection of stored vectors organised by an index. The diagram shows two possible search styles: exact search, which compares the query to all stored vectors, and approximate search, which checks only likely regions of the index to save time. Third, the system scores candidate items by mathematical closeness, for example with cosine similarity. Fourth, it returns the top k closest items as a ranked list. A note says approximate search is usually much faster on large collections but may occasionally miss the true best match.

With reduced motion: A static four-panel flowchart replaces animation. Panel 1 shows query to vector. Panel 2 shows stored vectors with two labelled routes: exact scans all items; approximate scans selected regions. Panel 3 shows candidate scoring. Panel 4 shows the top k ranked results, with optional next steps for reranking or answer generation.

Nearest-neighbour search starts with a query represented as numbers, often called a vector or embedding. It compares that query with many stored vectors and returns the closest ones according to a distance or similarity measure such as cosine similarity. The output is usually a short ranked list: the top 1, top 5, or top k most similar items.

Several related terms are often mixed together. k-nearest-neighbour search means returning the k closest items. The abbreviation k-NN can also refer to a supervised classification or regression method that uses nearby examples. Nearest-neighbour search is the general task of finding the closest items, whether one or many. ANN means approximate nearest-neighbour search: a faster method that may miss true nearest items but is more practical for large collections.

What happens in practice depends on scale. With a small collection, a system can compare the query against every stored vector. With millions of items, that is often too slow, so the system builds an index: a data structure that helps narrow the search to promising regions first. The index does not change the meaning of similarity; it changes how the system finds similar items efficiently.

This is commonly used in semantic search and retrieval-augmented generation. A user asks a question, the system turns it into a vector, nearest-neighbour search retrieves passages with similar meaning, and those passages may then be reranked or shown to a language model. This is different from keyword search, which matches words directly rather than closeness in vector space.

How “The box of mixed cables” maps onto the real thing

In the storyIn the AI system
the request from the readerthe query vector or embedding
books stored near related topicsitems positioned close in vector space
keeping the five best matchesk-nearest neighbours
using a floor map and shelf guide to search fasteran approximate nearest-neighbour index

A worked example

Before a portal update is cleared, an information management officer at a regional hub tests whether the search function can find relevant passages across a collection of cleared situation reports and guidance.

Starting from: Query: "maintaining access to essential services during transport disruptions"

  1. The system converts the query into an embedding in the same vector space as the stored passages.
  2. An approximate index uses partitions or graph links to identify a promising candidate set rather than comparing the query with every stored vector.
  3. The system calculates cosine similarity for the candidates and orders them from most to least similar.
  4. It returns the requested number of passages with source metadata for the officer to review.

Result: The shortlist includes passages on continuity arrangements, alternative delivery points and coordination with service providers. Because the indexed search is approximate, the officer also checks retrieval quality against a small set of expected results before clearance.

In a United Nations workplace

A protection officer in a liaison office is finishing a handover note for an incoming colleague. The note links to an approved repository where a plain-language query can retrieve cleared guidance and background notes by meaning rather than by exact wording.

Nearest-neighbour search compares the query embedding with stored document embeddings and returns the closest matches. An approximate index can make this fast across a large repository, although it may occasionally miss a passage that an exhaustive comparison would have ranked higher.

Before you try thisPersonal, survivor, witness or security-sensitive material must not be embedded, indexed or uploaded to a tool that has not been approved for it. Vectors and search indexes can retain or expose information beyond the intended access controls. Use an approved environment, restrict the collection and review retrieved passages before use.

Where you may hear it

  • During a vendor demo for an internal knowledge-search tool
  • In a procurement unit's technical evaluation meeting
  • At an ICT governance board review of a retrieval system
  • During a search-quality review before a portal release

Avoid confusion

Avoid confusion

Often confused with

Common misconceptions

  • Common misconception: ANN, k-NN and nearest-neighbour search are all the same thing.

    Correction: They are related but not identical. Nearest-neighbour search is the general task. k-NN usually means returning the k closest items, and in some contexts it also refers to a simple supervised classification method that uses neighbours. ANN usually means approximate nearest-neighbour search, where the system accepts a possible loss in exactness to gain speed at large scale.

  • Common misconception: Approximate search is low quality and should be avoided.

    Correction: Approximate methods are standard because exact comparison against every item can be too slow or expensive on large collections. Good ANN systems often recover almost all of the true nearest items while responding much faster. The right question is not 'approximate or not' in the abstract, but what recall, latency and cost are acceptable for the task.

  • Common misconception: Nearest means the result is definitely relevant or correct.

    Correction: Nearest only means closest under a chosen mathematical representation and similarity measure. If the embedding is poor, the chunking is weak, or the similarity metric is unsuitable, the nearest items may still be unhelpful. Retrieval quality depends on the full pipeline, not just the search step.

  • Common misconception: This is just clustering with a different name.

    Correction: Clustering groups items into sets based on overall structure in the data. Nearest-neighbour search answers a query by finding the closest stored items to that query. A system may use clusters to speed up search, but the tasks are different: grouping the collection versus retrieving neighbours for one input.

Quick check

Go deeper

Go deeper

The simple version

Turn the query and the stored items into numbers, find which stored items are closest, and return the best few. If the collection is huge, use a shortcut index so the search is fast enough.

The technical version

Given a query vector q and a set of stored vectors, nearest-neighbour search returns the item with minimum distance to q or the top k items under a distance or similarity measure such as Euclidean distance, cosine similarity or inner product. A brute-force method computes scores against every vector. At scale, systems build an index to reduce the search space, for example by partitioning vectors, building graph-based neighbourhood links or using quantisation to compress representations. These are often called approximate nearest-neighbour methods because they optimise latency, memory and throughput while aiming to preserve high recall of the true nearest items. In retrieval pipelines, ANN typically provides a candidate set that may then be reranked with a more expensive model.

Reviewed external resources

  • EncyclopaediaWikipedia search: Nearest-neighbour search (opens in a new tab)Wikipedia

    This search results page helps readers scan multiple Wikipedia entries and related topics on nearest-neighbour search, and it suits readers who want a broad starting point rather than one fixed article.

    search results, not one pagelink checked 2026-07-31

  • CourseEmbeddings (opens in a new tab)Google

    This course material explains embeddings, which are the inputs that nearest-neighbour search compares, and it suits readers who want structured background before going deeper.

    Intermediatelink checked 2026-07-31

  • InteractiveEmbedding Projector (opens in a new tab)TensorFlow

    This interactive tool lets readers explore points in an embedding space and inspect nearby items, and it suits visual learners who want intuition about closeness and neighbourhoods.

    Starterlink checked 2026-07-31

  • VideoVideo explanations: Nearest-neighbour search (opens in a new tab)YouTube search

    This search results page gathers video explanations from different creators, and it suits readers who prefer to compare teaching styles rather than rely on one video.

    search results, not one pagelink checked 2026-07-31

  • DiscussionCommunity discussion: Nearest-neighbour search (opens in a new tab)Reddit search

    This search results page shows community discussions and practical questions about nearest-neighbour search, and it suits readers who want informal troubleshooting perspectives with caution about variable quality.

    search results, not one pagelink checked 2026-07-31

Where this came from

Drafted with a language model through an offline pipeline, checked against this site's schema, and every external link above fetched to see whether it answers. Treat it as a starting point and check anything that matters against the primary sources. How this site is built and what that means.

The one thing to remember

Nearest-neighbour search ranks stored vectors by closeness to a query vector. Approximate indexes search far faster at scale, with a small risk of missing the true closest items.