Skip to main content

LshIndex

Struct LshIndex 

Source
pub struct LshIndex { /* private fields */ }
Expand description

Approximate nearest-neighbour index backed by LSH.

Insert embeddings during indexing, query during search. The returned candidate IDs should then be exact-scored with cosine similarity.

Implementations§

Source§

impl LshIndex

Source

pub fn new( dim: usize, num_hyperplanes: usize, num_tables: usize, seed: u64, ) -> Self

Create a new index.

  • dim — dimensionality of your embedding vectors
  • num_hyperplanes — bits per hash (12–16 is a good starting range)
  • num_tables — number of independent hash tables (4–8 typical)
  • seed — PRNG seed; use ic_cdk::api::time() on ICP
Source

pub fn insert(&mut self, id: String, embedding: &[f64])

Index an embedding under id.

Call once per embedding at insert time. If a document produces multiple embeddings (title + description + keywords), insert each separately with the same id — the candidate set deduplicates by id anyway.

Source

pub fn query(&self, embedding: &[f64]) -> Vec<String>

Return candidate IDs whose hash matches the query in at least one table.

This is the fast path. The caller is responsible for exact-scoring the candidates with cosine similarity and taking the top-N.

Source

pub fn len(&self) -> usize

Number of distinct IDs in the index.

Source

pub fn is_empty(&self) -> bool

Source

pub fn clear(&mut self)

Source

pub fn search( &self, query: &Embedding, store: &HashMap<String, Embedding>, metric: Option<DistanceMetric>, ) -> Vec<(String, f64)>

Score and rank candidates for query against store, returning (id, score) pairs.

The LSH bucket lookup narrows the field; metric exact-scores the survivors. Pass None to use the default DistanceMetric::Cosine { normalized: false }.

Results are sorted best-first:

  • similarity metrics (Cosine, DotProduct) → descending
  • distance metrics (Euclidean, Manhattan, Chebyshev, Angular) → ascending

IDs present in the candidate set but absent from store are silently skipped.

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<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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<T> WasmCompatSend for T
where T: Send,

Source§

impl<T> WasmCompatSync for T
where T: Sync,