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#[derive(Debug, Clone)]
40pub struct TermQueryInfo {
41 pub field: crate::dsl::Field,
43 pub term: Vec<u8>,
45}
46
47#[cfg(not(target_arch = "wasm32"))]
53pub trait Query: Send + Sync {
54 fn scorer<'a>(&self, reader: &'a SegmentReader, limit: usize) -> ScorerFuture<'a>;
62
63 fn count_estimate<'a>(&self, reader: &'a SegmentReader) -> CountFuture<'a>;
65
66 fn as_term_query_info(&self) -> Option<TermQueryInfo> {
70 None
71 }
72}
73
74#[cfg(target_arch = "wasm32")]
76pub trait Query {
77 fn scorer<'a>(&self, reader: &'a SegmentReader, limit: usize) -> ScorerFuture<'a>;
82
83 fn count_estimate<'a>(&self, reader: &'a SegmentReader) -> CountFuture<'a>;
85
86 fn as_term_query_info(&self) -> Option<TermQueryInfo> {
88 None
89 }
90}
91
92impl Query for Box<dyn Query> {
93 fn scorer<'a>(&self, reader: &'a SegmentReader, limit: usize) -> ScorerFuture<'a> {
94 (**self).scorer(reader, limit)
95 }
96
97 fn count_estimate<'a>(&self, reader: &'a SegmentReader) -> CountFuture<'a> {
98 (**self).count_estimate(reader)
99 }
100
101 fn as_term_query_info(&self) -> Option<TermQueryInfo> {
102 (**self).as_term_query_info()
103 }
104}
105
106pub type MatchedPositions = Vec<(u32, Vec<super::ScoredPosition>)>;
109
110#[cfg(not(target_arch = "wasm32"))]
112pub trait Scorer: Send {
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#[cfg(target_arch = "wasm32")]
137pub trait Scorer {
138 fn doc(&self) -> DocId;
140
141 fn score(&self) -> Score;
143
144 fn advance(&mut self) -> DocId;
146
147 fn seek(&mut self, target: DocId) -> DocId;
149
150 fn size_hint(&self) -> u32;
152
153 fn matched_positions(&self) -> Option<MatchedPositions> {
155 None
156 }
157}
158
159pub struct EmptyScorer;
161
162impl Scorer for EmptyScorer {
163 fn doc(&self) -> DocId {
164 crate::structures::TERMINATED
165 }
166
167 fn score(&self) -> Score {
168 0.0
169 }
170
171 fn advance(&mut self) -> DocId {
172 crate::structures::TERMINATED
173 }
174
175 fn seek(&mut self, _target: DocId) -> DocId {
176 crate::structures::TERMINATED
177 }
178
179 fn size_hint(&self) -> u32 {
180 0
181 }
182}