hermes_core/query/
traits.rs1use std::future::Future;
6use std::pin::Pin;
7
8use crate::segment::SegmentReader;
9use crate::{DocId, Result, Score};
10
11#[cfg(not(target_arch = "wasm32"))]
17pub type DocPredicate<'a> = Box<dyn Fn(DocId) -> bool + Send + Sync + 'a>;
18#[cfg(target_arch = "wasm32")]
19pub type DocPredicate<'a> = Box<dyn Fn(DocId) -> bool + 'a>;
20
21#[derive(Debug, Clone, Copy)]
23pub struct Bm25Params {
24 pub k1: f32,
26 pub b: f32,
28}
29
30impl Default for Bm25Params {
31 fn default() -> Self {
32 Self { k1: 1.2, b: 0.75 }
33 }
34}
35
36#[cfg(not(target_arch = "wasm32"))]
38pub type ScorerFuture<'a> = Pin<Box<dyn Future<Output = Result<Box<dyn Scorer + 'a>>> + Send + 'a>>;
39#[cfg(target_arch = "wasm32")]
40pub type ScorerFuture<'a> = Pin<Box<dyn Future<Output = Result<Box<dyn Scorer + 'a>>> + 'a>>;
41
42#[cfg(not(target_arch = "wasm32"))]
44pub type CountFuture<'a> = Pin<Box<dyn Future<Output = Result<u32>> + Send + 'a>>;
45#[cfg(target_arch = "wasm32")]
46pub type CountFuture<'a> = Pin<Box<dyn Future<Output = Result<u32>> + 'a>>;
47
48#[derive(Debug, Clone)]
50pub struct TermQueryInfo {
51 pub field: crate::dsl::Field,
53 pub term: Vec<u8>,
55}
56
57pub type MatchedPositions = Vec<(u32, Vec<super::ScoredPosition>)>;
60
61macro_rules! define_query_traits {
62 ($($send_bounds:tt)*) => {
63 pub trait Query: $($send_bounds)* {
69 fn scorer<'a>(
77 &self,
78 reader: &'a SegmentReader,
79 limit: usize,
80 predicate: Option<DocPredicate<'a>>,
81 ) -> ScorerFuture<'a>;
82
83 fn count_estimate<'a>(&self, reader: &'a SegmentReader) -> CountFuture<'a>;
85
86 #[cfg(feature = "sync")]
91 fn scorer_sync<'a>(
92 &self,
93 reader: &'a SegmentReader,
94 limit: usize,
95 predicate: Option<DocPredicate<'a>>,
96 ) -> Result<Box<dyn Scorer + 'a>> {
97 let _ = (reader, limit, predicate);
98 Err(crate::error::Error::Query(
99 "sync scorer not supported for this query type".into(),
100 ))
101 }
102
103 fn as_term_query_info(&self) -> Option<TermQueryInfo> {
107 None
108 }
109 }
110
111 pub trait Scorer: $($send_bounds)* {
113 fn doc(&self) -> DocId;
115
116 fn score(&self) -> Score;
118
119 fn advance(&mut self) -> DocId;
121
122 fn seek(&mut self, target: DocId) -> DocId;
124
125 fn size_hint(&self) -> u32;
127
128 fn matched_positions(&self) -> Option<MatchedPositions> {
131 None
132 }
133 }
134 };
135}
136
137#[cfg(not(target_arch = "wasm32"))]
138define_query_traits!(Send + Sync);
139
140#[cfg(target_arch = "wasm32")]
141define_query_traits!();
142
143impl Query for Box<dyn Query> {
144 fn scorer<'a>(
145 &self,
146 reader: &'a SegmentReader,
147 limit: usize,
148 predicate: Option<DocPredicate<'a>>,
149 ) -> ScorerFuture<'a> {
150 (**self).scorer(reader, limit, predicate)
151 }
152
153 fn count_estimate<'a>(&self, reader: &'a SegmentReader) -> CountFuture<'a> {
154 (**self).count_estimate(reader)
155 }
156
157 fn as_term_query_info(&self) -> Option<TermQueryInfo> {
158 (**self).as_term_query_info()
159 }
160
161 #[cfg(feature = "sync")]
162 fn scorer_sync<'a>(
163 &self,
164 reader: &'a SegmentReader,
165 limit: usize,
166 predicate: Option<DocPredicate<'a>>,
167 ) -> Result<Box<dyn Scorer + 'a>> {
168 (**self).scorer_sync(reader, limit, predicate)
169 }
170}
171
172pub struct EmptyScorer;
174
175impl Scorer for EmptyScorer {
176 fn doc(&self) -> DocId {
177 crate::structures::TERMINATED
178 }
179
180 fn score(&self) -> Score {
181 0.0
182 }
183
184 fn advance(&mut self) -> DocId {
185 crate::structures::TERMINATED
186 }
187
188 fn seek(&mut self, _target: DocId) -> DocId {
189 crate::structures::TERMINATED
190 }
191
192 fn size_hint(&self) -> u32 {
193 0
194 }
195}