Skip to main content

SparseInvertedIndex

Struct SparseInvertedIndex 

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

An in-memory inverted index over sparse vectors (ADR-0045).

Maps each sparse dimension to the documents that have a nonzero weight there, so search accumulates a dot-product score over only the query’s nonzero dimensions. Built and maintained by quiver-embed; never persisted.

Implementations§

Source§

impl SparseInvertedIndex

Source

pub fn new() -> SparseInvertedIndex

An empty index.

Source

pub fn len(&self) -> usize

Number of live documents.

Source

pub fn is_empty(&self) -> bool

Whether the index holds no documents.

Source

pub fn contains(&self, ext_id: &str) -> bool

Whether ext_id is currently indexed.

Source

pub fn upsert(&mut self, ext_id: &str, sv: &SparseVector)

Insert or replace ext_id’s sparse vector. Re-upserting an existing id first removes its prior postings, so a dimension it no longer carries does not linger and a changed weight is not double-counted. Duplicate input dimensions are de-duplicated (last weight wins).

Source

pub fn remove(&mut self, ext_id: &str) -> bool

Remove ext_id and free its slot. Returns whether it was present.

Source

pub fn search(&self, query: &SparseVector) -> Vec<(String, f32)>

Score every document that shares a nonzero dimension with query by sparse dot product, and return (ext_id, score) for those with a positive score, sorted by score descending then id ascending (a deterministic, total order). The caller re-checks any payload filter on the ranked ids and truncates to its depth, so low-scored rows never load a payload. Duplicate query dimensions are de-duplicated (last weight wins).

Score documents against query_terms (term ids) with Okapi BM25 (ADR-0046), treating each document’s stored weights as term frequencies and using the index’s own corpus statistics — document frequency (posting-list length), document count, and average document length. k1/b are the usual BM25 parameters (BM25_K1, BM25_B). Duplicate query terms count once. Returns (ext_id, score) for documents with a positive score, sorted by score descending then id ascending. Uses the Lucene-style smoothed IDF ln(1 + (N − df + 0.5)/(df + 0.5)), which is always non-negative, so even a term in most of the corpus contributes a small positive amount (no negative scores to clamp).

Trait Implementations§

Source§

impl Debug for SparseInvertedIndex

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for SparseInvertedIndex

Source§

fn default() -> SparseInvertedIndex

Returns the “default value” for a type. 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<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 = 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.