# How Does IVF Indexing Work?

> How IVF sorts stored vectors into lists, why a search opens only a few of them, and the trade-off it makes, explained simply for your AI interview.

**In short:** IVF sorts the stored vectors into lists, one per cluster centre. A search compares the question with the centres, then opens only the few lists nearest to it, so most of the data is never read. It is faster, but it can miss the best match when that match sits in a list nobody opened.

AI and LLM interview question Q121, Retrieval-augmented generation (RAG). Advanced, occasional (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/how-does-ivf-indexing-work

## Say it in 30 seconds

IVF stands for inverted file. When I build the index, I first pick a set of cluster centres, then file every vector in the list of its nearest centre. At search time I compare the question only with the centres, open the few nearest lists, and compare inside them. That skips most of the data, so it is fast. The catch is that the best match can sit in a list I didn't open, so I tune how many lists I open against the recall I need.

## Say it in 2 minutes

Picture a records clerk at a law firm. Contracts are sorted into labelled drawers by topic, so when a lawyer asks about late-payment clauses, the clerk opens the few drawers that fit instead of searching all 40,000 contracts. IVF does the same with vectors. A vector is a list of numbers that stands for the meaning of a clause, and IVF groups them so a search can skip most of them.

The build has two steps. First a clustering step picks a set of centres, called centroids, that cover the data. Then each vector joins the list of its nearest centre. Those lists are the inverted lists, and the whole store is the inverted file.

At search time the question vector is compared only with the centres. The nearest few lists are opened, and only their vectors are compared in full. The number of lists opened is called nprobe. If nprobe is 1, the search is fastest and most likely to miss a match that sits just across a boundary. Raising nprobe finds more matches but reads more data. Opening every list is exact only when the vectors in the lists are stored uncompressed, and then it saves no work.

In a legal search over ten million clauses, I would measure recall on questions with known answers, pick the smallest nprobe that meets the bar, and test again after each data refresh. IVF pairs well with compression, which is why it suits very large sets.

## What is IVF indexing, in plain words?

An AI search tool stores each item as a vector: a long list of numbers that stands for its meaning. To find the items closest to a question, it compares the question's vector with the stored ones. Doing that against ten million vectors on every search is slow.

IVF, short for inverted file, is a shortcut. It sorts the stored vectors into groups first, then searches only the few groups most likely to hold the answer. Faiss, an open-source toolkit for vector search, describes it as clustering the data at indexing time and searching only part of it.

Think of a records clerk at a law firm. Contracts sit in labelled drawers by topic. A lawyer asks about late-payment clauses, and the clerk opens the three drawers that fit instead of all 40,000 contracts. The drawers are the lists, and the labels are the centres.

Here is the same idea on a real job: a legal team searches ten million clauses by meaning. IVF lets the search open a small share of them and still find the right clause often.

- **Vector:** A list of numbers that stands for the meaning of a clause, photo or document.
- **Centroid:** A cluster centre. Each list is built around one centroid.
- **Inverted list:** The group of vectors filed under one centroid, stored together.
- **nprobe:** How many lists a search opens. More finds more matches but reads more data.
- **Recall:** The share of true best matches a search actually finds.

## How does IVF build its index?

Building has two steps. A clustering step first picks a set of cluster centres that cover the data. Each centre is a centroid. Then every stored vector joins the list of its nearest centre.

1. **Pick the centres:** A clustering step, such as k-means, learns a set of centres from the stored vectors. More centres give smaller lists.
2. **File every vector:** Each vector is filed in the list of its nearest centre. The lists are the inverted lists, and the whole store is the inverted file.
3. **Fix the list count:** The number of lists, called nlist in Faiss, is a field on the IndexIVF object.

*Diagram, Building an IVF index: Stored vectors teach the cluster centres, then each vector is filed under its nearest centre. The search later uses these lists.*
1. A clustering step learns the centres from the stored vectors.
2. Each vector joins the list of its nearest centre.

> "IVF indexing is a technique that clusters the database vectors at indexing time." (The Faiss library paper, version 4)

## How does a search use IVF, and what is nprobe?

A search compares the question only with the centroids. It then opens the closest few lists and compares the question with every vector inside them. Faiss calls the number of lists opened nprobe.

A made-up example with equal-sized lists: one million vectors in 1,000 lists, each about 1,000 long. Opening 10 lists means 1,000 centroid comparisons plus about 10,000 vector comparisons, so 11,000 in total. A full scan would compare all one million, so IVF does about 1 per cent of the work. Real lists are uneven, so the real count moves around.

*Diagram, One question, two nprobe settings: The same question with two settings. Opening one list misses the best clause; opening two finds it. The counts come from the small demo below.*
1. With nprobe 1, only the nearest list is opened.
2. With nprobe 2, the two nearest lists are opened.
3. The best clause sits in a list that was not opened, so it is missed.
4. The second list holds it, so the search finds it.

Here is the same idea in a short piece of plain Python. The twelve clauses stand in for a whole contract library, the three centres are hand-set stand-ins for what clustering would learn, and every number is made up.

```python
# A made-up clause search: 12 clause vectors (made-up 2-D numbers) split into 3 lists.
clauses = {
    "c01": (1.0, 1.2), "c02": (0.8, 0.9), "c03": (1.3, 1.0), "c04": (1.1, 0.7),
    "c05": (5.0, 5.2), "c06": (5.4, 4.8), "c07": (4.7, 5.5), "c08": (5.2, 5.0),
    "c09": (9.0, 1.0), "c10": (8.6, 1.3), "c11": (9.3, 0.8), "c12": (8.8, 1.1),
}
centroids = [(1.1, 1.0), (5.1, 5.1), (9.0, 1.0)]

def dist2(a, b):
    return (a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2

# Build: file each clause in the list of its nearest centroid.
lists = {i: [] for i in range(len(centroids))}
for name, vec in clauses.items():
    lists[min(range(len(centroids)), key=lambda i: dist2(vec, centroids[i]))].append(name)

# Search: visit only the nprobe nearest lists, then compare inside them.
def search(query, nprobe):
    picked = sorted(range(len(centroids)), key=lambda i: dist2(query, centroids[i]))[:nprobe]
    seen = [n for i in picked for n in lists[i]]
    best = min(seen, key=lambda n: dist2(query, clauses[n]))
    return best, len(seen), picked

query = (3.2, 3.0)
print("lists:", lists)
print("exact best:", min(clauses, key=lambda n: dist2(query, clauses[n])))
for nprobe in (1, 2, 3):
    best, seen, picked = search(query, nprobe)
    print(f"nprobe={nprobe}: lists {picked}, compared {seen} of 12, best {best}")
```

Output:

```text
lists: {0: ['c01', 'c02', 'c03', 'c04'], 1: ['c05', 'c06', 'c07', 'c08'], 2: ['c09', 'c10', 'c11', 'c12']}
exact best: c03
nprobe=1: lists [1], compared 4 of 12, best c08
nprobe=2: lists [1, 0], compared 8 of 12, best c03
nprobe=3: lists [1, 0, 2], compared 12 of 12, best c03
```

The output shows the miss. Opening one list returns c08, while the exact best match is c03. Opening a second list finds c03, after comparing 8 of the 12 clauses.

## What does IVF cost you, and when is it the right choice?

Faiss describes two ways to skip most of the data, inverted files and graph-based search. Each one trades memory and speed against accuracy.

- **Graph-based: Needs memory to spare.** Good when memory is not the limit, typically for indexes below 1M vectors. Building it gets slow beyond 10M vectors.
- **IVF: Lighter, but approximate.** It opens only some lists, so it can miss matches. It pairs well with compression for very large sets.

> "For larger indexes, where compression is required to even fit the database vectors in memory, IVF indexes are the only option." (The Faiss library paper, version 4)

- **Recall is lost at the edges:** A true match just across a boundary sits in a list the search never opened.
- **Lists can grow unequal:** Faiss measures this as an imbalance factor. Lopsided lists make some searches much slower.
- **Training data has to look like real data:** Centres learned from a different mix of documents put the lists in the wrong places.

Compressing the vectors is what makes IVF fit very large sets, and it is covered in the product quantization answer.

### How would you size nlist and nprobe for ten million clauses?

I would start from the memory budget and the recall target. Then I would run real questions that have known correct answers at several nprobe values, and pick the smallest nprobe that meets the bar.

> **Tip:** Say that the right setting depends on the data and the accuracy you need, then show the test you would run to find it.

## What does the interviewer want to hear?

**A strong answer covers**

- [ ] IVF in one sentence: vectors are grouped by nearest centre, and a search opens a few groups.
- [ ] The two build steps: learn the centres, then file each vector.
- [ ] nprobe as the speed and recall dial, set per query.
- [ ] Where it misses: the best match sits in a list that was not opened.
- [ ] When to pick it: very large sets, usually with compression.
- [ ] How you tune it: measure recall on real questions with known answers.

**Common traps**

- Trap: "It is an exact search, just faster." Better: It is approximate. It skips lists, so it can miss the best match.
- Trap: "More probes always means a slower search." Better: Opening more lists raises recall at a cost in speed. Measure both before you pick nprobe.
- Trap: "Set the list count once and forget it." Better: Watch list sizes and recall after each data refresh, since lopsided lists slow searches.

## Can you spot the risk?

**Spot the risk:** A 10 million vector index misses true matches often. Which setting causes the most misses?

A. nlist=4096, nprobe=1 (one list opened per search)
B. nlist=4096, nprobe=64 (64 lists opened per search)
C. nlist=64, nprobe=64 (every list opened, a full scan)
D. nlist=4096, nprobe=16 (16 lists opened per search)

**Answer:** A. Opening one list out of 4,096 reads the least data, so the best match is most likely to sit in a list that was never opened. Opening every list is exact only if the vectors are uncompressed, and then it is slow.

**Quick check:** A legal search misses the right clause in about 1 search in 5. What is the most direct setting to change?

A. Raise nprobe so more lists are opened
B. Lower nprobe so fewer lists are opened
C. Store the clauses as plain text
D. Shorten the clause titles

**Answer:** A. Missed matches can come from lists that were never opened, or from lost detail when the vectors are compressed. Opening more lists raises recall at a cost in speed. Titles and storage do not change which lists a query opens.

## Can you answer these from memory?

**Flash cards**

- **What does IVF stand for?** Inverted file. Vectors are filed into lists, and a search opens only some of them.
- **What is a centroid?** A cluster centre. Each inverted list is built around one centroid.
- **What does nprobe control?** How many lists a search opens. More finds more matches but reads more data.
- **Where does IVF lose matches?** When the best match sits in a list the search did not open, or when compressed vectors lose detail.
- **Is IVF exact?** No. Only opening every list, with uncompressed vectors, is exact, and that gives up the speed.
- **When is IVF the only option?** When compression is needed to fit the vectors in memory, per the Faiss paper.

## Likely follow-up questions

- Q120 How does HNSW work? https://designervenkat.online/interview/ai/how-does-hnsw-work
- Q122 What is product quantization? https://designervenkat.online/interview/ai/what-is-product-quantization
- Q119 What is approximate nearest-neighbour search? https://designervenkat.online/interview/ai/what-is-approximate-nearest-neighbour-search
- Q117 What is a vector database? https://designervenkat.online/interview/ai/what-is-a-vector-database
- Q123 What is hybrid search (BM25 + vectors)? https://designervenkat.online/interview/ai/what-is-hybrid-search
- Q118 Cosine vs dot product vs Euclidean distance? https://designervenkat.online/interview/ai/cosine-vs-dot-product-vs-euclidean-distance
