Package org.apache.lucene.search
Class WANDScorer
java.lang.Object
org.apache.lucene.search.Scorable
org.apache.lucene.search.Scorer
org.apache.lucene.search.WANDScorer
This implements the WAND (Weak AND) algorithm for dynamic pruning
described in "Efficient Query Evaluation using a Two-Level Retrieval
Process" by Broder, Carmel, Herscovici, Soffer and Zien. Enhanced with
techniques described in "Faster Top-k Document Retrieval Using Block-Max
Indexes" by Ding and Suel.
This scorer maintains a feedback loop with the collector in order to
know at any time the minimum score that is required in order for a hit
to be competitive. Then it leverages the
max score
from each scorer in order to know when it may call
DocIdSetIterator.advance(int) rather than DocIdSetIterator.nextDoc()
to move to the next competitive hit.
Implementation is similar to MinShouldMatchSumScorer except that
instead of enforcing that freq >= minShouldMatch, we enforce that
∑ max_score >= minCompetitiveScore.-
Nested Class Summary
Nested classes/interfaces inherited from class org.apache.lucene.search.Scorable
Scorable.ChildScorable -
Field Summary
FieldsModifier and TypeFieldDescription(package private) final long(package private) int(package private) int(package private) final DisiPriorityQueue(package private) DisiWrapper(package private) long(package private) final MaxScoreSumPropagatorprivate long(package private) final intprivate final int(package private) final DisiWrapper[](package private) long(package private) int(package private) int -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate voidaddLead(DisiWrapper lead) Add a disi to the linked list of leads.private voidAdd an entry to 'tail'.private voidAdvance all entries from the tail to know about all matches on the current doc.private voidadvanceHead(int target) Make sure all disis in 'head' are on or after 'target'.intadvanceShallow(int target) Advance to the block of documents that containstargetin order to get scoring information about this block.private voidPop the entry from the 'tail' that has the greatest score contribution, advance it to the current doc and then add it to 'lead' or 'head' depending on whether it matches.private voidadvanceTail(DisiWrapper disi) intdocID()Returns the doc ID that is currently being scored.private intMove iterators to the tail until there is a potential match.private static voiddownHeapMaxScore(DisiWrapper[] heap, int size) private booleanfinal Collection<Scorable.ChildScorable> Returns child sub-scorers positioned on the current documentfloatgetMaxScore(int upTo) Return the maximum score that documents between the lasttargetthat this iterator wasshallow-advancedto included andupToincluded.private static booleangreaterMaxScore(DisiWrapper w1, DisiWrapper w2) In the tail, we want to get first entries that produce the maximum scores and in case of ties (eg.private DisiWrapperInsert an entry in 'tail' and evict the least-costly scorer if full.iterator()Return aDocIdSetIteratorover matching documents.private voidmoveToNextCandidate(int target) Set 'doc' to the next potential match, and move all disis of 'head' that are on this doc into 'lead'.private DisiWrapperpopTail()Pop the least-costly scorer from 'tail'.private voidpushBackLeads(int target) Move disis that are in 'lead' back to the tail.private static longscaleMaxScore(float maxScore, int scalingFactor) Scale max scores in an unsigned integer to avoid overflows (only the lower 32 bits of the long are used) as well as floating-point arithmetic errors.private static longscaleMinScore(float minScore, int scalingFactor) Scale min competitive scores the same way as max scores but this time by rounding down in order to make sure that we do not miss any matches.(package private) static intscalingFactor(float f) Return a scaling factor for the given float so that f x 2^scalingFactor would be in ]2^15, 2^16].floatscore()Returns the score of the current document matching the query.voidsetMinCompetitiveScore(float minScore) Optional method: Tell the scorer that its iterator may safely ignore all documents whose score is less than the givenminScore.Optional method: Return aTwoPhaseIteratorview of thisScorer.private voidupdateMaxScores(int target) private voidupdateMaxScoresIfNecessary(int target) UpdateupToand maximum scores of sub scorers so thatupTois greater than or equal to the next candidate aftertarget, i.e.private static voidupHeapMaxScore(DisiWrapper[] heap, int i) Heap helpers
-
Field Details
-
scalingFactor
private final int scalingFactor -
minCompetitiveScore
private long minCompetitiveScore -
lead
DisiWrapper lead -
doc
int doc -
leadMaxScore
long leadMaxScore -
head
-
tail
-
tailMaxScore
long tailMaxScore -
tailSize
int tailSize -
cost
final long cost -
maxScorePropagator
-
upTo
int upTo -
minShouldMatch
final int minShouldMatch -
freq
int freq
-
-
Constructor Details
-
WANDScorer
WANDScorer(Weight weight, Collection<Scorer> scorers, int minShouldMatch) throws IOException - Throws:
IOException
-
-
Method Details
-
scalingFactor
static int scalingFactor(float f) Return a scaling factor for the given float so that f x 2^scalingFactor would be in ]2^15, 2^16]. Special cases: scalingFactor(0) = scalingFactor(MIN_VALUE) - 1 scalingFactor(+Infty) = scalingFactor(MAX_VALUE) + 1 -
scaleMaxScore
private static long scaleMaxScore(float maxScore, int scalingFactor) Scale max scores in an unsigned integer to avoid overflows (only the lower 32 bits of the long are used) as well as floating-point arithmetic errors. Those are rounded up in order to make sure we do not miss any matches. -
scaleMinScore
private static long scaleMinScore(float minScore, int scalingFactor) Scale min competitive scores the same way as max scores but this time by rounding down in order to make sure that we do not miss any matches. -
ensureConsistent
private boolean ensureConsistent() -
setMinCompetitiveScore
Description copied from class:ScorableOptional method: Tell the scorer that its iterator may safely ignore all documents whose score is less than the givenminScore. This is a no-op by default. This method may only be called from collectors that useScoreMode.TOP_SCORES, and successive calls may only set increasing values ofminScore.- Overrides:
setMinCompetitiveScorein classScorable- Throws:
IOException
-
getChildren
Description copied from class:ScorableReturns child sub-scorers positioned on the current document- Overrides:
getChildrenin classScorable- Throws:
IOException
-
iterator
Description copied from class:ScorerReturn aDocIdSetIteratorover matching documents. The returned iterator will either be positioned on-1if no documents have been scored yet,DocIdSetIterator.NO_MORE_DOCSif all documents have been scored already, or the last document id that has been scored otherwise. The returned iterator is a view: calling this method several times will return iterators that have the same state. -
twoPhaseIterator
Description copied from class:ScorerOptional method: Return aTwoPhaseIteratorview of thisScorer. A return value ofnullindicates that two-phase iteration is not supported. Note that the returnedTwoPhaseIterator'sapproximationmust advance synchronously with theScorer.iterator(): advancing the approximation must advance the iterator and vice-versa. Implementing this method is typically useful onScorers that have a high per-document overhead in order to confirm matches. The default implementation returnsnull.- Overrides:
twoPhaseIteratorin classScorer
-
addLead
Add a disi to the linked list of leads. -
pushBackLeads
Move disis that are in 'lead' back to the tail.- Throws:
IOException
-
advanceHead
Make sure all disis in 'head' are on or after 'target'.- Throws:
IOException
-
advanceTail
- Throws:
IOException
-
advanceTail
Pop the entry from the 'tail' that has the greatest score contribution, advance it to the current doc and then add it to 'lead' or 'head' depending on whether it matches.- Throws:
IOException
-
updateMaxScores
- Throws:
IOException
-
updateMaxScoresIfNecessary
UpdateupToand maximum scores of sub scorers so thatupTois greater than or equal to the next candidate aftertarget, i.e. the top of `head`.- Throws:
IOException
-
moveToNextCandidate
Set 'doc' to the next potential match, and move all disis of 'head' that are on this doc into 'lead'.- Throws:
IOException
-
doNextCompetitiveCandidate
Move iterators to the tail until there is a potential match.- Throws:
IOException
-
advanceAllTail
Advance all entries from the tail to know about all matches on the current doc.- Throws:
IOException
-
score
Description copied from class:ScorableReturns the score of the current document matching the query.- Specified by:
scorein classScorable- Throws:
IOException
-
advanceShallow
Description copied from class:ScorerAdvance to the block of documents that containstargetin order to get scoring information about this block. This method is implicitly called byDocIdSetIterator.advance(int)andDocIdSetIterator.nextDoc()on the returned doc ID. Calling this method doesn't modify the currentDocIdSetIterator.docID(). It returns a number that is greater than or equal to all documents contained in the current block, but less than any doc IDS of the next block.targetmust be >=Scorable.docID()as well as all targets that have been passed toScorer.advanceShallow(int)so far.- Overrides:
advanceShallowin classScorer- Throws:
IOException
-
getMaxScore
Description copied from class:ScorerReturn the maximum score that documents between the lasttargetthat this iterator wasshallow-advancedto included andupToincluded.- Specified by:
getMaxScorein classScorer- Throws:
IOException
-
docID
public int docID()Description copied from class:ScorableReturns the doc ID that is currently being scored. -
insertTailWithOverFlow
Insert an entry in 'tail' and evict the least-costly scorer if full. -
addTail
Add an entry to 'tail'. Fails if over capacity. -
popTail
Pop the least-costly scorer from 'tail'. -
upHeapMaxScore
Heap helpers -
downHeapMaxScore
-
greaterMaxScore
In the tail, we want to get first entries that produce the maximum scores and in case of ties (eg. constant-score queries), those that have the least cost so that they are likely to advance further.
-