#[derive(
serde::Serialize,
serde::Deserialize,
zerompk::ToMessagePack,
zerompk::FromMessagePack,
Clone,
Debug,
)]
pub struct Posting {
pub doc_id: String,
pub term_freq: u32,
pub positions: Vec<u32>,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum QueryMode {
#[default]
And,
Or,
}
#[derive(Debug, Clone)]
pub struct TextSearchResult {
pub doc_id: String,
pub score: f32,
pub fuzzy: bool,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MatchOffset {
pub start: usize,
pub end: usize,
pub term: String,
}
#[derive(Debug, Clone, Copy)]
pub struct Bm25Params {
pub k1: f32,
pub b: f32,
}
impl Default for Bm25Params {
fn default() -> Self {
Self { k1: 1.2, b: 0.75 }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_query_mode_is_and() {
assert_eq!(QueryMode::default(), QueryMode::And);
}
#[test]
fn default_bm25_params() {
let p = Bm25Params::default();
assert!((p.k1 - 1.2).abs() < f32::EPSILON);
assert!((p.b - 0.75).abs() < f32::EPSILON);
}
#[test]
fn posting_fields() {
let posting = Posting {
doc_id: "doc1".into(),
term_freq: 3,
positions: vec![0, 5, 12],
};
assert_eq!(posting.doc_id, "doc1");
assert_eq!(posting.term_freq, 3);
assert_eq!(posting.positions, vec![0, 5, 12]);
}
}