IVF Index: Clustering-Based Vector Search & Partitioning › Worked Example
Language AI HandbookAll chapters IVF Index: Clustering-Based Vector Search & Partitioning › Worked ExampleOne section · Sections in this chapter4 / 12 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Let's walk through a concrete 2D example to ground the abstract concepts in tangible calculations. Working in two dimensions lets us follow every computation by hand and verify our geometric intuition. Suppose we have 12 vectors and partition them into clusters.
Training phase: K-means identifies three centroids. These are the points that minimize the within-cluster variance for their respective groups:
Assignment phase: Each vector is assigned to its nearest centroid, creating three inverted lists. Notice how the vectors in each cell are geographically close to their centroid. This is exactly the grouping property that makes IVF effective:
| Cell | Centroid | Vectors |
|---|
| 1 | | | | 2 | | | | 3 | | |
: Vector assignments to IVF cells after k-means clustering. {#tbl-ivf-assignments}
Each cell contains exactly 4 vectors, giving us perfectly balanced inverted lists. In practice, cells are rarely this uniform; the balance here makes the example easier to follow.
Query phase: A query arrives, and we set .
where is the query vector and means we will search the 2 cells whose centroids are closest to the query.
The first task is to determine which cells are most promising. We compute distances from the query to all centroids using Euclidean distance. The Euclidean distance between two 2D points and is:
where:
- : the coordinates of the first point
- : the coordinates of the second point
- : the sum of squared differences in each dimension
Taking the square root of this sum gives us the actual straight-line geometric distance and ensures the distance obeys the triangle inequality.
Applying this formula to each centroid, we can determine which regions of space are closest to our query:
where:
- : the Euclidean distance between the query vector and centroid
- : the query vector coordinates
- : the first centroid coordinates
- : the second centroid coordinates
- : the third centroid coordinates
Ranking these distances from smallest to largest, centroid is closest at distance 2.24, followed by centroid at distance 4.47, and finally centroid at distance 5.0. The two closest centroids are (distance 2.24) and (distance 4.47). With , we search the inverted lists for cells 3 and 2, scanning 8 of the 12 vectors. We skip cell 1 entirely, saving those 4 distance computations. In this small example, we save only 4 comparisons, but in a real dataset with millions of vectors and hundreds of centroids, this same principle eliminates the large majority of distance computations.
Among the 8 vectors searched, the closest to is at distance 3.0 from cell 3:
where:
- : the vector from the scanned cells that is nearest to the query
- : the Euclidean distance calculation between query and result vector
- distance 3.0: the final computed distance
The zero contribution from the x-dimension shows the vector is directly above the query, differing only in the y-coordinate.
Note that if the actual nearest neighbor had been in cell 1 (which it isn't in this case), we would have missed it. This is the approximation trade-off. The decision to skip cell 1 was made based solely on centroid distances. While centroids are good summaries of their cells, they are not perfect predictors of which cell contains the nearest neighbor. In this example the approximation worked perfectly, but in general there is always a chance that a skipped cell harbors a closer vector, which is why increasing improves recall.
