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
impl LshIndex
Sourcepub fn new(
dim: usize,
num_hyperplanes: usize,
num_tables: usize,
seed: u64,
) -> Self
pub fn new( dim: usize, num_hyperplanes: usize, num_tables: usize, seed: u64, ) -> Self
Create a new index.
dim— dimensionality of your embedding vectorsnum_hyperplanes— bits per hash (12–16 is a good starting range)num_tables— number of independent hash tables (4–8 typical)seed— PRNG seed; useic_cdk::api::time()on ICP
Sourcepub fn insert(&mut self, id: String, embedding: &[f64])
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.
Sourcepub fn query(&self, embedding: &[f64]) -> Vec<String>
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.
pub fn is_empty(&self) -> bool
pub fn clear(&mut self)
Sourcepub fn search(
&self,
query: &Embedding,
store: &HashMap<String, Embedding>,
metric: Option<DistanceMetric>,
) -> Vec<(String, f64)>
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.