Skip to content

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).

IVF: centroids divide space into cells; the query probes the nprobe nearest cells q ● centroid · stored vector probed cell nprobe = 2 of nlist = 6: only shaded cells are searched, each with its own HNSW graph
A vector belongs to the cell of its most similar centroid; a search ranks centroids by cosine and probes the closest nprobe cells.
  1. 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 after max_iterations (20) or when centroids move less than the tolerance. nlist is capped at the number of training vectors.
  2. 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.
  3. Search. Centroids are ranked by cosine distance to the query, the top nprobe partitions are searched in parallel, and the results are merged into the top k with exact cosine scores.
  4. Before training IVF mode searches exactly, like flat.
  5. Save. save() saves ivf.index; it is reloaded on open and retrained if it does not cover the committed vectors. The vectors stay in vectors.bin, so the index can always be rebuilt.
import random
import 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)))
# 5
  • 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 nprobe for recall; nprobe = nlist probes all.
  • Speed. Ranking centroids costs nlist cosine computations; then only about nprobe / nlist of the data is searched.
  • Bytes per vector. Full vectors (4 × D) plus HNSW links in one partition graph (see HNSW), plus nlist × 4 × D bytes 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. nlist and nprobe are 1 to 65536.
  • 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.

Yes. Call train_ivf() after adding data; it clusters up to the first 10000 stored vectors. Until then, IVF mode searches exactly, like flat.

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.

Yes. save() saves ivf.index and it is reloaded on open. If it does not cover the committed vectors, SrvDB retrains.

Next: What is SQ8 scalar quantization?

  • 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