use std::future::Future;
use std::pin::Pin;
use crate::segment::SegmentReader;
use crate::{DocId, Result, Score};
#[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(not(target_arch = "wasm32"))]
pub type ScorerFuture<'a> = Pin<Box<dyn Future<Output = Result<Box<dyn Scorer + 'a>>> + Send + 'a>>;
#[cfg(target_arch = "wasm32")]
pub type ScorerFuture<'a> = Pin<Box<dyn Future<Output = Result<Box<dyn Scorer + 'a>>> + 'a>>;
#[cfg(not(target_arch = "wasm32"))]
pub type CountFuture<'a> = Pin<Box<dyn Future<Output = Result<u32>> + Send + 'a>>;
#[cfg(target_arch = "wasm32")]
pub type CountFuture<'a> = Pin<Box<dyn Future<Output = Result<u32>> + 'a>>;
#[derive(Debug, Clone)]
pub struct TermQueryInfo {
pub field: crate::dsl::Field,
pub term: Vec<u8>,
}
#[cfg(not(target_arch = "wasm32"))]
pub trait Query: Send + Sync {
fn scorer<'a>(&self, reader: &'a SegmentReader, limit: usize) -> ScorerFuture<'a>;
fn count_estimate<'a>(&self, reader: &'a SegmentReader) -> CountFuture<'a>;
fn as_term_query_info(&self) -> Option<TermQueryInfo> {
None
}
}
#[cfg(target_arch = "wasm32")]
pub trait Query {
fn scorer<'a>(&self, reader: &'a SegmentReader, limit: usize) -> ScorerFuture<'a>;
fn count_estimate<'a>(&self, reader: &'a SegmentReader) -> CountFuture<'a>;
fn as_term_query_info(&self) -> Option<TermQueryInfo> {
None
}
}
impl Query for Box<dyn Query> {
fn scorer<'a>(&self, reader: &'a SegmentReader, limit: usize) -> ScorerFuture<'a> {
(**self).scorer(reader, limit)
}
fn count_estimate<'a>(&self, reader: &'a SegmentReader) -> CountFuture<'a> {
(**self).count_estimate(reader)
}
fn as_term_query_info(&self) -> Option<TermQueryInfo> {
(**self).as_term_query_info()
}
}
pub type MatchedPositions = Vec<(u32, Vec<u32>)>;
#[cfg(not(target_arch = "wasm32"))]
pub trait Scorer: Send {
fn doc(&self) -> DocId;
fn score(&self) -> Score;
fn advance(&mut self) -> DocId;
fn seek(&mut self, target: DocId) -> DocId;
fn size_hint(&self) -> u32;
fn matched_positions(&self) -> Option<MatchedPositions> {
None
}
}
#[cfg(target_arch = "wasm32")]
pub trait Scorer {
fn doc(&self) -> DocId;
fn score(&self) -> Score;
fn advance(&mut self) -> DocId;
fn seek(&mut self, target: DocId) -> DocId;
fn size_hint(&self) -> u32;
fn matched_positions(&self) -> Option<MatchedPositions> {
None
}
}
pub struct EmptyScorer;
impl Scorer for EmptyScorer {
fn doc(&self) -> DocId {
crate::structures::TERMINATED
}
fn score(&self) -> Score {
0.0
}
fn advance(&mut self) -> DocId {
crate::structures::TERMINATED
}
fn seek(&mut self, _target: DocId) -> DocId {
crate::structures::TERMINATED
}
fn size_hint(&self) -> u32 {
0
}
}