Start studying
Module 8 · Embeddings & Search Module contents
Module 8  ·  Card 15 / 17intermediate

IVF Indexes

An inverted file index clusters the vectors first, then searches only the nearest few clusters.

Each cluster has a centroid, and a query is compared against those centroids rather than every vector.

Searching four clusters out of a thousand cuts the work by roughly the same factor.

The parameter that matters is how many clusters to probe: more means better recall and more time.

Vectors near a boundary can be missed, which is the recall cost you accept for the speed.

Learn in depth ↓
Module 08 · Embeddings & Search · 17 concepts
In depth  /  15 lines

IVF Indexes, in more detail

Exact nearest-neighbour search compares the query with every stored vector, and that does not scale.

An inverted file index, universally shortened to IVF, buys speed by partitioning the space first.

Building it runs k-means over a sample of the vectors to find a set of centroids.

Every vector is then assigned to its nearest centroid, forming one list per cluster.

The name comes from text search, where an inverted file maps a term to the documents containing it.

At query time the vector is compared against the centroids alone, which is a much smaller set.

The closest few clusters are selected and only the vectors inside them are scanned.

Two parameters control everything: the number of clusters and the number probed per query.

A common starting point is roughly the square root of the collection size for the cluster count.

Probing more clusters raises recall and costs time in an almost linear trade.

The failure mode is inherent to partitioning: a true neighbour may sit just across a cluster boundary.

If its centroid is not among those probed, that neighbour is never even considered.

IVF is usually paired with product quantization so the scanned vectors are also compressed.

That combination is what makes billion-scale search fit on a single machine.

Compared with HNSW it builds faster and uses less memory, while HNSW generally wins on recall at a given latency.

Deep dive

What is underneath IVF Indexes

Partition first, then search a few partitions

{μ1,…,μnlist}=k-means(X)\{\boldsymbol{\mu}_1, \dots, \boldsymbol{\mu}_{n_{\mathrm{list}}}\} = \mathrm{k\text{-}means}(X)

The corpus is clustered once, and every vector is assigned to its nearest centroid. Those clusters are the inverted lists.

A query is compared against the centroids first, which is cheap, and then only against the vectors inside the closest few clusters.

nprobenlist\frac{n_{\mathrm{probe}}}{n_{\mathrm{list}}}

That fraction is the share of the corpus actually examined. With 4,096 lists and 16 probed, a query touches 0.4 percent of the vectors.

The two parameters

nlist≈nn_{\mathrm{list}} \approx \sqrt{n}

The usual rule sets the number of clusters near the square root of the corpus size, which balances the cost of scanning centroids against the cost of scanning lists.

cost≈nlist⋅d+nprobenlist⋅n⋅d\text{cost} \approx n_{\mathrm{list}} \cdot d + \frac{n_{\mathrm{probe}}}{n_{\mathrm{list}}} \cdot n \cdot d

Both terms are in that expression. Too few clusters and each list is enormous. Too many and finding the right ones becomes its own scan.

nproben_{\mathrm{probe}}

The probe count is the query-time dial: 1 is fast and misses a lot, 64 approaches a brute-force scan, and the recall curve between them is steep at first and then flat.

The boundary problem

A query near the edge of a cluster has its true nearest neighbour in the adjacent cluster, which is not probed unless nproben_{\mathrm{probe}} is large enough to include it.

That is the structural weakness of partitioning, and it is why recall at nprobe=1n_{\mathrm{probe}} = 1 is usually poor even though the partition is good.

Probing several neighbouring clusters fixes it statistically rather than exactly, which is the honest description of what any IVF index guarantees.

One index, in order

  1. Sample a few hundred thousand vectors and run k-means to get the centroids.
  2. Assign every vector in the corpus to its nearest centroid, building the lists.
  3. Store each list contiguously, so scanning it is a sequential read.
  4. At query time, score the centroids and pick the nearest nproben_{\mathrm{probe}}.
  5. Scan those lists exactly and keep the global top kk.

Training on a sample is standard and safe, since the centroids only have to describe the shape of the distribution rather than every point.

Why it scales where a graph does not

Only the centroids and the current lists need to be resident, so a billion-vector index runs on a machine that could not hold a tenth of it as a graph.

Combined with product quantisation, each vector inside a list is stored compressed, which cuts memory by an order of magnitude and makes the scan faster still.

Insertion is cheap because a new vector joins one list, and deletion simply removes it, which is a real operational advantage over a graph index.

The cost is recall per millisecond. HNSW is faster at the same recall for anything that fits in memory, and IVF wins the moment it does not.

Test yourself

One question on this card. Every option explains itself once you pick, right or wrong.

Question 1
How does an IVF index avoid comparing the query against every vector?
Embeddings & Search

17 concepts in this module, then a graded test with every answer explained.

Study this module →
↑↓ move↵ openesc close