pub struct InvertedIndex {
pub doc_lengths: HashMap<VectorId, usize>,
pub total_docs: usize,
pub total_tokens: usize,
/* private fields */
}Expand description
BM25-scored inverted index.
Scoring formula:
IDF(t) = ln((N − df + 0.5) / (df + 0.5) + 1)
score(q, d) = Σ IDF(tᵢ) × tf × (k1 + 1) / (tf + k1 × (1 − b + b × |d| / avgdl))Fields§
§doc_lengths: HashMap<VectorId, usize>§total_docs: usizeTotal number of indexed documents.
Renamed from doc_count in P23; the serde alias preserves backward-compatible
deserialization of existing JSON files that still use "doc_count".
total_tokens: usizeRunning sum of all document lengths; enables O(1) avg_doc_length recomputation.
Defaults to 0 for backward compat; load() migrates old files that lack this field.
Implementations§
Source§impl InvertedIndex
impl InvertedIndex
pub fn new(k1: f32, b: f32) -> Self
Sourcepub fn index_document(&mut self, id: &VectorId, text: &str) -> Result<()>
pub fn index_document(&mut self, id: &VectorId, text: &str) -> Result<()>
Add or update a document. If the document already exists it is first removed.
Sourcepub fn remove_document(&mut self, id: &VectorId) -> Result<()>
pub fn remove_document(&mut self, id: &VectorId) -> Result<()>
Remove a document from the index.
Sourcepub fn score(
&self,
query_terms: &[String],
candidate_ids: &[VectorId],
) -> Result<Vec<(VectorId, f32)>>
pub fn score( &self, query_terms: &[String], candidate_ids: &[VectorId], ) -> Result<Vec<(VectorId, f32)>>
Score a pre-tokenized list of query terms against a set of candidate documents.
If candidate_ids is empty all documents in the posting lists are scored.
Results are sorted descending by BM25 score.
Sourcepub fn search(&self, query: &str, k: usize) -> Result<Vec<(VectorId, f32)>>
pub fn search(&self, query: &str, k: usize) -> Result<Vec<(VectorId, f32)>>
Tokenize query then score all matching documents. Returns top k.
Sourcepub fn score_candidates(
&self,
query_text: &str,
candidate_ids: &[VectorId],
) -> Result<Vec<(VectorId, f32)>>
pub fn score_candidates( &self, query_text: &str, candidate_ids: &[VectorId], ) -> Result<Vec<(VectorId, f32)>>
Tokenize query_text and score against candidate_ids.
If candidate_ids is empty, scores all matching documents.
Results sorted descending by BM25 score.
Sourcepub fn load(path: &Path) -> Result<Self>
pub fn load(path: &Path) -> Result<Self>
Load from a JSON file written by save. Reconstructs the tokenizer.
Migration: if total_tokens is 0 (absent in old files) but doc_lengths is
non-empty, the running sum is recomputed once via O(n) scan so that subsequent
calls to recompute_avg_doc_length are O(1).
pub fn doc_count(&self) -> usize
pub fn avg_doc_length(&self) -> f32
Trait Implementations§
Source§impl Clone for InvertedIndex
impl Clone for InvertedIndex
Source§impl Debug for InvertedIndex
impl Debug for InvertedIndex
Source§impl Default for InvertedIndex
impl Default for InvertedIndex
Source§impl<'de> Deserialize<'de> for InvertedIndex
impl<'de> Deserialize<'de> for InvertedIndex
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Auto Trait Implementations§
impl Freeze for InvertedIndex
impl RefUnwindSafe for InvertedIndex
impl Send for InvertedIndex
impl Sync for InvertedIndex
impl Unpin for InvertedIndex
impl UnsafeUnpin for InvertedIndex
impl UnwindSafe for InvertedIndex
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
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> ⓘ
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 more