Approximate nearest neighbor search
Approximate nearest neighbor search 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 up dense vector retrieval for semantic autocomplete and document search.
- How deep it goes
- Explains HNSW’s layered proximity graphs and demonstrates index creation and querying, while relying on a library for algorithm implementation.
- Examples
- Indexes 12,375 autocomplete embeddings and retrieves related terms for “bag,” then searches question titles for “mountain hike.”
- What you can do after
- Apply it via NMSLIB to build an HNSW index, encode queries, and retrieve nearest-neighbor candidates.
Read how AI-Powered Search explains approximate nearest neighbor search →
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 search →
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
- Uses ANN to retrieve semantically similar listings before filtering and final ranking.
- How deep it goes
- Explains HNSW graph traversal and index-building parameters, including accuracy, memory, and indexing trade-offs.
- Examples
- Illustrates retrieval with listing embeddings and a real-time user query.
- What you can do after
- Evaluate and understand ANN retrieval and HNSW tuning in a production search pipeline.
Read how Building Reliable AI Systems explains approximate nearest neighbor search →
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 search →
Retrieval Augmented Generation, The Foundational Ideas
Approximate nearest-neighbor search efficiently finds vectors close to a query vector without exhaustively comparing every stored vector.
- How it is used
- Makes semantic search and RAG retrieval responsive across millions or billions of vectors.
- How deep it goes
- Contrasts brute-force and approximate search, then emphasizes recall, latency, memory, and monitoring without detailing index internals.
- Examples
- Uses movie embeddings, Amazon's 200-million-item catalog, and billion-document web search as scale comparisons.
- What you can do after
- Evaluate ANN deployments by balancing recall, query latency, memory use, and index-growth behavior.
LLM Customization and Fine-Tuning
An indexing/search approach that trades a small amount of recall for much faster nearest-neighbor retrieval.
- How it is used
- Speeds up top-k similarity search in vector indexes when exhaustive search becomes too slow.
- How deep it goes
- Defines the recall-speed tradeoff and names HNSW and IVF, but does not detail their internals.
- Examples
- Compares NumPy, FAISS, and vector databases across corpus sizes and names HNSW and IVF deployment contexts.
- What you can do after
- Apply the scale-based guidance to choose an index library or vector database for retrieval.
Read how LLM Customization and Fine-Tuning explains approximate nearest neighbor search →
Also covered in
- Just Use Postgres!
- Grokking Software ArchitectureIn early access (MEAP)
- Advanced Algorithms and Data Structures
- Context EngineeringIn early access (MEAP)
- Agent Design PatternsIn early access (MEAP)
- Generative AI in Action
- AI Agents and Applications
- Build an AI Agent (From Scratch)
What to know first
- embeddingAn embedding is a numerical vector representation of an item in a feature space, encoding selected properties or relationships for computational tasks such as comparison or retrieval.
- similarity searchSimilarity search is a retrieval technique that finds or ranks items by their similarity to a query, often by comparing feature or vector representations under a similarity or distance measure.
- vector databaseA vector database is a data-management system that stores and indexes numerical vector representations and supports retrieval through similarity or distance searches.
- Euclidean distanceEuclidean distance is the straight-line distance between two points, calculated as the square root of the sum of squared differences between corresponding coordinates.
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 their similarity to a query, often by comparing feature or vector representations under a similarity or distance measure.
- brute-force similarity searchalternative to · Brute-force similarity search compares a query vector with every vector in a dataset to identify the most similar items.
- Inverted File Indexuses · An inverted file index is an approximate nearest-neighbor structure that partitions vectors into clusters and searches selected clusters to find similar vectors efficiently.
- ivfflatimplemented by · IVFFlat is an approximate nearest-neighbor vector index that partitions vectors into centroid-based inverted lists and compares vectors within selected lists.
Questions about approximate nearest neighbor search
What is approximate nearest neighbor search?
Approximate nearest neighbor search efficiently finds items close to a query under a distance or similarity measure without guaranteeing the exact nearest items.
Which Manning books explain approximate nearest neighbor search?
AI-Powered Search, Natural Language Processing in Action, Second Edition, MongoDB 8.0 in Action, Third Edition, Building Reliable AI Systems, Outlier Detection in Python, Retrieval Augmented Generation, The Foundational Ideas and LLM Customization and Fine-Tuning.
What should I know before learning approximate nearest neighbor search?
Manning books that teach approximate nearest neighbor search usually introduce embedding, similarity search, vector database and Euclidean distance first.
What is the difference between approximate nearest neighbor search and similarity search?
Approximate nearest neighbor search efficiently finds items close to a query under a distance or similarity measure without guaranteeing the exact nearest items. Similarity search is a retrieval technique that finds or ranks items by their similarity to a query, often by comparing feature or vector representations under a similarity or distance measure.
What is approximate nearest neighbor search also called?
Approximate nearest neighbor search is also called ANN or approximate nearest neighbor algorithm.
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.






