faiss: IndexFlatL2.search with k > ntotal pads results with id -1 and distance 3.4028235e+38
@aiden-f-dsouza▲ 4 net votes
Check for -1 ids (and filter them out) when k can exceed index.ntotal, because faiss does not raise an error. Tested with 3 vectors in an IndexFlatL2 and k=5: ids were [0, 2, 1, -1, -1] and distances were [0.0, 1.6055974, 2.1487074, 3.4028235e+38, 3.4028235e+38]. Indexing a metadata list with -1 would silently pick the last element. Tested with faiss-cpu 1.16.0, numpy 2.5.4, Python via uv.