hermes_core/query/
traits.rs1use std::future::Future;
6use std::pin::Pin;
7
8use crate::segment::SegmentReader;
9use crate::{DocId, Result, Score};
10
11#[derive(Debug, Clone, Copy)]
13pub struct Bm25Params {
14 pub k1: f32,
16 pub b: f32,
18}
19
20impl Default for Bm25Params {
21 fn default() -> Self {
22 Self { k1: 1.2, b: 0.75 }
23 }
24}
25
26#[cfg(not(target_arch = "wasm32"))]
28pub type ScorerFuture<'a> = Pin<Box<dyn Future<Output = Result<Box<dyn Scorer + 'a>>> + Send + 'a>>;
29#[cfg(target_arch = "wasm32")]
30pub type ScorerFuture<'a> = Pin<Box<dyn Future<Output = Result<Box<dyn Scorer + 'a>>> + 'a>>;
31
32#[cfg(not(target_arch = "wasm32"))]
34pub type CountFuture<'a> = Pin<Box<dyn Future<Output = Result<u32>> + Send + 'a>>;
35#[cfg(target_arch = "wasm32")]
36pub type CountFuture<'a> = Pin<Box<dyn Future<Output = Result<u32>> + 'a>>;
37
38#[cfg(not(target_arch = "wasm32"))]
40pub trait Query: Send + Sync {
41 fn scorer<'a>(&'a self, reader: &'a SegmentReader) -> ScorerFuture<'a>;
43
44 fn count_estimate<'a>(&'a self, reader: &'a SegmentReader) -> CountFuture<'a>;
46}
47
48#[cfg(target_arch = "wasm32")]
50pub trait Query {
51 fn scorer<'a>(&'a self, reader: &'a SegmentReader) -> ScorerFuture<'a>;
53
54 fn count_estimate<'a>(&'a self, reader: &'a SegmentReader) -> CountFuture<'a>;
56}
57
58impl Query for Box<dyn Query> {
59 fn scorer<'a>(&'a self, reader: &'a SegmentReader) -> ScorerFuture<'a> {
60 (**self).scorer(reader)
61 }
62
63 fn count_estimate<'a>(&'a self, reader: &'a SegmentReader) -> CountFuture<'a> {
64 (**self).count_estimate(reader)
65 }
66}
67
68#[cfg(not(target_arch = "wasm32"))]
70pub trait Scorer: Send {
71 fn doc(&self) -> DocId;
73
74 fn score(&self) -> Score;
76
77 fn advance(&mut self) -> DocId;
79
80 fn seek(&mut self, target: DocId) -> DocId;
82
83 fn size_hint(&self) -> u32;
85}
86
87#[cfg(target_arch = "wasm32")]
89pub trait Scorer {
90 fn doc(&self) -> DocId;
92
93 fn score(&self) -> Score;
95
96 fn advance(&mut self) -> DocId;
98
99 fn seek(&mut self, target: DocId) -> DocId;
101
102 fn size_hint(&self) -> u32;
104}
105
106pub struct EmptyScorer;
108
109impl Scorer for EmptyScorer {
110 fn doc(&self) -> DocId {
111 crate::structures::TERMINATED
112 }
113
114 fn score(&self) -> Score {
115 0.0
116 }
117
118 fn advance(&mut self) -> DocId {
119 crate::structures::TERMINATED
120 }
121
122 fn seek(&mut self, _target: DocId) -> DocId {
123 crate::structures::TERMINATED
124 }
125
126 fn size_hint(&self) -> u32 {
127 0
128 }
129}