Module 5.1
Module 1.2 found the nearest neighbor by checking all 6 Nimbus documents — brute force. A real vector database almost never does that. It gets fast by being clever about which candidates it even bothers to compare.
The math
Both IVF and HNSW are approximate — they can occasionally miss the true nearest neighbor to save that much work. At the toy scale below, the corpus is small enough that all three still land on the right answer.
The code
def brute_force(query, corpus):
return max(corpus, key=lambda doc: cosine_sim(query, doc.vec))
def ivf_search(query, index, n_probe=1):
nearest_clusters = index.nearest_centroids(query, n_probe)
candidates = [doc for c in nearest_clusters for doc in c.members]
return max(candidates, key=lambda doc: cosine_sim(query, doc.vec))
# HNSW isn't something you'd hand-roll — real code just calls a library:
import hnswlib
index = hnswlib.Index(space='cosine', dim=384)
index.add_items(corpus_vecs)
labels, distances = index.knn_query(query_vec, k=1) # ~O(log n) internally
Chroma, Pinecone, Weaviate, FAISS, and pgvector all implement some version of IVF or HNSW under the hood — this is what "vector database" actually means at the algorithm level.
The Nimbus thread
At 6 documents, brute force is instant and this whole page is academic. The moment Nimbus's support corpus grows to real scale — years of tickets, every doc revision, every internal wiki page — brute force's O(n) starts costing real, measurable latency on every single query. That's the exact point where an index (IVF or HNSW) stops being an optimization and becomes a requirement.
So what
A vector database doesn't do the math faster — it's clever about which candidates it even looks at.