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
47pub type MatchedPositions = Vec<(u32, Vec<super::ScoredPosition>)>;
50
51macro_rules! define_query_traits {
52 ($($send_bounds:tt)*) => {
53 pub trait Query: $($send_bounds)* {
59 fn scorer<'a>(&self, reader: &'a SegmentReader, limit: usize) -> ScorerFuture<'a>;
67
68 fn count_estimate<'a>(&self, reader: &'a SegmentReader) -> CountFuture<'a>;
70
71 fn as_term_query_info(&self) -> Option<TermQueryInfo> {
75 None
76 }
77 }
78
79 pub trait Scorer: $($send_bounds)* {
81 fn doc(&self) -> DocId;
83
84 fn score(&self) -> Score;
86
87 fn advance(&mut self) -> DocId;
89
90 fn seek(&mut self, target: DocId) -> DocId;
92
93 fn size_hint(&self) -> u32;
95
96 fn matched_positions(&self) -> Option<MatchedPositions> {
99 None
100 }
101 }
102 };
103}
104
105#[cfg(not(target_arch = "wasm32"))]
106define_query_traits!(Send + Sync);
107
108#[cfg(target_arch = "wasm32")]
109define_query_traits!();
110
111impl Query for Box<dyn Query> {
112 fn scorer<'a>(&self, reader: &'a SegmentReader, limit: usize) -> ScorerFuture<'a> {
113 (**self).scorer(reader, limit)
114 }
115
116 fn count_estimate<'a>(&self, reader: &'a SegmentReader) -> CountFuture<'a> {
117 (**self).count_estimate(reader)
118 }
119
120 fn as_term_query_info(&self) -> Option<TermQueryInfo> {
121 (**self).as_term_query_info()
122 }
123}
124
125pub struct EmptyScorer;
127
128impl Scorer for EmptyScorer {
129 fn doc(&self) -> DocId {
130 crate::structures::TERMINATED
131 }
132
133 fn score(&self) -> Score {
134 0.0
135 }
136
137 fn advance(&mut self) -> DocId {
138 crate::structures::TERMINATED
139 }
140
141 fn seek(&mut self, _target: DocId) -> DocId {
142 crate::structures::TERMINATED
143 }
144
145 fn size_hint(&self) -> u32 {
146 0
147 }
148}