By default, most Faiss index implementations bind a sequential ID to each vector. If you need to bind vectors to specific, arbitrary IDs, you must use the IdMap<I> wrapper. IdMap<I> wraps an existing index I and provides the necessary mapping functionality while preserving the original index's properties.
Note on GPU usage: IdMap works with GPU-backed indexes, but the ID mapping itself resides in CPU memory. Once an index is wrapped in an IdMap, you cannot move it between the CPU and GPU.
use faiss::{IdMap, Idx, Index, FlatIndex};
let mut index = FlatIndex::new_l2(4)?;
// FlatIndex does not support arbitrary IDs directly
assert!(index.add_with_ids(&[0., 1., 0., 1.], &[Idx::new(5)]).is_err());
// Wrap it in IdMap to enable arbitrary ID support
let mut index = IdMap::new(index)?;
index.add_with_ids(&[0., 1., 0., 1.], &[Idx::new(5)])?;
assert_eq!(index.ntotal(), 1);