pub struct BinaryBlocker {
pub threshold: u32,
/* private fields */
}Expand description
Blocker using binary embeddings for fast candidate filtering.
§Usage Pattern
- Pre-compute binary hashes for all entities in your KB
- At query time, hash the query embedding
- Find candidates within Hamming distance threshold
- Run dense similarity only on candidates
§Example
use anno::backends::inference::{BinaryBlocker, BinaryHash};
let mut blocker = BinaryBlocker::new(8); // 8-bit Hamming threshold
// Add entities to the index
let hash1 = BinaryHash::from_embedding(&vec![0.1; 768]);
let hash2 = BinaryHash::from_embedding(&vec![-0.1; 768]);
blocker.add(0, hash1);
blocker.add(1, hash2);
// Query
let query = BinaryHash::from_embedding(&vec![0.1; 768]);
let candidates = blocker.query(&query);
assert!(candidates.contains(&0)); // Similar to hash1Fields§
§threshold: u32Hamming distance threshold for candidates
Implementations§
Source§impl BinaryBlocker
impl BinaryBlocker
Sourcepub fn add(&mut self, id: usize, hash: BinaryHash)
pub fn add(&mut self, id: usize, hash: BinaryHash)
Add an entity to the index.
Sourcepub fn add_batch(
&mut self,
entries: impl IntoIterator<Item = (usize, BinaryHash)>,
)
pub fn add_batch( &mut self, entries: impl IntoIterator<Item = (usize, BinaryHash)>, )
Add multiple entities.
Sourcepub fn query(&self, query: &BinaryHash) -> Vec<usize>
pub fn query(&self, query: &BinaryHash) -> Vec<usize>
Find candidate IDs within Hamming distance threshold.
Sourcepub fn query_with_distance(&self, query: &BinaryHash) -> Vec<(usize, u32)>
pub fn query_with_distance(&self, query: &BinaryHash) -> Vec<(usize, u32)>
Find candidates with their distances.
Trait Implementations§
Source§impl Clone for BinaryBlocker
impl Clone for BinaryBlocker
Source§fn clone(&self) -> BinaryBlocker
fn clone(&self) -> BinaryBlocker
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for BinaryBlocker
impl RefUnwindSafe for BinaryBlocker
impl Send for BinaryBlocker
impl Sync for BinaryBlocker
impl Unpin for BinaryBlocker
impl UnsafeUnpin for BinaryBlocker
impl UnwindSafe for BinaryBlocker
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
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>
Converts
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>
Converts
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