CLOUD CONCEPT

Hierarchical Navigable Small World (HNSW)

Hierarchical Navigable Small World (HNSW) is a graph-based indexing algorithm that accelerates approximate nearest-neighbor searches over vectors.

Cloud Concepts Hub →

What is Hierarchical Navigable Small World (HNSW)

HNSW organizes vectors into multiple layers of connected nodes so searches can navigate toward similar vectors without exhaustively comparing every stored vector. It supports semantic search, recommendations, and retrieval-augmented generation by trading additional index memory and construction work for low query latency and high recall.

Hierarchical Navigable Small World (HNSW) is an approximate nearest-neighbor (ANN) indexing algorithm for finding vectors close to a query vector. Embedding models represent items such as documents, images, and products as numerical vectors. A vector search compares these representations using a measure such as Euclidean distance, cosine distance, or inner product. A brute-force exact search evaluates every eligible vector; HNSW instead explores a graph to find promising candidates, typically performing far fewer comparisons. Other exact-search algorithms can prune candidates while preserving correctness, so exhaustive scanning is not a requirement of exact search itself.

The index contains several graph layers. The bottom layer includes all indexed vectors, while progressively higher layers contain smaller subsets selected through randomized level assignment. Search begins in an upper layer, navigating toward nodes closer to the query, and then descends through lower layers. Sparse upper layers provide long-range navigation; the larger bottom layer supports more detailed candidate exploration. The hierarchy describes the search structure, not a business taxonomy or a hierarchy of document categories.

In a cloud application, an ingestion pipeline generates embeddings and writes them alongside identifiers and metadata into a vector-capable data store. That store builds and maintains an HNSW index. For example, a support assistant can embed a user's question, retrieve nearby document chunks, and pass those chunks to a language model. HNSW performs retrieval; it neither generates embeddings nor produces the final answer. Stored and query vectors must use compatible embedding models, dimensions, and preprocessing, and the index must support the intended similarity measure.

Three common tuning controls govern the main trade-offs, although names and availability differ by implementation. M controls graph connectivity: larger values generally increase memory consumption and construction work but can improve recall. ef_construction controls candidate exploration during index construction, affecting build cost and graph quality. ef_search controls candidate exploration during queries; increasing it generally improves recall at the cost of additional latency and CPU work. The requested result count, k, is distinct from the search exploration budget.

HNSW often provides high recall and low latency, but it does not guarantee the exact nearest neighbors. Measure recall@k against an exact-search baseline using representative queries, then evaluate latency percentiles, throughput, index size, and ingestion performance. Graph links add storage overhead beyond the vectors themselves. Traditional implementations can be memory-intensive, although some systems support disk-backed or compressed variants. Incremental insertion is supported, but update, deletion, cleanup, and rebuild behavior depend on the implementation.

For data-store selection, distinguish HNSW from inverted file (IVF) indexes. IVF partitions vector space into clusters and searches selected partitions rather than traversing a multilayer graph. Common IVF implementations require training representative centroids, whereas HNSW does not require a separate centroid-training phase. HNSW often suits high-recall, low-latency retrieval when sufficient resources are available. IVF offers different memory and performance trade-offs, particularly when combined with compression. Neither index family is universally faster or more accurate; benchmark both against the workload when both are available.

Metadata filtering is another practical consideration. A tenant or access-control filter can change how many useful candidates a search finds, especially if filtering occurs after approximate retrieval. Verify the store's filtering behavior and test highly selective predicates; requesting ten neighbors does not always produce ten qualifying results, even when enough matching records exist. Choose the data store using the complete workload, including SQL or keyword-search requirements, consistency, filtering, ingestion rate, resource costs, and supported index controls—not simply the presence of an HNSW option.

Key aspects of Hierarchical Navigable Small World (HNSW)

HNSW uses a tunable multilayer graph for fast approximate vector retrieval, with trade-offs in recall, resource consumption, construction cost, and maintenance.

Multilayer graph navigation

Sparse upper layers guide searches toward promising regions, while the bottom layer provides detailed exploration across all indexed vectors. This structure typically avoids an exhaustive comparison with every vector.

Approximate retrieval with measurable recall

HNSW can miss some true nearest neighbors, so accuracy should be measured against an exact-search baseline. Use recall@k together with latency and throughput targets to evaluate a configuration.

Separate construction and query controls

Graph connectivity and construction exploration affect index quality, build time, and resource consumption. Query exploration can often be adjusted separately to balance recall against latency without rebuilding the graph.

Graph storage overhead

The index stores graph connections in addition to vector data, increasing its resource footprint. Capacity planning should account for index construction, concurrent queries, replicas, and the implementation's memory or disk requirements.

No centroid-training requirement

HNSW builds its graph through vector insertion rather than learning cluster centroids first. This distinguishes it from common IVF implementations, although building a high-quality HNSW index still requires substantial computation.

Implementation-dependent filtering and maintenance

Metadata filters, deletions, updates, and index cleanup behave differently across data stores. Test selective filters and ongoing data changes rather than assuming identical behavior across HNSW implementations.

Cloud services for Hierarchical Navigable Small World (HNSW)

These services provide alternative hosting choices for HNSW-backed retrieval rather than components that must all be deployed together. An ingestion pipeline generates embeddings and stores them in the selected system; applications embed incoming queries and search its vector index. OpenSearch Service supports workloads combining vector retrieval with search-engine capabilities, while Aurora PostgreSQL and RDS for PostgreSQL support workloads that benefit from relational data and SQL. If relational records are also indexed in OpenSearch, synchronize identifiers, metadata, and deletions. Across these choices, compare recall, latency, filtering behavior, ingestion throughput, and total resource cost using representative data.

Amazon OpenSearch Service image Amazon OpenSearch Service

Amazon OpenSearch Service supports HNSW-based vector indexes through supported vector-search engines and configurations. Applications can combine vector retrieval with keyword search and metadata filtering; available tuning controls depend on the engine and deployment configuration.

Open Amazon OpenSearch Service →