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"))]
49pub trait Query: Send + Sync {
50 fn scorer<'a>(&'a self, reader: &'a SegmentReader, limit: usize) -> ScorerFuture<'a>;
55
56 fn count_estimate<'a>(&'a self, reader: &'a SegmentReader) -> CountFuture<'a>;
58
59 fn as_term_query_info(&self) -> Option<TermQueryInfo> {
63 None
64 }
65}
66
67#[cfg(target_arch = "wasm32")]
69pub trait Query {
70 fn scorer<'a>(&'a self, reader: &'a SegmentReader, limit: usize) -> ScorerFuture<'a>;
75
76 fn count_estimate<'a>(&'a self, reader: &'a SegmentReader) -> CountFuture<'a>;
78
79 fn as_term_query_info(&self) -> Option<TermQueryInfo> {
81 None
82 }
83}
84
85impl Query for Box<dyn Query> {
86 fn scorer<'a>(&'a self, reader: &'a SegmentReader, limit: usize) -> ScorerFuture<'a> {
87 (**self).scorer(reader, limit)
88 }
89
90 fn count_estimate<'a>(&'a self, reader: &'a SegmentReader) -> CountFuture<'a> {
91 (**self).count_estimate(reader)
92 }
93
94 fn as_term_query_info(&self) -> Option<TermQueryInfo> {
95 (**self).as_term_query_info()
96 }
97}
98
99#[cfg(not(target_arch = "wasm32"))]
101pub trait Scorer: Send {
102 fn doc(&self) -> DocId;
104
105 fn score(&self) -> Score;
107
108 fn advance(&mut self) -> DocId;
110
111 fn seek(&mut self, target: DocId) -> DocId;
113
114 fn size_hint(&self) -> u32;
116}
117
118#[cfg(target_arch = "wasm32")]
120pub trait Scorer {
121 fn doc(&self) -> DocId;
123
124 fn score(&self) -> Score;
126
127 fn advance(&mut self) -> DocId;
129
130 fn seek(&mut self, target: DocId) -> DocId;
132
133 fn size_hint(&self) -> u32;
135}
136
137pub struct EmptyScorer;
139
140impl Scorer for EmptyScorer {
141 fn doc(&self) -> DocId {
142 crate::structures::TERMINATED
143 }
144
145 fn score(&self) -> Score {
146 0.0
147 }
148
149 fn advance(&mut self) -> DocId {
150 crate::structures::TERMINATED
151 }
152
153 fn seek(&mut self, _target: DocId) -> DocId {
154 crate::structures::TERMINATED
155 }
156
157 fn size_hint(&self) -> u32 {
158 0
159 }
160}