hermes_core/query/
boost.rs

1//! Boost query - multiplies the score of the inner query
2
3use crate::segment::SegmentReader;
4use crate::{DocId, Score};
5
6use super::{CountFuture, Query, Scorer, ScorerFuture};
7
8/// Boost query - multiplies the score of the inner query
9pub struct BoostQuery {
10    pub inner: Box<dyn Query>,
11    pub boost: f32,
12}
13
14impl std::fmt::Debug for BoostQuery {
15    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
16        f.debug_struct("BoostQuery")
17            .field("boost", &self.boost)
18            .finish()
19    }
20}
21
22impl BoostQuery {
23    pub fn new(query: impl Query + 'static, boost: f32) -> Self {
24        Self {
25            inner: Box::new(query),
26            boost,
27        }
28    }
29}
30
31impl Query for BoostQuery {
32    fn scorer<'a>(&'a self, reader: &'a SegmentReader) -> ScorerFuture<'a> {
33        Box::pin(async move {
34            let inner_scorer = self.inner.scorer(reader).await?;
35            Ok(Box::new(BoostScorer {
36                inner: inner_scorer,
37                boost: self.boost,
38            }) as Box<dyn Scorer + 'a>)
39        })
40    }
41
42    fn count_estimate<'a>(&'a self, reader: &'a SegmentReader) -> CountFuture<'a> {
43        Box::pin(async move { self.inner.count_estimate(reader).await })
44    }
45}
46
47struct BoostScorer<'a> {
48    inner: Box<dyn Scorer + 'a>,
49    boost: f32,
50}
51
52impl Scorer for BoostScorer<'_> {
53    fn doc(&self) -> DocId {
54        self.inner.doc()
55    }
56
57    fn score(&self) -> Score {
58        self.inner.score() * self.boost
59    }
60
61    fn advance(&mut self) -> DocId {
62        self.inner.advance()
63    }
64
65    fn seek(&mut self, target: DocId) -> DocId {
66        self.inner.seek(target)
67    }
68
69    fn size_hint(&self) -> u32 {
70        self.inner.size_hint()
71    }
72}