pub struct FibSidecarIndex<Id>{ /* private fields */ }Expand description
Generic ID-typed sidecar search index.
Wraps a FibScorer and stores a list of (Id, FibCodeV1) entries.
Search uses the Gram-table estimator for each stored code and returns
candidates sorted by descending approximate inner product.
The index is cheap to clone (via Clone on the scorer and entries) but
in typical usage a single instance is shared by reference across queries.
§Generics
Id must be Clone + Eq + Debug. Common choices are u64, String,
or a newtype key. The index does not enforce uniqueness — adding the
same ID twice will produce two entries.
Implementations§
Source§impl<Id> FibSidecarIndex<Id>
impl<Id> FibSidecarIndex<Id>
Sourcepub fn new(scorer: FibScorer) -> Self
pub fn new(scorer: FibScorer) -> Self
Construct a new sidecar index from a FibScorer.
The scorer’s profile is cloned for validation of incoming codes. The index starts empty.
Sourcepub fn profile(&self) -> &FibQuantProfileV1
pub fn profile(&self) -> &FibQuantProfileV1
Return a reference to the profile used for validation.
Sourcepub fn add(&mut self, id: Id, code: FibCodeV1)
pub fn add(&mut self, id: Id, code: FibCodeV1)
Add a single encoded vector with a caller-owned ID.
The code’s ambient_dim and block_dim are checked against the
scorer’s profile to catch mismatches early.
Sourcepub fn add_batch(&mut self, entries: Vec<(Id, FibCodeV1)>)
pub fn add_batch(&mut self, entries: Vec<(Id, FibCodeV1)>)
Add many encoded vectors at once.
Equivalent to calling add in a loop but avoids
repeated capacity growth.
Sourcepub fn entries(&self) -> &[(Id, FibCodeV1)]
pub fn entries(&self) -> &[(Id, FibCodeV1)]
Return a slice of all (Id, FibCodeV1) entries.
Used by persistence to serialize the index
to disk. The slice borrows from the index.
Sourcepub fn search(
&self,
query: &[f32],
top_k: usize,
oversample: usize,
) -> Result<Vec<ScoredCandidate<Id>>>
pub fn search( &self, query: &[f32], top_k: usize, oversample: usize, ) -> Result<Vec<ScoredCandidate<Id>>>
Approximate search returning sorted candidates.
Scores every stored code against the query, sorts by descending
approximate inner product, and returns the top top_k * oversample
candidates (or fewer if the index has fewer entries).
The returned candidates have rank assigned starting from 0.
Callers must rerank these candidates with an exact inner product
against the original (un-encoded) vectors before acting on results.
Sourcepub fn search_with_receipt(
&self,
query: &[f32],
top_k: usize,
oversample: usize,
) -> Result<(Vec<ScoredCandidate<Id>>, SearchReceiptV1)>
pub fn search_with_receipt( &self, query: &[f32], top_k: usize, oversample: usize, ) -> Result<(Vec<ScoredCandidate<Id>>, SearchReceiptV1)>
Approximate search returning both candidates and a SearchReceiptV1.
See search for search semantics. The receipt
documents the operational parameters and timing of the search.
Sourcepub fn ivf(&self) -> Option<&IvfCoarseQuantizer>
pub fn ivf(&self) -> Option<&IvfCoarseQuantizer>
Return a reference to the IVF coarse quantizer, if built.
Sourcepub fn ivf_is_built(&self) -> bool
pub fn ivf_is_built(&self) -> bool
Whether the IVF coarse quantizer has been built.
Sourcepub fn build_ivf(&mut self, num_centroids: usize) -> Result<()>
pub fn build_ivf(&mut self, num_centroids: usize) -> Result<()>
Build the IVF coarse quantizer by running k-means on decoded vectors.
Decodes all stored FibCodeV1 entries to their approximate vectors
using the scorer’s quantizer, runs k-means with num_centroids
centroids (default: sqrt(N)), and stores the centroids and
assignments. After this call, search_ivf
will use the IVF path instead of falling back to linear scan.
§Arguments
num_centroids— number of k-means centroids (k). Pass(self.len() as f64).sqrt() as usizeor letbuild_ivf_defaultcompute it.
Sourcepub fn build_ivf_default(&mut self) -> Result<()>
pub fn build_ivf_default(&mut self) -> Result<()>
Build IVF with default k = sqrt(N) centroids.
Sourcepub fn search_ivf(
&self,
query: &[f32],
top_k: usize,
oversample: usize,
nprobe: usize,
) -> Result<Vec<ScoredCandidate<Id>>>
pub fn search_ivf( &self, query: &[f32], top_k: usize, oversample: usize, nprobe: usize, ) -> Result<Vec<ScoredCandidate<Id>>>
IVF-accelerated search.
Finds the nprobe nearest centroids to the query, gathers all
entries assigned to those centroids, scores them using the prepared
query (Gram table), and returns the top top_k * oversample
candidates sorted by descending approximate inner product.
If IVF is not built (centroids empty or ivf is None), falls
back to a full linear scan via search.
Sourcepub fn search_with_receipt_ivf(
&self,
query: &[f32],
top_k: usize,
oversample: usize,
nprobe: usize,
) -> Result<(Vec<ScoredCandidate<Id>>, SearchReceiptIvfV1)>
pub fn search_with_receipt_ivf( &self, query: &[f32], top_k: usize, oversample: usize, nprobe: usize, ) -> Result<(Vec<ScoredCandidate<Id>>, SearchReceiptIvfV1)>
IVF-accelerated search with receipt.
Like search_ivf but returns a
SearchReceiptIvfV1 documenting the IVF parameters used.
Auto Trait Implementations§
impl<Id> Freeze for FibSidecarIndex<Id>
impl<Id> RefUnwindSafe for FibSidecarIndex<Id>where
Id: RefUnwindSafe,
impl<Id> Send for FibSidecarIndex<Id>where
Id: Send,
impl<Id> Sync for FibSidecarIndex<Id>where
Id: Sync,
impl<Id> Unpin for FibSidecarIndex<Id>where
Id: Unpin,
impl<Id> UnsafeUnpin for FibSidecarIndex<Id>
impl<Id> UnwindSafe for FibSidecarIndex<Id>where
Id: UnwindSafe,
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
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>
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>
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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.