Skip to content

Instantly share code, notes, and snippets.

@Enet4
Created April 12, 2018 19:02
Show Gist options
  • Save Enet4/a04ceef2f1aa0605f65065021b4ee0c9 to your computer and use it in GitHub Desktop.
Save Enet4/a04ceef2f1aa0605f65065021b4ee0c9 to your computer and use it in GitHub Desktop.
Index trait in faiss-rs
/// Interface for a Faiss index.
pub trait Index {
/// Whether the index is trained
fn is_trained(&self) -> bool;
/// The total number of vectors indexed
fn ntotal(&self) -> u64;
/// The dimensionality of the indexed vectors
fn d(&self) -> u32;
/// The metric type assumed by the index
fn metric_type(&self) -> MetricType;
/// Add new data vectors to the index.
/// This assumes a contiguous memory slice of vectors, where the total
/// number of vectors is `x.len() / d`.
fn add(&mut self, x: &[f32]) -> Result<()>;
/// Add new data vectors to the index with ids.
/// This assumes a contiguous memory slice of vectors, where the total
/// number of vectors is `x.len() / d`.
/// Not all index types may support this operation.
fn add_with_ids(&mut self, x: &[f32], xids: &[Idx]) -> Result<()>;
/// Train the underlying index with the given data.
fn train(&mut self, x: &[f32]) -> Result<()>;
/// Similar to `search`, but only provides the labels.
fn assign(&mut self, q: &[f32], k: usize) -> Result<AssignSearchResult>;
/// Perform a search for the `k` closest vectors to the given query vectors.
fn search(&mut self, q: &[f32], k: usize) -> Result<SearchResult>;
/// Perform a ranged search for the vectors closest to the given query vectors
/// by the given radius.
fn range_search(&mut self, q: &[f32], radius: f32) -> Result<RangeSearchResult>;
/// Clear the entire index.
fn reset(&mut self) -> Result<()>;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment