FAISS only has IndexFlatL2 and IndexFlatIP. My model's docs say to use cosine similarity. Which index should I use?
How the measures relate
Cosine similarity compares only the direction of two vectors and ignores their length. Dot product (inner product) depends on both direction and length. L2 (Euclidean) distance measures straight-line distance.
When all vectors are normalized to length 1, the three give the same ranking. Cosine similarity equals the dot product, and L2 distance becomes a simple function of it. So the practical rule is: normalize your vectors, then use inner product.
Answer 1: Cosine similarity in FAISS with normalization and IndexFlatIP
Normalize both the document vectors and every query vector:
vecs = np.ascontiguousarray(doc_vecs, dtype="float32")
faiss.normalize_L2(vecs)
index = faiss.IndexFlatIP(vecs.shape[1])
index.add(vecs)
q = np.ascontiguousarray(query_vec.reshape(1, -1),
dtype="float32")
faiss.normalize_L2(q) # forgetting this is a very common bug
scores, ids = index.search(q, 5) # scores are cosine similarities in [-1, 1]
If you normalize the documents but not the query, the ranking is still correct (all scores are scaled by the same query length), but the scores themselves are no longer cosine values, so thresholds won't work.
