pub struct HnswIndex { /* private fields */ }Expand description
HNSW approximate nearest neighbor index.
Wraps usearch::Index and provides a type-safe, ergonomic interface
for dense vector operations. The index is not thread-safe internally —
callers must synchronize access (e.g., via parking_lot::RwLock).
Implementations§
Source§impl HnswIndex
impl HnswIndex
Sourcepub fn new(dimensions: usize, capacity: usize) -> Result<HnswIndex, Error>
pub fn new(dimensions: usize, capacity: usize) -> Result<HnswIndex, Error>
Create a new HNSW index.
§Arguments
dimensions— Dimensionality of vectors (e.g., 1536 for OpenAI).capacity— Initial capacity hint (pre-allocated slots).
Sourcepub fn with_default_dims(capacity: usize) -> Result<HnswIndex, Error>
pub fn with_default_dims(capacity: usize) -> Result<HnswIndex, Error>
Create with default dimensions (1536).
Sourcepub fn add(&self, key: u64, vector: &[f32]) -> Result<(), Error>
pub fn add(&self, key: u64, vector: &[f32]) -> Result<(), Error>
Add a vector to the index with the given key.
The key is a u64 identifier. Callers should maintain a mapping from u64 key to logical ID (e.g., via SQLite).
Sourcepub fn search(&self, query: &[f32], k: usize) -> Result<Vec<(u64, f32)>, Error>
pub fn search(&self, query: &[f32], k: usize) -> Result<Vec<(u64, f32)>, Error>
Search for the k nearest neighbors of the query vector.
Returns a sorted list of (key, distance) pairs. Distance is cosine distance (0.0 = identical for normalized vectors).
Sourcepub fn dimensions(&self) -> usize
pub fn dimensions(&self) -> usize
Vector dimensions.
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for HnswIndex
impl !UnwindSafe for HnswIndex
impl Freeze for HnswIndex
impl Send for HnswIndex
impl Sync for HnswIndex
impl Unpin for HnswIndex
impl UnsafeUnpin 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
Mutably borrows from an owned value. Read more