IVF FLAT INDEX
A trained IVF-Flat vector index (nlist = 100) for Lab 17.2: HNSW vs IVF.
A trained FAISS IVF-Flat index over 5,000 English sentences embedded with BAAI/bge-small-en-v1.5 (384 dimensions, unit length,
so inner product = cosine similarity). Made for the course 23CSE528 "NoSQL Databases using MongoDB", Lab 17.2, which compares HNSW
and IVF vector indexes (MongoDB Atlas $vectorSearch offers HNSW only, so FAISS is used for IVF).
What was trained: the 100 cluster centres (k-means over the 5,000 vectors, faiss.IndexIVFFlat.train). That is the whole
"model": there are no neural-network weights here, and the embedding model itself is the unmodified pre-trained bge-small-en-v1.5.
Files
| File | Content |
|---|---|
ivf_flat_nlist100.faiss |
the trained index with all vectors (7.9 MB) |
centroids.npy |
the 100 learned centres, shape (100, 384) |
embeddings.npy |
the vectors, float32 (5000, 384); row i is sentence i |
sentences.jsonl |
{"id", "text"} per line |
metrics.json |
recall and latency numbers below |
Use
import faiss, json, numpy as np
from fastembed import TextEmbedding
index = faiss.read_index("ivf_flat_nlist100.faiss")
index.nprobe = 10 # clusters scanned per query (1..100)
texts = [json.loads(l)["text"] for l in open("sentences.jsonl", encoding="utf-8")]
q = np.array(list(TextEmbedding("BAAI/bge-small-en-v1.5").embed(["how are connections pooled?"])), dtype="float32")
q /= np.linalg.norm(q, axis=1, keepdims=True)
scores, ids = index.search(q, 5)
print([texts[i] for i in ids[0]])
Results (100 queries, top-20, recall against exact search, one CPU thread)
| nprobe | recall@20 | latency ms / query |
|---|---|---|
| 1 | 31.1% | 0.064 |
| 5 | 64.8% | 0.065 |
| 10 | 78.3% | 0.083 |
| 25 | 93.3% | 0.152 |
| 50 | 98.1% | 0.277 |
| 100 | 100.0% | 0.511 |
nprobe = 100 scans every cluster, so it equals exact search. Smaller nprobe is faster and less accurate.
Build (k-means training + adding vectors): 0.23 s.
Limits
The index file and vectors are course material. The sentences come from the docstrings of open-source Python packages, each under its own licence, so check those before reusing the text.
The sentences are docstring sentences from Python packages, so the index is a teaching example, not a general-purpose search index.
Rebuilding on different text needs re-training (index.train) because the centres depend on the data.