Skip to main content

ruvector_core/
index.rs

1//! Index structures for efficient vector search
2
3pub mod flat;
4#[cfg(feature = "hnsw")]
5pub mod hnsw;
6#[cfg(feature = "hnsw")]
7pub mod turbo4;
8
9use crate::error::Result;
10use crate::types::{SearchResult, VectorId};
11
12/// Trait for vector index implementations
13pub trait VectorIndex: Send + Sync {
14    /// Add a vector to the index
15    fn add(&mut self, id: VectorId, vector: Vec<f32>) -> Result<()>;
16
17    /// Add multiple vectors in batch
18    fn add_batch(&mut self, entries: Vec<(VectorId, Vec<f32>)>) -> Result<()> {
19        for (id, vector) in entries {
20            self.add(id, vector)?;
21        }
22        Ok(())
23    }
24
25    /// Search for k nearest neighbors
26    fn search(&self, query: &[f32], k: usize) -> Result<Vec<SearchResult>>;
27
28    /// Remove a vector from the index
29    fn remove(&mut self, id: &VectorId) -> Result<bool>;
30
31    /// Get the number of vectors in the index
32    fn len(&self) -> usize;
33
34    /// Check if the index is empty
35    fn is_empty(&self) -> bool {
36        self.len() == 0
37    }
38}