liveBook · Manning

HNSW

As explained in AI-Powered Search

HNSW is a graph-based approximate nearest neighbor indexing and search algorithm that builds a multilayer hierarchy of proximity graphs.

HNSW (Hierarchical Navigable Small World) is a graph-based approximate nearest neighbor algorithm for searching dense vectors. It trades some accuracy for faster, more space-efficient retrieval than comparing a query with every vector, making it useful as a dense-vector equivalent of an inverted index. section 13.5.3

During indexing, HNSW incrementally builds multiple proximity-graph layers over nested subsets of stored vectors. Similar vectors become neighbors, while edges between neighborhoods make the graph navigable. Search follows these connections instead of scanning the full corpus, balancing recall and query throughput. section 13.5.3

A practical pipeline normalizes stored embeddings, adds them to an HNSW index, creates the index, encodes and normalizes each query, then retrieves the k nearest vectors. A similarity threshold can filter weak matches, with a fallback result when none qualify. section 13.5.4

Semantic autocomplete

An HNSW index containing 12,375 concept embeddings returns terms related to “bag,” including “bag ratings,” “bag liner,” “garbage bags,” and “tea bags.” For “mountain hike,” it returns “mountain hiking,” “mountain trail,” and “mountain guide,” supporting more precise suggestions than literal prefix matching alone. section 13.5.4

Listing 13.14 ANN search using NMSLIB
import nmslib

concepts_index = nmslib.init(method="hnsw",  #1
                             space="negdotprod")  #1
normalized_embeddings = list(map(normalize_embedding, embeddings))
concepts_index.addDataPointBatch(normalized_embeddings)  #2
concepts_index.createIndex(print_progress=True)   #3

ids, _ = concepts_index.knnQuery( #4
           normalized_embeddings[25], k=10)  #4
matches = [labels[phrases[i]].lower() for i in ids] #5
display(matches)

The listing shows a minimal NMSLIB implementation: initialize HNSW with a distance space, batch-add normalized embeddings, create the index, and query nearest neighbors. section 13.5.4

How AI-Powered Search covers it

How deep it goes
Explains multilayer graph construction and gives an implementation workflow, while deferring detailed algorithm internals.
Examples
Demonstrates concept autocomplete for “bag,” “mountain hike,” and “dehyd” using nearest-neighbor result lists.
What it assumes
Assumes embeddings and dense vectors exist, while demonstrating normalization, index creation, query encoding, and nearest-neighbor retrieval.
What you can do after
Apply HNSW via NMSLIB by indexing normalized embeddings and querying filtered nearest neighbors.
How it connects
Connects approximate nearest neighbor search with dense vectors, embeddings, semantic search, product quantization, and inverted file indexes.

Also explained in

Compare how 7 books explain HNSW →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of AI-Powered Search. The section links point to the parts of the book it draws on.