Brute-force search over millions of vectors takes seconds per query. I tried IndexIVFFlat and got an error about is_trained.
Why this happens
IndexFlatL2 and IndexFlatIP compare the query with every stored vector. This gives exact results but becomes slow at large scale. Approximate indexes are much faster but need extra setup. IVF indexes group vectors into clusters and need to learn those clusters from data before you add anything, which is what the is_trained error is about.
Answer 1: Use an IVF index, and train it first
nlist = 1024 # number of clusters
quantizer = faiss.IndexFlatIP(dim)
index = faiss.IndexIVFFlat(quantizer, dim, nlist,
faiss.METRIC_INNER_PRODUCT)
index.train(train_vecs) # must come before add()
index.add(doc_vecs)
index.nprobe = 16 # clusters searched per query
D, I = index.search(q, 5)
Train on a representative sample, ideally at least 30–40 times nlist vectors. nprobe controls the trade-off between speed and accuracy: higher values search more clusters, which is slower but finds more of the true nearest neighbors. Remember to pass the metric explicitly; IVF indexes default to L2.
