diff options
author | Paul Masurel <paul.masurel@gmail.com> | 2018-02-05 09:33:25 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-02-05 09:33:25 +0900 |
commit | 1fc7afa90a69d47d836ea22bae2da785fb75dad7 (patch) | |
tree | 0d2a874107cab7684a71038a9d90810096c28dde /src/query/scorer.rs | |
parent | 6a104e4f696d47a9aac7639fad5f2654970dd0e9 (diff) |
Issue/range query (#242)
BitSet and RangeQuery
Diffstat (limited to 'src/query/scorer.rs')
-rw-r--r-- | src/query/scorer.rs | 64 |
1 files changed, 63 insertions, 1 deletions
diff --git a/src/query/scorer.rs b/src/query/scorer.rs index 170e6aa..2cbeb00 100644 --- a/src/query/scorer.rs +++ b/src/query/scorer.rs @@ -2,6 +2,8 @@ use DocSet; use DocId; use Score; use collector::Collector; +use postings::SkipResult; +use common::BitSet; use std::ops::{Deref, DerefMut}; /// Scored set of documents matching a query within a specific segment. @@ -49,7 +51,7 @@ impl DocSet for EmptyScorer { DocId::max_value() } - fn size_hint(&self) -> usize { + fn size_hint(&self) -> u32 { 0 } } @@ -59,3 +61,63 @@ impl Scorer for EmptyScorer { 0f32 } } + + +/// Wraps a `DocSet` and simply returns a constant `Scorer`. +/// The `ConstScorer` is useful if you have a `DocSet` where +/// you needed a scorer. +/// +/// The `ConstScorer`'s constant score can be set +/// by calling `.set_score(...)`. +pub struct ConstScorer<TDocSet: DocSet> { + docset: TDocSet, + score: Score, +} + +impl<TDocSet: DocSet> ConstScorer<TDocSet> { + + /// Creates a new `ConstScorer`. + pub fn new(docset: TDocSet) -> ConstScorer<TDocSet> { + ConstScorer { + docset, + score: 1f32, + } + } + + /// Sets the constant score to a different value. + pub fn set_score(&mut self, score: Score) { + self.score = score; + } +} + +impl<TDocSet: DocSet> DocSet for ConstScorer<TDocSet> { + fn advance(&mut self) -> bool { + self.docset.advance() + } + + fn skip_next(&mut self, target: DocId) -> SkipResult { + self.docset.skip_next(target) + } + + fn fill_buffer(&mut self, buffer: &mut [DocId]) -> usize { + self.docset.fill_buffer(buffer) + } + + fn doc(&self) -> DocId { + self.docset.doc() + } + + fn size_hint(&self) -> u32 { + self.docset.size_hint() + } + + fn append_to_bitset(&mut self, bitset: &mut BitSet) { + self.docset.append_to_bitset(bitset); + } +} + +impl<TDocSet: DocSet> Scorer for ConstScorer<TDocSet> { + fn score(&self) -> Score { + 1f32 + } +} |