Class TwoPhaseIterator
- Direct Known Subclasses:
DocValuesRangeIterator
Scorer.twoPhaseIterator() to expose an approximation of a DocIdSetIterator. When the approximation()'s DocIdSetIterator.nextDoc() or
DocIdSetIterator.advance(int) return, matches() needs to be checked in order to
know whether the returned doc ID actually matches.- WARNING: This API is experimental and might change in incompatible ways in the next release.
-
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedTwoPhaseIterator(DocIdSetIterator approximation) Takes the approximation to be returned byapproximation. -
Method Summary
Modifier and TypeMethodDescriptionvoidapplyMask(int upTo, FixedBitSet bitSet, int offset) Confirmsmatches()only for the doc IDs whose bit is set inbitSet(the document whose ID isibeing stored at biti - offset), clearing bits for docs that turn out not to match.Return an approximation.static DocIdSetIteratorasDocIdSetIterator(TwoPhaseIterator twoPhaseIterator) Return aDocIdSetIteratorview of the providedTwoPhaseIterator.intReturns the end of the run of consecutive doc IDs that match thisTwoPhaseIteratorand that contains the current doc ID of the approximation, that is: one plus the last doc ID of the run.voidintoBitSet(int upTo, FixedBitSet bitSet, int offset) Load the doc IDs that both belong to theapproximation()andmatch, and are in[approximation().docID(), upTo), intobitSet, the document whose ID isibeing stored at biti - offset.abstract floatAn estimate of the expected cost to determine that a single documentmatches().abstract booleanmatches()Return whether the current doc ID thatapproximation()is on matches.static TwoPhaseIteratorunwrap(DocIdSetIterator iterator) If the givenDocIdSetIteratorhas been created withasDocIdSetIterator(org.apache.lucene.search.TwoPhaseIterator), then this will return the wrappedTwoPhaseIterator.
-
Field Details
-
approximation
-
-
Constructor Details
-
TwoPhaseIterator
Takes the approximation to be returned byapproximation. Not null.
-
-
Method Details
-
asDocIdSetIterator
Return aDocIdSetIteratorview of the providedTwoPhaseIterator. -
unwrap
If the givenDocIdSetIteratorhas been created withasDocIdSetIterator(org.apache.lucene.search.TwoPhaseIterator), then this will return the wrappedTwoPhaseIterator. Otherwise this returnsnull. -
approximation
Return an approximation. The returnedDocIdSetIteratoris a superset of the matching documents, and each match needs to be confirmed withmatches()in order to know whether it matches or not. -
matches
Return whether the current doc ID thatapproximation()is on matches. This method should only be called when the iterator is positioned -- ie. not whenDocIdSetIterator.docID()is-1orDocIdSetIterator.NO_MORE_DOCS-- and at most once.- Throws:
IOException
-
matchCost
public abstract float matchCost()An estimate of the expected cost to determine that a single documentmatches(). This can be called before iterating the documents ofapproximation(). Returns an expected cost in number of simple operations like addition, multiplication, comparing two numbers and indexing an array. The returned value must be positive. -
docIDRunEnd
Returns the end of the run of consecutive doc IDs that match thisTwoPhaseIteratorand that contains the current doc ID of the approximation, that is: one plus the last doc ID of the run.Note: It is illegal to call this method when the approximation is exhausted or not positioned.
The default implementation is conservative and returns the current doc ID of the approximation, i.e. it reports no matching run. Unlike a positioned
DocIdSetIterator-- whose current doc is by definition a match -- a positionedTwoPhaseIteratorhas not yet confirmed that the current doc matches, so the default cannot claim it as part of a matching run without a (potentially side-effecting)matches()call. Callers must therefore treat a return value equal toapproximation().docID()as "no known matching run" and fall back to per-docmatches()confirmation. Implementations that can cheaply prove that a run of docs matches -- e.g. a doc-values skipper reporting a fully-matching block -- should override this to return the real run end.- Throws:
IOException
-
intoBitSet
Load the doc IDs that both belong to theapproximation()andmatch, and are in[approximation().docID(), upTo), intobitSet, the document whose ID isibeing stored at biti - offset. Upon return theapproximation()is positioned on the first doc that is>= upTo, mirroringDocIdSetIterator.intoBitSet(int, org.apache.lucene.util.FixedBitSet, int).The default implementation walks the
approximation()and confirms each doc withmatches()— i.e. it is functionally identical to the leap-frog evaluation, just writing into a bit set. Implementations that can confirm matches in bulk — for instance a vectorized scan over a columnar block — should override it; the bit set window is then collected in one shot rather than one doc at a time.- Throws:
IOException
-
applyMask
Confirmsmatches()only for the doc IDs whose bit is set inbitSet(the document whose ID isibeing stored at biti - offset), clearing bits for docs that turn out not to match. Upon returnapproximation()is positioned on the first doc that is>= upTo, mirroringintoBitSet(int, org.apache.lucene.util.FixedBitSet, int).Prefer this over
intoBitSet(int, org.apache.lucene.util.FixedBitSet, int)followed by a separate intersection whenbitSetalready holds a candidate set narrowed down by other clauses of a conjunction: it avoids confirming matches for docs that are already known not to be candidates.The default implementation walks the set bits of
bitSetone at a time, advancingapproximation()to each and callingmatches(). Implementations that can classify a whole span of a sparse candidate set in bulk -- for instance a block-based skip index that can tell that an entire span is fully matching or fully non-matching without visiting doc values -- should override this.- Throws:
IOException
-