1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use crate::core::util::bits::Bits;
use crate::core::util::error::lucene_error::Result;
/// This trait is used to score a range of documents at once, and is returned by [`Weight::bulk_scorer`](crate::core::search::weight::Weight::bulk_scorer).
///
/// Only queries that have a more optimized means of scoring across a range of
/// documents need to provide this method. Otherwise, a default implementation is
/// wrapped around the [`Scorer`](crate::core::search::scorer::Scorer) returned by [`Weight::scorer`](crate::core::search::weight::Weight::scorer).
pub trait BulkScorer {
/// Collects matching documents in a range and returns an estimation of the
/// next matching document which is on or after `max`.
///
/// # Return value
///
/// - `>= max`
/// - [`NO_MORE_DOCS`](crate::core::search::doc_id_set_iterator::NO_MORE_DOCS) if there are no more matches
/// - `<=` the first matching document that is `>= max` otherwise
///
/// # Parameters
///
/// - `collector`: The collector to which all matching documents are passed.
/// - `accept_docs`: [`Bits`] that represents the allowed documents to match,
/// or `None` if all are allowed to match.
/// - `min`: Score starting at, including, this document.
/// - `max`: Score up to, but not including, this doc.
///
/// # Notes
///
/// - `min` is the minimum document to be considered for matching. All documents
/// strictly before this value must be ignored.
/// - Although `max` would be a legal return value for this method, higher values
/// might help callers skip more efficiently over non-matching portions of the
/// docID space.
///
/// # Returns
///
/// An under-estimation of the next matching doc after `max`.
fn score<CLC: crate::core::search::leaf_collector::LeafCollector, AB: Bits + ?Sized>(
&mut self,
collector: &mut CLC,
accept_docs: Option<&AB>,
min: i32,
max: i32,
) -> Result<i32>;
/// Same as [`DocIdSetIterator::cost`](crate::core::search::doc_id_set_iterator::DocIdSetIterator::cost) for bulk scorers.
fn cost(&mut self) -> Result<i64>;
#[cfg(test)]
fn kind(&self) -> BulkScorerKind {
unimplemented!("")
}
}
#[cfg(test)]
pub enum BulkScorerKind {
Asserting,
Default,
ReqExcl,
}
macro_rules! either_bulk_scorer {
($vis:vis $name:ident { $( $Variant:ident : $T:ident ),+ $(,)? }) => {
$vis enum $name<$( $T ),+> {
$( $Variant($T), )+
}
impl<$( $T ),+> BulkScorer for $name<$( $T ),+>
where
$( $T: BulkScorer ),+
{
#[inline]
fn score<CLC: crate::core::search::leaf_collector::LeafCollector, AB: Bits + ?Sized>(
&mut self,
collector: &mut CLC,
accept_docs: Option<&AB>,
min: i32,
max: i32,
) -> Result<i32> {
match self {
$( Self::$Variant(inner) => inner.score(collector, accept_docs, min, max), )+
}
}
#[inline]
fn cost(&mut self) -> Result<i64> {
match self {
$( Self::$Variant(inner) => inner.cost(), )+
}
}
#[cfg(test)]
fn kind(&self) -> BulkScorerKind{
match self { $( Self::$Variant(inner) => inner.kind(), )+ }
}
}
};
}
either_bulk_scorer!(pub BulkScorerEnum2 { A: A1, B: B1});
either_bulk_scorer!(pub BulkScorerEnum4 { A: A1, B: B1, C: C1, D: D1});
impl<T> BulkScorer for Box<T>
where
T: BulkScorer + ?Sized,
{
fn score<CLC: crate::core::search::leaf_collector::LeafCollector, AB: Bits + ?Sized>(
&mut self,
collector: &mut CLC,
accept_docs: Option<&AB>,
min: i32,
max: i32,
) -> Result<i32> {
(**self).score(collector, accept_docs, min, max)
}
fn cost(&mut self) -> Result<i64> {
(**self).cost()
}
#[cfg(test)]
fn kind(&self) -> BulkScorerKind {
(**self).kind()
}
}