Skip to content

Concepts

What is an HNSW index?

How SrvDB's HNSW mode builds a layered neighbour graph, descends it at search time, saves it with save(), and supports filtered search - with m, ef settings and memory per vector.

HNSW (Hierarchical Navigable Small World) is a graph index: each vector is a node linked to its near neighbours, on several layers of decreasing density. A search starts at the sparse top layer and walks greedily downwards, so it scores a small part of the collection instead of all of it.

HNSW descent: greedy steps on sparse upper layers, then a wider ef search on layer 0 layer 2 layer 1 layer 0 entry point query is near here ef best candidates kept → top k
Upper layers are sparse and give long jumps; layer 0 holds every vector and is searched with a candidate list of size ef.
  1. Levels. A new node gets a random top layer; the chance of reaching each higher layer falls by a factor of m (level multiplier 1 / ln m). So almost every node lives only on layer 0, and a few reach the top.
  2. Insert. The node is placed by searching the graph with a candidate list of ef_construction. Neighbours are chosen with the diversity heuristic: a candidate is kept only if it is closer to the new node than to every neighbour already kept. This keeps long links between clusters, so clustered data does not split into islands (issue #28). Each node keeps up to m links per upper layer and 2 × m on layer 0.
  3. Search. From the entry point the walk moves greedily to the neighbour closest to the query on each upper layer, then runs a best-first search on layer 0 that keeps ef_search candidates (at least k). Scores are exact cosine similarities from vectors.bin.
  4. Save. save() writes hnsw.graph atomically (temp file, fsync, rename). The graph is derived data: if it is missing or damaged it is rebuilt from the vectors on open.
  5. Filtered search (Rust). search_filtered walks the graph and, when it reaches neighbours the filter rejects, looks at their neighbours (ACORN-1 style two-hop expansion). When few ids match it scores them exactly instead. See ADR 0010 and Using SrvDB from Rust.

Durable vector storage, quantized modes and filtered HNSW in one embedded library, with no server: that combination is what SrvDB is built for.

import random
import srvdb
rng = random.Random(7)
vectors = [[rng.gauss(0, 1) for _ in range(128)] for _ in range(1000)]
ids = [f"doc-{i}" for i in range(len(vectors))]
db = srvdb.create("./hnsw-db", dimension=128, index="hnsw", m=16, ef_construction=200, ef_search=50)
db.add(ids, vectors, ["{}"] * len(ids))
db.set_ef_search(120) # wider search: higher recall, slower
db.save() # hnsw.graph is saved and reloaded on open
print(len(db.search(vectors[42], k=5)))
# 5
  • Recall. Approximate. Raise ef_search (search time) or m and ef_construction (build time) for higher recall. Measure against flat on your own data.
  • Speed. A search visits a small part of the graph instead of all n vectors. Inserts cost more than in flat mode, because each one runs a graph search.
  • Bytes per vector. The full vectors (4 × D) plus the links. With m = 16, layer 0 holds up to 32 neighbour ids per node; upper layers add on average about one more list of up to 16 for every 15 nodes. In memory each id is 8 bytes, so up to about 256 bytes of layer-0 links per node; on disk hnsw.graph uses variable-length (postcard) encoding.
  • Limits. m is 2 to 256; ef_construction and ef_search are 1 to 10000. Values outside raise ValueError.
  • Flat search is too slow and approximate results are acceptable.
  • Data is added gradually: no training step, unlike IVF.
  • You need filtered search over metadata from Rust.
  • Not when disk is the limit: HNSW stores full vectors plus links. Then see SQ8 or PQ.

No, it is approximate: the graph walk can miss a true neighbour. The scores it returns are true cosine similarities, and raising ef_search raises recall.

What happens if the HNSW graph file is lost?

Section titled “What happens if the HNSW graph file is lost?”

The graph is derived data. SrvDB rebuilds it from vectors.bin when hnsw.graph is missing or damaged.

Which HNSW settings does SrvDB use by default?

Section titled “Which HNSW settings does SrvDB use by default?”

m = 16 links per node (32 on layer 0), ef_construction = 200 and ef_search = 50.

Next: What is an IVF index?

  • Yu. A. Malkov, D. A. Yashunin (2016, journal version 2018). Efficient and robust approximate nearest neighbor search using Hierarchical Navigable Small World graphs. IEEE TPAMI. arXiv:1603.09320
  • Liana Patel, Peter Kraft, Carlos Guestrin, Matei Zaharia (2024). ACORN: Performant and Predicate-Agnostic Search Over Vector Embeddings and Structured Data. SIGMOD 2024. arXiv:2403.04871