Concepts
What is an IVF index?
How SrvDB's IVF mode clusters vectors with spherical k-means into nlist partitions, probes the nprobe closest at search time, and searches each with its own HNSW graph.
IVF (inverted file) splits the collection into nlist partitions around
learned centroids and, at query time, searches only the nprobe
partitions whose centroids are closest to the query. In SrvDB each
partition has its own HNSW graph, so the search
is coarse (pick partitions) then fine (walk small graphs).
How it works
Section titled “How it works”- Train.
train_ivf()takes up to the first 10000 stored vectors, scales them to unit length and runs spherical k-means: each centroid is the normalised mean direction of its members. It stops aftermax_iterations(20) or when centroids move less than the tolerance.nlistis capped at the number of training vectors. - Assign. Every stored vector, and every vector added later, goes to the partition whose centroid has the highest cosine similarity, and is inserted into that partition’s HNSW graph.
- Search. Centroids are ranked by cosine distance to the query, the
top
nprobepartitions are searched in parallel, and the results are merged into the topkwith exact cosine scores. - Before training IVF mode searches exactly, like flat.
- Save.
save()savesivf.index; it is reloaded on open and retrained if it does not cover the committed vectors. The vectors stay invectors.bin, so the index can always be rebuilt.
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(2000)]ids = [f"doc-{i}" for i in range(len(vectors))]
db = srvdb.create("./ivf-db", dimension=128, index="ivf", nlist=20, nprobe=4)db.add(ids, vectors, ["{}"] * len(ids))db.train_ivf()db.save()print(len(db.search(vectors[42], k=5)))# 5Trade-offs
Section titled “Trade-offs”- Recall. Approximate in two ways: a true neighbour can sit in a
partition that was not probed, and each partition’s HNSW walk is itself
approximate. Raise
nprobefor recall;nprobe = nlistprobes all. - Speed. Ranking centroids costs
nlistcosine computations; then only aboutnprobe / nlistof the data is searched. - Bytes per vector. Full vectors (
4 × D) plus HNSW links in one partition graph (see HNSW), plusnlist × 4 × Dbytes of centroids for the whole index. - Training. Needs data first and a call to
train_ivf(). If the data drifts, centroids trained on old data fit it less well; retrain. - Limits.
nlistandnprobeare 1 to 65536.
When to choose it
Section titled “When to choose it”- Large collections whose data is mostly present before you train.
- When you want an explicit recall knob (
nprobe) that maps to how much of the data is read. - For data that arrives gradually with no training step, prefer HNSW or RaBitQ.
Does IVF in SrvDB need training?
Section titled “Does IVF in SrvDB need training?”Yes. Call train_ivf() after adding data; it clusters up to the first
10000 stored vectors. Until then, IVF mode searches exactly, like flat.
What do nlist and nprobe mean?
Section titled “What do nlist and nprobe mean?”nlist is the number of partitions (centroids) and nprobe is how many of
the closest partitions a search looks in. The defaults are 100 and 10.
Is the trained IVF index saved?
Section titled “Is the trained IVF index saved?”Yes. save() saves ivf.index and it is reloaded on open. If it does
not cover the committed vectors, SrvDB retrains.
Related concepts
Section titled “Related concepts”Next: What is SQ8 scalar quantization?
Papers and further reading
Section titled “Papers and further reading”- Josef Sivic, Andrew Zisserman (2003). Video Google: A Text Retrieval Approach to Object Matching in Videos. ICCV 2003. The inverted file over learned visual words.
- Hervé Jégou, Matthijs Douze, Cordelia Schmid (2011). Product Quantization for Nearest Neighbor Search. IEEE TPAMI 33(1). Introduces IVFADC, the inverted file used with compressed codes. doi:10.1109/TPAMI.2010.57
Was this page helpful? Tell us in Discussions.