use crate::db::record::Key;
use crate::file::QueryAlgorithm;
pub use crate::format::catalog::TfMode;
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub enum TextScorer {
Bm25 {
k1: f32,
b: f32,
},
TfidfCosine {
tf_mode: TfMode,
},
CountCosine,
TfidfCosineApprox {
tf_mode: TfMode,
},
CountCosineApprox,
Dice,
Overlap,
Containment,
}
impl TextScorer {
#[must_use]
pub fn bm25() -> Self {
TextScorer::Bm25 { k1: 1.2, b: 0.75 }
}
#[must_use]
pub fn bm25_with(k1: f32, b: f32) -> Self {
TextScorer::Bm25 { k1, b }
}
#[must_use]
pub fn tfidf_cosine(tf_mode: TfMode) -> Self {
TextScorer::TfidfCosine { tf_mode }
}
#[must_use]
pub fn tfidf_cosine_approx(tf_mode: TfMode) -> Self {
TextScorer::TfidfCosineApprox { tf_mode }
}
#[must_use]
pub fn count_cosine() -> Self {
TextScorer::CountCosine
}
#[must_use]
pub fn count_cosine_approx() -> Self {
TextScorer::CountCosineApprox
}
#[must_use]
pub fn dice() -> Self {
TextScorer::Dice
}
#[must_use]
pub fn overlap() -> Self {
TextScorer::Overlap
}
#[must_use]
pub fn containment() -> Self {
TextScorer::Containment
}
pub(crate) fn to_algorithm(self) -> QueryAlgorithm {
match self {
TextScorer::Bm25 { k1, b } => QueryAlgorithm::Bm25 { k1, b },
TextScorer::TfidfCosine { tf_mode } => QueryAlgorithm::TfidfCosine { tf_mode },
TextScorer::CountCosine => QueryAlgorithm::CountCosine,
TextScorer::TfidfCosineApprox { tf_mode } => {
QueryAlgorithm::TfidfCosineApprox { tf_mode }
}
TextScorer::CountCosineApprox => QueryAlgorithm::CountCosineApprox,
TextScorer::Dice => QueryAlgorithm::Dice,
TextScorer::Overlap => QueryAlgorithm::Overlap,
TextScorer::Containment => QueryAlgorithm::Containment,
}
}
}
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub enum Fusion {
Rrf { k: u32 },
Weighted { text: f32, vector: f32 },
}
impl Fusion {
#[must_use]
pub fn rrf(k: u32) -> Self {
Fusion::Rrf { k }
}
#[must_use]
pub fn weighted(text: f32, vector: f32) -> Self {
Fusion::Weighted { text, vector }
}
}
impl Default for Fusion {
fn default() -> Self {
Fusion::Rrf { k: 60 }
}
}
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub enum Rerank {
Fast,
Accurate,
}
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub enum Recency {
Range { from: i64, to: i64 },
HalfLife { days: f64 },
RrfChannel { half_life_days: f64, weight: f32 },
}
impl Recency {
#[must_use]
pub fn range(from: i64, to: i64) -> Self {
Recency::Range { from, to }
}
#[must_use]
pub fn half_life(days: f64) -> Self {
Recency::HalfLife { days }
}
#[must_use]
pub fn rrf_channel(half_life_days: f64, weight: f32) -> Self {
Recency::RrfChannel {
half_life_days,
weight,
}
}
}
#[derive(Clone, Debug)]
pub struct Hit {
pub key: Key,
pub score: f32,
pub collection: String,
}
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct SearchResult {
pub hits: Vec<Hit>,
}
impl SearchResult {
pub(crate) fn new(hits: Vec<Hit>) -> Self {
Self { hits }
}
#[must_use]
pub fn into_hits(self) -> Vec<Hit> {
self.hits
}
}
impl std::ops::Deref for SearchResult {
type Target = [Hit];
fn deref(&self) -> &[Hit] {
&self.hits
}
}
impl IntoIterator for SearchResult {
type Item = Hit;
type IntoIter = std::vec::IntoIter<Hit>;
fn into_iter(self) -> Self::IntoIter {
self.hits.into_iter()
}
}
impl<'a> IntoIterator for &'a SearchResult {
type Item = &'a Hit;
type IntoIter = std::slice::Iter<'a, Hit>;
fn into_iter(self) -> Self::IntoIter {
self.hits.iter()
}
}
pub(crate) struct TextChannelSpec {
pub field: String,
pub query: String,
pub scorer: TextScorer,
}
pub(crate) struct VectorChannelSpec {
pub field: String,
pub query: Vec<f32>,
pub rerank: Rerank,
}
#[derive(Default)]
pub struct Search {
pub(crate) text: Option<TextChannelSpec>,
pub(crate) vectors: Vec<VectorChannelSpec>,
pub(crate) fusion: Fusion,
pub(crate) recency: Option<Recency>,
pub(crate) top_k: usize,
pub(crate) now_override: Option<i64>,
}
impl Search {
#[must_use]
pub fn new() -> Self {
Self {
text: None,
vectors: Vec::new(),
fusion: Fusion::default(),
recency: None,
top_k: 10,
now_override: None,
}
}
#[must_use]
pub fn text(self, field: &str, query: &str) -> Self {
self.text_with(field, query, TextScorer::bm25())
}
#[must_use]
pub fn text_with(mut self, field: &str, query: &str, scorer: TextScorer) -> Self {
self.text = Some(TextChannelSpec {
field: field.to_owned(),
query: query.to_owned(),
scorer,
});
self
}
#[must_use]
pub fn vector(self, field: &str, query: &[f32]) -> Self {
self.vector_with(field, query, Rerank::Accurate)
}
#[must_use]
pub fn vector_with(mut self, field: &str, query: &[f32], rerank: Rerank) -> Self {
self.vectors.push(VectorChannelSpec {
field: field.to_owned(),
query: query.to_vec(),
rerank,
});
self
}
#[must_use]
pub fn fuse(mut self, fusion: Fusion) -> Self {
self.fusion = fusion;
self
}
#[must_use]
pub fn recency(mut self, recency: Recency) -> Self {
self.recency = Some(recency);
self
}
#[must_use]
pub fn top_k(mut self, k: usize) -> Self {
self.top_k = k;
self
}
#[must_use]
pub fn now(mut self, unix_secs: i64) -> Self {
self.now_override = Some(unix_secs);
self
}
}