pub struct Index { /* private fields */ }Expand description
In-memory flat ANN index.
Implementations§
Source§impl Index
impl Index
Sourcepub fn add(&mut self, id: impl Into<String>, vector: &[f32]) -> Result<()>
pub fn add(&mut self, id: impl Into<String>, vector: &[f32]) -> Result<()>
Insert a single vector.
Sourcepub fn remove(&mut self, id: &str) -> bool
pub fn remove(&mut self, id: &str) -> bool
Remove the first entry whose id matches. Returns true if found.
O(n) — uses swap-remove so the rest of the index isn’t shifted.
dim is preserved even after the index becomes empty.
Sourcepub fn save<P: AsRef<Path>>(&self, path: P) -> Result<()>
pub fn save<P: AsRef<Path>>(&self, path: P) -> Result<()>
Persist the entire index (metric, dim, ids, vectors) to a JSON
file. Re-load with Index::load. JSON is verbose for f32 arrays
but cross-platform and debuggable; binary persistence is on the
v0.2 list.
Sourcepub fn load<P: AsRef<Path>>(path: P) -> Result<Self>
pub fn load<P: AsRef<Path>>(path: P) -> Result<Self>
Reverse of Index::save. Loads an index previously saved with
the same crate version.
Sourcepub fn add_batch(
&mut self,
ids: Vec<String>,
matrix: &ArrayView2<'_, f32>,
) -> Result<()>
pub fn add_batch( &mut self, ids: Vec<String>, matrix: &ArrayView2<'_, f32>, ) -> Result<()>
Insert many vectors. matrix is (n, d).
Sourcepub fn search(&self, query: &[f32], k: usize) -> Result<Vec<Hit>>
pub fn search(&self, query: &[f32], k: usize) -> Result<Vec<Hit>>
Top-k search. query is a 1-D vector of the same dimension as
stored vectors.
Sourcepub fn search_batch(
&self,
queries: &ArrayView2<'_, f32>,
k: usize,
parallel: bool,
) -> Result<Vec<Vec<Hit>>>
pub fn search_batch( &self, queries: &ArrayView2<'_, f32>, k: usize, parallel: bool, ) -> Result<Vec<Vec<Hit>>>
Batch search. queries is (n_q, d). With parallel = true, each
query runs on a rayon thread.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Index
impl<'de> Deserialize<'de> for Index
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Auto Trait Implementations§
impl Freeze for Index
impl RefUnwindSafe for Index
impl Send for Index
impl Sync for Index
impl Unpin for Index
impl UnsafeUnpin for Index
impl UnwindSafe for Index
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more