liveBook · Manning

Approximate nearest neighbor algorithm

As explained in MongoDB 8.0 in Action, Third Edition

Approximate nearest neighbor search retrieves vectors near a query vector using an efficient approximation rather than exhaustive comparison.

An approximate nearest neighbor (ANN) algorithm finds vectors close to a query vector without comparing it with every stored vector. In Atlas Vector Search, ANN supports fast similarity searches for uses such as semantic search and product recommendations. It trades some precision for speed; exact nearest neighbor (ENN) search compares every vector and costs more on large data sets. section 12.2 section 12.7

Atlas Vector Search uses Apache Lucene’s Hierarchical Navigable Small Worlds (HNSW) algorithm for ANN. HNSW connects similar vectors in a layered graph. A search starts in a sparse upper layer, moves toward nearby nodes, then descends through denser layers to refine the result. This avoids a brute-force scan. section 12.2

To run an ANN query with Atlas Vector Search:

  • Create a vector search index on the field holding stored embeddings. section 12.8
  • Embed the query with the same model used for the stored data. Atlas did not provide text-to-embedding conversion at the time described, so this step may require an external system. section 12.3.1
  • Use $vectorSearch with the index name, embedding field as path, and numeric queryVector. Set exact to false or omit it; ANN is the default. Choose limit for returned results and numCandidates for vectors considered. section 12.3.1 section 12.3
db.embedded_movies.aggregate([
  {
    "$vectorSearch": {
      "index": "MongoDB-in-Action-VectorSearchIndex",
➥// #1
      "path": "plot_embedding", // #2
      "queryVector": [<array-of-numbers>], // #3
      "numCandidates": <number-of-candidates>, // #4
      "exact": false, // #5
      "limit": <number-of-results> // #6
    }
  }
])

This $vectorSearch outline shows an approximate query against movie plot embeddings. Replace the placeholders with an embedding array, candidate count, and result limit. section 12.3.1

Tune numCandidates with the speed–precision trade-off in mind: considering more candidates can improve precision but take longer. Prefer ANN for routine similarity queries and use ENN when exactness is necessary. For performance, avoid indexing during queries, omit large vector fields from returned documents, and provide enough memory for vector data and indexes. section 12.3 section 12.7

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Explains HNSW’s layered graph traversal and walks through query parameters, but does not implement the algorithm from scratch.
Examples
Uses a movie-plot embedding query for “village life” and a local index configured with 1,536 dimensions and Euclidean similarity.
What it assumes
Requires indexed document embeddings and a query embedding produced with the same model; explains the needed query fields.
What you can do after
Apply it via Atlas Vector Search by configuring an ANN query and choosing when its speed warrants a precision trade-off.
How it connects
Connects HNSW, exact nearest neighbor search, vector embeddings, vector indexes, and the $vectorSearch aggregation stage.

Also explained in

Compare how 5 books explain approximate nearest neighbor algorithm →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of MongoDB 8.0 in Action, Third Edition. The section links point to the parts of the book it draws on.