Skip to main content

HnswIndex

Struct HnswIndex 

Source
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

Source

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).
Source

pub fn with_default_dims(capacity: usize) -> Result<HnswIndex, Error>

Create with default dimensions (1536).

Source

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).

Source

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).

Source

pub fn remove(&self, key: u64) -> Result<(), Error>

Remove a vector by key.

Source

pub fn contains(&self, key: u64) -> bool

Check if a key exists in the index.

Source

pub fn get(&self, key: u64) -> Option<Vec<f32>>

Get the vector stored for a key.

Source

pub fn len(&self) -> usize

Number of vectors currently in the index.

Source

pub fn is_empty(&self) -> bool

Whether the index is empty.

Source

pub fn dimensions(&self) -> usize

Vector dimensions.

Source

pub fn save(&self, path: &Path) -> Result<(), Error>

Save the index to a file.

Source

pub fn load(path: &Path) -> Result<HnswIndex, Error>

Load (restore) an index from a file.

Returns a new HnswIndex with the same dimensions as the saved index.

Source

pub fn reserve(&self, capacity: usize) -> Result<(), Error>

Reserve additional capacity.

Source

pub fn rename(&self, from: u64, to: u64) -> Result<(), Error>

Rename a key (reassign vector from old key to new key).

Trait Implementations§

Source§

impl Debug for HnswIndex

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more