pub struct HnswIndex { /* private fields */ }Expand description
HNSW Index for approximate nearest neighbor search
Implementations§
Source§impl HnswIndex
impl HnswIndex
Sourcepub fn new(config: HnswConfig) -> Self
pub fn new(config: HnswConfig) -> Self
Create a new HNSW index
Sourcepub fn build(&mut self, embeddings: &HashMap<String, Vec<f32>>) -> Result<()>
pub fn build(&mut self, embeddings: &HashMap<String, Vec<f32>>) -> Result<()>
Build HNSW index from embeddings
Sourcepub fn search(&self, query: &[f32], k: usize) -> Result<Vec<SearchResult>>
pub fn search(&self, query: &[f32], k: usize) -> Result<Vec<SearchResult>>
Search for K nearest neighbors
Sourcepub fn batch_search(
&self,
queries: &[Vec<f32>],
k: usize,
) -> Result<Vec<Vec<SearchResult>>>
pub fn batch_search( &self, queries: &[Vec<f32>], k: usize, ) -> Result<Vec<Vec<SearchResult>>>
Batch search for multiple queries
Sourcepub fn add(&mut self, entity_id: &str, vector: &[f32]) -> Result<()>
pub fn add(&mut self, entity_id: &str, vector: &[f32]) -> Result<()>
Add a new vector to the index (incremental update)
Sourcepub fn set_ef_search(&mut self, ef: usize)
pub fn set_ef_search(&mut self, ef: usize)
Set ef_search parameter (trade-off between speed and recall)
Sourcepub fn remove(&mut self, entity_id: &str) -> bool
pub fn remove(&mut self, entity_id: &str) -> bool
Remove a vector from the index (lazy deletion with tombstone)
The vector is marked as deleted and will be excluded from search results.
The actual data is not removed until compact() is called.
Sourcepub fn is_deleted(&self, entity_id: &str) -> bool
pub fn is_deleted(&self, entity_id: &str) -> bool
Check if a vector is deleted
Sourcepub fn deleted_count(&self) -> usize
pub fn deleted_count(&self) -> usize
Get the number of deleted vectors (tombstones)
Sourcepub fn active_count(&self) -> usize
pub fn active_count(&self) -> usize
Get the number of active (non-deleted) vectors
Sourcepub fn set_metadata(&mut self, entity_id: &str, metadata: Metadata)
pub fn set_metadata(&mut self, entity_id: &str, metadata: Metadata)
Set metadata for an entity
Sourcepub fn set_metadata_batch(&mut self, metadata_map: HashMap<String, Metadata>)
pub fn set_metadata_batch(&mut self, metadata_map: HashMap<String, Metadata>)
Set metadata for multiple entities
Sourcepub fn get_metadata(&self, entity_id: &str) -> Option<&Metadata>
pub fn get_metadata(&self, entity_id: &str) -> Option<&Metadata>
Get metadata for an entity
Sourcepub fn filtered_search(
&self,
query: &[f32],
k: usize,
filter: &Filter,
) -> Result<Vec<SearchResult>>
pub fn filtered_search( &self, query: &[f32], k: usize, filter: &Filter, ) -> Result<Vec<SearchResult>>
Search with metadata filtering (post-filtering)
Searches with HNSW, then filters results by metadata. Efficient when most results pass the filter.
Sourcepub fn prefiltered_search(
&self,
query: &[f32],
k: usize,
filter: &Filter,
) -> Result<Vec<SearchResult>>
pub fn prefiltered_search( &self, query: &[f32], k: usize, filter: &Filter, ) -> Result<Vec<SearchResult>>
Search with pre-filtering (filter candidates during search)
More accurate when filters are very selective, but may be slower. Uses brute-force search on filtered candidates.
Sourcepub fn optimize_graph(&mut self) -> Result<()>
pub fn optimize_graph(&mut self) -> Result<()>
Optimize the HNSW graph structure
This method performs periodic maintenance on the graph to improve search quality:
- Removes references to deleted nodes from neighbor lists
- Trims neighbor lists to respect the M parameter
- Rebuilds connections for isolated nodes
Call this method periodically after many deletions or additions to maintain optimal search performance.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for HnswIndex
impl<'de> Deserialize<'de> for HnswIndex
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>,
Auto Trait Implementations§
impl Freeze for HnswIndex
impl RefUnwindSafe for HnswIndex
impl Send for HnswIndex
impl Sync for HnswIndex
impl Unpin for HnswIndex
impl UnwindSafe for HnswIndex
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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>
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>
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