Skip to content
AI Skills Central

Type to search Agent Skills, workflows, tutorials, tools and the glossary.

HNSW

Definition

Hierarchical Navigable Small World: a graph-based index for fast approximate nearest-neighbour search, widely used by vector databases.

In more detail

HNSW finds close matches quickly by walking a layered graph instead of comparing the query to every vector. Settings trade recall against speed and memory, so an approximate index can occasionally miss the true nearest neighbour.

The top layers hold few points with long-range links; lower layers are denser. A search starts at the top, jumps close to the target, then refines on the layers below. Build-time settings (often called m and ef_construction) control how many links each node keeps, and a query-time setting (ef_search or similar) controls how widely the search explores.

HNSW indexes are typically held in memory and are slower to build than flat or IVF indexes, so very large collections sometimes use quantisation or disk-based alternatives. pgvector, Qdrant, Weaviate and Milvus all offer HNSW.

  • Cosine similarity

    A measure of how similar two vectors are based on the angle between them, ranging from -1 (opposite) to 1 (same direction).

  • Embedding

    A vector representation of text or other data that places items with similar meaning close together, so they can be searched by similarity.

  • Hybrid search

    Combining keyword search, such as BM25, with vector search and merging the results into one ranking, often with reciprocal rank fusion.

  • Vector database

    A database designed to store embeddings and quickly find the nearest neighbours to a query vector, often with metadata filtering.

  • pgvector

    Postgres extension for vector similarity search with HNSW and IVFFlat indexes.

    Tool

  • Embeddings and vector search, hands on

    Embed documents, store them in Postgres with pgvector, index them and run similarity and hybrid search.

    TutorialIntermediate45 min

  • pgvector vs Pinecone, Qdrant and Weaviate

    When storing embeddings in Postgres with pgvector is enough, and when a dedicated vector database such as Pinecone, Qdrant or Weaviate earns its extra cost.

    Article