pub mod inverted;
pub mod tokenizer;
pub use inverted::{InvertedIndex, PostingEntry, PostingList};
pub use tokenizer::{Tokenizer, TokenizerConfig};
use std::path::{Path, PathBuf};
use crate::errors::Result;
use crate::types::VectorId;
pub struct SparseIndex {
pub index: InvertedIndex,
path: PathBuf,
}
impl SparseIndex {
pub fn create(path: &Path) -> Self {
Self {
index: InvertedIndex::default(),
path: path.to_path_buf(),
}
}
pub fn open(path: &Path) -> Result<Self> {
let index = if path.exists() {
InvertedIndex::load(path)?
} else {
InvertedIndex::default()
};
Ok(Self {
index,
path: path.to_path_buf(),
})
}
pub fn index_document(&mut self, id: &VectorId, text: &str) -> Result<()> {
self.index.index_document(id, text)
}
pub fn remove_document(&mut self, id: &VectorId) -> Result<()> {
self.index.remove_document(id)
}
pub fn search(&self, query: &str, k: usize) -> Result<Vec<(VectorId, f32)>> {
self.index.search(query, k)
}
pub fn save(&self) -> Result<()> {
self.index.save(&self.path)
}
pub fn score(
&self,
query_text: &str,
candidate_ids: &[VectorId],
) -> Result<Vec<(VectorId, f32)>> {
self.index.score_candidates(query_text, candidate_ids)
}
pub fn score_all(&self, query_text: &str, k: usize) -> Result<Vec<(VectorId, f32)>> {
self.index.search(query_text, k)
}
pub fn doc_count(&self) -> usize {
self.index.doc_count()
}
}