Use an inverted-file vector index when partition scans beat a full scan, and evaluate the recall cost of probing fewer cells.
IVF vector search: train centroids, budget probes, measure misses
IVF partitions vector space
An inverted-file vector index assigns each stored embedding to a coarse centroid and keeps a posting list for each centroid. At query time, it finds nearby centroids and scans only their lists. The number of lists is commonly called nlist; the number searched for one query is nprobe. Fewer probes reduce candidate distance calculations but can miss a nearest vector assigned to a neighboring cell. This is an approximate candidate-generation decision, not an answer-quality guarantee. Compare results with exact search under the same vector metric and normalization contract before drawing conclusions about recall.
Train on the distribution you intend to serve
Coarse centroids need representative training vectors from the corpus and embedding model that will actually populate the index. Training only on the first ingestion batch, one language or one high-volume tenant can leave later material in badly balanced cells. Record model revision, dimension, metric, normalization, sample selection and training seed with the index generation. If the embedding distribution changes enough, a fixed centroid set can produce long lists or poor recall even when index size stays constant. Embedding migrations should rebuild and evaluate the coarse assignment alongside the stored vectors.
Probe count trades work for recall
With a small nprobe, a query scans few lists and risks missing candidates near cell boundaries. A larger nprobe visits more lists, costs more CPU and memory bandwidth, and usually improves recall until gains flatten. If nprobe approaches every list, an uncompressed IVFFlat search approaches an exhaustive vector scan while adding coarse-routing overhead. An IVF index with compressed payloads can still differ from an exact flat index because compression changes distances. Quantization and exact reranking should be measured as a separate source of error rather than folded into the probe-count result.
Filters and skew alter the effective budget
A query restricted to one tenant or status may have few eligible vectors in the first probed cells. Some backends filter before vector search, others overfetch and filter afterward; the two plans have different recall and latency behavior. List imbalance also matters: probing four huge cells may scan more vectors than probing eight small ones. Measure eligible candidate count, list-size distribution, probes visited, p95 latency and exact top-K recall by filter slice. Filtered ANN tests expose an apparently fast configuration that returns too few authorized results. Never compensate for a failed permission filter by searching a broader scope.
Use a release gate, not a magic nprobe
Select several probe budgets and compare them against exact top-K results on representative queries: common intents, rare terms, new content, multilingual requests and difficult filter combinations. Record both passage recall and downstream supported-answer rate. The best value can differ by index size, hardware and query slice, but per-query adaptation needs a clear ceiling so tail latency remains bounded. HNSW candidate budgets have a related speed-versus-recall shape but use a different search structure. Review both index memory and build time before deciding that IVF is preferable to a graph or flat index.
Worked contract
from math import dist
def probe_cells(query: tuple[float, ...],
centroids: dict[str, tuple[float, ...]],
postings: dict[str, list[tuple[str, tuple[float, ...]]]],
nprobe: int, limit: int) -> list[tuple[str, float]]:
if not query or nprobe < 1 or limit < 1 or nprobe > len(centroids):
raise ValueError("invalid query or search budget")
if any(len(vector) != len(query) for vector in centroids.values()):
raise ValueError("centroid dimension mismatch")
cells = sorted(centroids, key=lambda cell: (dist(query, centroids[cell]), cell))
candidates = []
for cell in cells[:nprobe]:
for document_id, vector in postings.get(cell, []):
if len(vector) != len(query):
raise ValueError("passage dimension mismatch")
candidates.append((document_id, dist(query, vector)))
return sorted(candidates, key=lambda item: (item[1], item[0]))[:limit]
centroids = {"west": (0.0, 0.0), "east": (8.0, 0.0)}
postings = {"west": [("manual-47", (2.5, 0.0))],
"east": [("manual-81", (4.1, 0.0))]}
query = (3.95, 0.0)
assert probe_cells(query, centroids, postings, 1, 1)[0][0] == "manual-47"
assert probe_cells(query, centroids, postings, 2, 2)[0][0] == "manual-81"Performance and operating cost
For C centroids, the toy code scores and sorts all centroids in O(CD + C log C) time for D dimensions, then scores M vectors in the selected lists in O(MD) and sorts them in O(M log M). It uses O(C + M) temporary space. Real implementations use trained coarse quantizers, compressed lists and efficient top-K selection, but the fundamental probe-versus-recall tradeoff remains. The demonstration does not train centroids or enforce filters; those are production responsibilities. Log actual scanned-vector count, because nprobe alone hides cell-size skew.
Common Mistakes
- Training centroids on vectors from an old embedding model or an unrepresentative tenant.
- Reporting ANN recall against another approximate index instead of an exact baseline.
- Assuming the same nprobe scans the same amount of work for every filtered query.
- Attributing compressed-distance errors entirely to the IVF probe setting.
Validation drill
Train one IVF generation on a stratified corpus sample, then test 93 judged queries at four probe budgets. Report exact top-12 recall, eligible-candidate counts, list-size skew and p95 latency by tenant and language. Repeat after the next substantial ingestion wave.
