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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
* 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::index::index_reader_context::{IRCLeafReader, IndexReaderContext};
use crate::core::index::leaf_reader_context::LeafReaderContext;
use crate::core::search::bulk_scorer::BulkScorer;
use crate::core::search::index_searcher::IndexSearcher;
use crate::core::search::scorer::Scorer;
use crate::core::search::similarities_impl::similarities::Similarity;
use crate::core::search::weight::DefaultBulkScorer;
use crate::core::util::error::lucene_error::Result;
#[cfg(test)]
use std::any::Any;
/// A supplier of [`Scorer`].
///
/// This allows to get an estimate of the cost before building the [`Scorer`].
pub trait ScorerSupplier<
IRC: IndexReaderContext,
SIM = crate::core::search::similarities_impl::bm25_similarity::BM25Similarity,
> where
SIM: Similarity,
{
type Scorer: Scorer;
type BulkScorer: BulkScorer;
/// Get the [`Scorer`].
/// This must be called at most once.
///
/// # Parameters
///
/// - `lead_cost`: Cost of the scorer that will be used in order to lead iteration.
/// This can be interpreted as an upper bound of the number of times that
/// [`DocIdSetIterator::next_doc`](crate::core::search::doc_id_set_iterator::DocIdSetIterator::next_doc), [`DocIdSetIterator::advance`](crate::core::search::doc_id_set_iterator::DocIdSetIterator::advance), and
/// [`TwoPhaseIterator::matches`](crate::core::search::two_phase_iterator::TwoPhaseIterator::matches) will be called.
/// If in doubt, pass `i64::MAX`, which will produce a [`Scorer`] that has good iteration capabilities.
/// - `context`: The [`LeafReaderContext`] that this scorer supplier was created for.
fn get(
&mut self,
lead_cost: i64,
context: &LeafReaderContext<IRCLeafReader<IRC>>,
searcher: &IndexSearcher<IRC, SIM>,
) -> Result<Self::Scorer>
where
IndexSearcher<IRC, SIM>: Sync;
/// Optional: Get a bulk scorer that is optimized for bulk-scoring.
///
/// The default implementation wraps `get(i64::MAX)` in a [`DefaultBulkScorer`],
/// which iterates matches from the scorer. Some queries can have more efficient
/// approaches for matching all hits.
fn bulk_scorer(
&mut self,
context: &LeafReaderContext<IRCLeafReader<IRC>>,
searcher: &IndexSearcher<IRC, SIM>,
) -> Result<Option<Self::BulkScorer>>
where
IndexSearcher<IRC, SIM>: Sync;
fn default_bulk_scorer(
&mut self,
context: &LeafReaderContext<IRCLeafReader<IRC>>,
searcher: &IndexSearcher<IRC, SIM>,
) -> Result<DefaultBulkScorer<Self::Scorer>>
where
IndexSearcher<IRC, SIM>: Sync,
{
let scorer = self.get(i64::MAX, context, searcher)?;
Ok(DefaultBulkScorer::new(scorer))
}
/// Get an estimate of the [`Scorer`] that would be returned by [`ScorerSupplier::get`].
/// This may be a costly operation, so it should only be called if necessary.
///
/// Corresponds to [`DocIdSetIterator::cost`](crate::core::search::doc_id_set_iterator::DocIdSetIterator::cost).
fn cost(
&mut self,
context: &LeafReaderContext<IRCLeafReader<IRC>>,
searcher: &IndexSearcher<IRC, SIM>,
) -> Result<i64>
where
IndexSearcher<IRC, SIM>: Sync;
/// Inform this [`ScorerSupplier`] that its returned scorers produce scores that get passed
/// to the collector, as opposed to partial scores that then need to get combined (e.g. summed up).
///
/// Note: This method also gets called if scores are not requested, e.g. because the score mode
/// is [`ScoreMode::CompleteNoScores`](crate::core::search::score_mode::ScoreMode::CompleteNoScores).
/// Implementations should look at both the score mode and this `bool` to know whether to prepare
/// for reacting to [`Scorable::set_min_competitive_score`](crate::core::search::scorable::Scorable::set_min_competitive_score) calls.
fn set_top_level_scoring_clause(&mut self) -> Result<()> {
Ok(())
}
#[cfg(test)]
fn as_any(&mut self) -> &mut dyn std::any::Any {
unreachable!("")
}
}
impl<IRC, T, SIM> ScorerSupplier<IRC, SIM> for Box<T>
where
SIM: Similarity,
IRC: IndexReaderContext,
T: ScorerSupplier<IRC, SIM> + ?Sized,
{
type Scorer = T::Scorer;
type BulkScorer = T::BulkScorer;
fn get(
&mut self,
lead_cost: i64,
context: &LeafReaderContext<IRCLeafReader<IRC>>,
searcher: &IndexSearcher<IRC, SIM>,
) -> Result<Self::Scorer>
where
IndexSearcher<IRC, SIM>: Sync,
{
(**self).get(lead_cost, context, searcher)
}
fn bulk_scorer(
&mut self,
context: &LeafReaderContext<IRCLeafReader<IRC>>,
searcher: &IndexSearcher<IRC, SIM>,
) -> Result<Option<Self::BulkScorer>>
where
IndexSearcher<IRC, SIM>: Sync,
{
(**self).bulk_scorer(context, searcher)
}
fn default_bulk_scorer(
&mut self,
context: &LeafReaderContext<IRCLeafReader<IRC>>,
searcher: &IndexSearcher<IRC, SIM>,
) -> Result<DefaultBulkScorer<Self::Scorer>>
where
IndexSearcher<IRC, SIM>: Sync,
{
(**self).default_bulk_scorer(context, searcher)
}
fn cost(
&mut self,
context: &LeafReaderContext<IRCLeafReader<IRC>>,
searcher: &IndexSearcher<IRC, SIM>,
) -> Result<i64>
where
IndexSearcher<IRC, SIM>: Sync,
{
(**self).cost(context, searcher)
}
fn set_top_level_scoring_clause(&mut self) -> Result<()> {
(**self).set_top_level_scoring_clause()
}
#[cfg(test)]
fn as_any(&mut self) -> &mut dyn Any {
(**self).as_any()
}
}