Approximate nearest neighbor algorithm
An approximate nearest neighbor algorithm efficiently finds items close to a query under a distance or similarity measure without guaranteeing the exact nearest items.
AI-Powered Search
Approximate nearest neighbor search is a retrieval technique that finds vectors likely to be nearest to a query vector while reducing the computations required for exact nearest-neighbor search.
- How it is used
- Speeds dense vector retrieval by selecting likely matches before expensive similarity scoring.
- How deep it goes
- Works through HNSW index creation and querying, while leaving the graph algorithm’s implementation to NMSLIB.
- Examples
- Demonstrates autocomplete over 12,375 terms and semantic retrieval of hiking-related question titles.
- What you can do after
- Build it from scratch with NMSLIB by indexing normalized embeddings and querying HNSW for semantic suggestions.
Read how AI-Powered Search explains approximate nearest neighbor algorithm →
Natural Language Processing in Action, Second Edition
An approximate nearest neighbor algorithm finds items near a query vector with lower computational cost than exhaustive exact search while allowing some accuracy loss.
- How it is used
- Accelerates semantic search over dense embedding vectors when exhaustive exact search cannot meet scale or latency needs.
- How deep it goes
- Defines the speed–precision tradeoff, explains LSH and ANN families, and connects indexing choices to production vector databases.
- Examples
- Uses Wikipedia-scale document retrieval, job-title similarity, LSH, Annoy, pgvector, and hosted vector databases as concrete examples.
- What you can do after
- Apply it via an ANN index or vector database to build faster semantic retrieval while evaluating acceptable precision loss.
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.
- How it is used
- Finds similar embedded documents efficiently through Atlas Vector Search, particularly for semantic search and recommendations.
- 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 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.
Read how MongoDB 8.0 in Action, Third Edition explains approximate nearest neighbor algorithm →
Building Reliable AI Systems
A search method that efficiently finds vectors close to a query vector while allowing approximate rather than exact nearest neighbors.
- How it is used
- Retrieves semantically similar candidates within a listing-search pipeline before explicit filtering and final ranking.
- How deep it goes
- Explains HNSW’s layered graph traversal and index-tuning trade-offs, without giving a code implementation.
- Examples
- Uses listing search with price and date filters, plus an HNSW diagram tracing a search through graph layers.
- What you can do after
- Operate/configure HNSW by weighing M and efConstruction against search quality, memory use, and indexing time.
Read how Building Reliable AI Systems explains approximate nearest neighbor algorithm →
Outlier Detection in Python
A search technique that finds points near a query point with lower computational cost than exact nearest-neighbor search by allowing limited approximation.
- How it is used
- Supports fast nearest-neighbor searches for large-scale outlier detection and related data-processing tasks.
- How deep it goes
- Explains the performance motivation, numeric-data limitation, HNSW workflow, and query example without deriving approximation guarantees.
- Examples
- Demonstrates a datasketch HNSW index over 1,000 ten-feature records and a ten-neighbor query.
- What you can do after
- Apply it via datasketch HNSW to index numeric records and query approximate nearest neighbors.
Read how Outlier Detection in Python explains approximate nearest neighbor algorithm →
Related concepts
- vector databaseused by · A vector database is a data-management system that stores and indexes numerical vector representations and supports retrieval through similarity or distance searches.
- HNSWimplemented by · HNSW is a graph-based approximate nearest-neighbor indexing and search method that organizes data points in a hierarchy of navigable proximity graphs.
- similarity searchcontrasts with · Similarity search is a retrieval technique that finds or ranks items by comparing their representations with a query according to a similarity or distance measure.
- ivfflatimplemented by · IVFFlat is an approximate nearest-neighbor vector index that partitions vectors into centroid-based inverted lists and compares vectors within selected lists.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.




