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<u32>)>;
108
109#[cfg(not(target_arch = "wasm32"))]
111pub trait Scorer: Send {
112 fn doc(&self) -> DocId;
114
115 fn score(&self) -> Score;
117
118 fn advance(&mut self) -> DocId;
120
121 fn seek(&mut self, target: DocId) -> DocId;
123
124 fn size_hint(&self) -> u32;
126
127 fn matched_positions(&self) -> Option<MatchedPositions> {
130 None
131 }
132}
133
134#[cfg(target_arch = "wasm32")]
136pub trait Scorer {
137 fn doc(&self) -> DocId;
139
140 fn score(&self) -> Score;
142
143 fn advance(&mut self) -> DocId;
145
146 fn seek(&mut self, target: DocId) -> DocId;
148
149 fn size_hint(&self) -> u32;
151
152 fn matched_positions(&self) -> Option<MatchedPositions> {
154 None
155 }
156}
157
158pub struct EmptyScorer;
160
161impl Scorer for EmptyScorer {
162 fn doc(&self) -> DocId {
163 crate::structures::TERMINATED
164 }
165
166 fn score(&self) -> Score {
167 0.0
168 }
169
170 fn advance(&mut self) -> DocId {
171 crate::structures::TERMINATED
172 }
173
174 fn seek(&mut self, _target: DocId) -> DocId {
175 crate::structures::TERMINATED
176 }
177
178 fn size_hint(&self) -> u32 {
179 0
180 }
181}