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.
How it works
Section titled “How it works”- Levels. A new node gets a random top layer; the chance of reaching
each higher layer falls by a factor of
m(level multiplier1 / ln m). So almost every node lives only on layer 0, and a few reach the top. - 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 tomlinks per upper layer and2 × mon layer 0. - 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_searchcandidates (at leastk). Scores are exact cosine similarities fromvectors.bin. - Save.
save()writeshnsw.graphatomically (temp file, fsync, rename). The graph is derived data: if it is missing or damaged it is rebuilt from the vectors on open. - Filtered search (Rust).
search_filteredwalks 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.
Try it in SrvDB
Section titled “Try it in SrvDB”import randomimport 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, slowerdb.save() # hnsw.graph is saved and reloaded on openprint(len(db.search(vectors[42], k=5)))# 5Trade-offs
Section titled “Trade-offs”- Recall. Approximate. Raise
ef_search(search time) ormandef_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
nvectors. 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. Withm = 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 diskhnsw.graphuses variable-length (postcard) encoding. - Limits.
mis 2 to 256;ef_constructionandef_searchare 1 to 10000. Values outside raiseValueError.
When to choose it
Section titled “When to choose it”- 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.
Is HNSW search exact in SrvDB?
Section titled “Is HNSW search exact in SrvDB?”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.
Related concepts
Section titled “Related concepts”Next: What is an IVF index?
Papers and further reading
Section titled “Papers and further reading”- 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
Was this page helpful? Tell us in Discussions.