Skip to main content

ruvector_core/
index.rs

1//! Index structures for efficient vector search
2
3pub mod flat;
4#[cfg(feature = "hnsw")]
5pub(crate) mod flat_graph;
6#[cfg(feature = "hnsw")]
7pub mod hnsw;
8#[cfg(feature = "hnsw")]
9pub mod turbo4;
10
11use crate::error::Result;
12use crate::types::{SearchResult, VectorId};
13
14/// Trait for vector index implementations
15pub trait VectorIndex: Send + Sync {
16    /// Add a vector to the index
17    fn add(&mut self, id: VectorId, vector: Vec<f32>) -> Result<()>;
18
19    /// Add multiple vectors in batch
20    fn add_batch(&mut self, entries: Vec<(VectorId, Vec<f32>)>) -> Result<()> {
21        for (id, vector) in entries {
22            self.add(id, vector)?;
23        }
24        Ok(())
25    }
26
27    /// Search for k nearest neighbors
28    fn search(&self, query: &[f32], k: usize) -> Result<Vec<SearchResult>>;
29
30    /// Search with a per-query candidate-list size (HNSW `efSearch`). `None` means the index's configured value.
31    /// Indexes without such a knob ignore it.
32    fn search_ef(
33        &self,
34        query: &[f32],
35        k: usize,
36        _ef_search: Option<usize>,
37    ) -> Result<Vec<SearchResult>> {
38        self.search(query, k)
39    }
40
41    /// Remove a vector from the index
42    fn remove(&mut self, id: &VectorId) -> Result<bool>;
43
44    /// Get the number of vectors in the index
45    fn len(&self) -> usize;
46
47    /// Check if the index is empty
48    fn is_empty(&self) -> bool {
49        self.len() == 0
50    }
51}