PhraseQuery, built with one or more MultiTermQuery that
provides term expansions for multi-terms (one of the expanded terms must match).
Its main advantage is to control the total number of expansions across all MultiTermQuery and across all segments.
Use the PhraseWildcardQuery.Builder to build a PhraseWildcardQuery.
This query is similar to MultiPhraseQuery, but it handles, controls and optimizes the
multi-term expansions.
This query is equivalent to building an ordered SpanNearQuery with a list of SpanTermQuery and SpanMultiTermQueryWrapper. But it optimizes the multi-term
expansions and the segment accesses. It first resolves the single-terms to early stop if some
does not match. Then it expands each multi-term sequentially, stopping immediately if one does
not match. It detects the segments that do not match to skip them for the next expansions. This
often avoid expanding the other multi-terms on some or even all segments. And finally it controls
the total number of expansions.
Immutable.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilds aPhraseWildcardQuery.protected static classPhrase term with expansions.protected static classAllPhraseWildcardQuery.PhraseTermare light and immutable.protected classCompares segments based of the number of terms they contain.protected static classPhrase term with no expansion.static classHolds a pair of term bytes - term state.protected static classprotected static classHolds theTermStateandTermStatisticsfor all the matched and collectedTerm, for all phrase terms, for all segments.static classAccumulates the doc freq and total term freq.protected static classTest counters incremented when assertions are enabled. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final Stringprotected final intprotected static final Queryprotected final List<PhraseWildcardQuery.PhraseTerm> protected final booleanprotected final int -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedPhraseWildcardQuery(String field, List<PhraseWildcardQuery.PhraseTerm> phraseTerms, int slop, int maxMultiTermExpansions, boolean segmentOptimizationEnabled) -
Method Summary
Modifier and TypeMethodDescriptionprotected voidcheckTermsHavePositions(Terms terms) protected intcollectMultiTermData(PhraseWildcardQuery.MultiTerm multiTerm, IndexSearcher searcher, List<LeafReaderContext> segments, int remainingMultiTerms, int maxExpansionsForTerm, PhraseWildcardQuery.TermsData termsData) Collects theTermStateandTermStatisticsfor a multi-term with expansion.protected List<PhraseWildcardQuery.TermBytesTermState> collectMultiTermDataForSegment(PhraseWildcardQuery.MultiTerm multiTerm, LeafReaderContext leafReaderContext, int remainingExpansions, MutableValueBool shouldStopSegmentIteration, Map<BytesRef, PhraseWildcardQuery.TermStats> termStatsMap) Collects theTermStatelist andTermStatisticsfor a multi-term on a specific index segment.protected voidcollectMultiTermStats(IndexSearcher searcher, Map<BytesRef, PhraseWildcardQuery.TermStats> termStatsMap, PhraseWildcardQuery.TermsData termsData, PhraseWildcardQuery.TermData termData) Collect the term stats across all segments.protected intcollectSingleTermData(PhraseWildcardQuery.SingleTerm singleTerm, IndexSearcher searcher, List<LeafReaderContext> segments, PhraseWildcardQuery.TermsData termsData) Collects theTermStateandTermStatisticsfor a single-term without expansion.(package private) PhraseWeightcreatePhraseWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost, PhraseWildcardQuery.TermsData termsData) protected PhraseWildcardQuery.TermsDatacreateTermsData(int numSegments) Creates newPhraseWildcardQuery.TermsData.protected TermsEnumcreateTermsEnum(PhraseWildcardQuery.MultiTerm multiTerm, LeafReaderContext leafReaderContext) Creates theTermsEnumfor the givenPhraseWildcardQuery.MultiTermand segment.protected Map<BytesRef, PhraseWildcardQuery.TermStats> createTermStatsMap(PhraseWildcardQuery.MultiTerm multiTerm) Creates aPhraseWildcardQuery.TermStatsmap for aPhraseWildcardQuery.MultiTerm.createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) Expert: Constructs an appropriate Weight implementation for this query.protected WeightbooleanOverride and implement query instance equivalence properly in a subclass.getField()inthashCode()Override and implement query hash code properly in a subclass.protected Weightrewrite(IndexSearcher indexSearcher) Expert: called to re-write queries into primitive queries.protected booleanfinal StringPrints a query to a string, withfieldassumed to be the default field and omitted.voidvisit(QueryVisitor visitor) Recurse through the query tree, visiting any child queries.Methods inherited from class org.apache.lucene.search.Query
classHash, rewrite, sameClassAs, toString
-
Field Details
-
NO_MATCH_QUERY
-
field
-
phraseTerms
-
slop
protected final int slop -
maxMultiTermExpansions
protected final int maxMultiTermExpansions -
segmentOptimizationEnabled
protected final boolean segmentOptimizationEnabled
-
-
Constructor Details
-
PhraseWildcardQuery
protected PhraseWildcardQuery(String field, List<PhraseWildcardQuery.PhraseTerm> phraseTerms, int slop, int maxMultiTermExpansions, boolean segmentOptimizationEnabled)
-
-
Method Details
-
getField
-
rewrite
Description copied from class:QueryExpert: called to re-write queries into primitive queries. For example, a PrefixQuery will be rewritten into a BooleanQuery that consists of TermQuerys.Callers are expected to call
rewritemultiple times if necessary, until the rewritten query is the same as the original query.The rewrite process may be able to make use of IndexSearcher's executor and be executed in parallel if the executor is provided.
However, if any of the intermediary queries do not satisfy the new API, parallel rewrite is not possible for any subsequent sub-queries. To take advantage of this API, the entire query tree must override this method.
- Overrides:
rewritein classQuery- Throws:
IOException- See Also:
-
visit
Description copied from class:QueryRecurse through the query tree, visiting any child queries. -
createWeight
public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException Description copied from class:QueryExpert: Constructs an appropriate Weight implementation for this query.Only implemented by primitive queries, which re-write to themselves.
- Overrides:
createWeightin classQuery- Parameters:
scoreMode- How the produced scorers will be consumed.boost- The boost that is propagated by the parent queries.- Throws:
IOException
-
createTermsData
Creates newPhraseWildcardQuery.TermsData. -
earlyStopWeight
-
noMatchWeight
-
createPhraseWeight
PhraseWeight createPhraseWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost, PhraseWildcardQuery.TermsData termsData) throws IOException - Throws:
IOException
-
equals
Description copied from class:QueryOverride and implement query instance equivalence properly in a subclass. This is required so thatQueryCacheworks properly.Typically a query will be equal to another only if it's an instance of the same class and its document-filtering properties are identical to those of the other instance. Utility methods are provided for certain repetitive code.
-
hashCode
public int hashCode()Description copied from class:QueryOverride and implement query hash code properly in a subclass. This is required so thatQueryCacheworks properly. -
toString
Description copied from class:QueryPrints a query to a string, withfieldassumed to be the default field and omitted. -
collectSingleTermData
protected int collectSingleTermData(PhraseWildcardQuery.SingleTerm singleTerm, IndexSearcher searcher, List<LeafReaderContext> segments, PhraseWildcardQuery.TermsData termsData) throws IOException Collects theTermStateandTermStatisticsfor a single-term without expansion.- Parameters:
termsData- receives the collected data.- Throws:
IOException
-
collectMultiTermData
protected int collectMultiTermData(PhraseWildcardQuery.MultiTerm multiTerm, IndexSearcher searcher, List<LeafReaderContext> segments, int remainingMultiTerms, int maxExpansionsForTerm, PhraseWildcardQuery.TermsData termsData) throws IOException Collects theTermStateandTermStatisticsfor a multi-term with expansion.- Parameters:
remainingMultiTerms- the number of remaining multi-terms to process, including the current one, excluding the multi-terms already processed.termsData- receives the collected data.- Throws:
IOException
-
shouldOptimizeSegments
protected boolean shouldOptimizeSegments() -
createTermStatsMap
protected Map<BytesRef,PhraseWildcardQuery.TermStats> createTermStatsMap(PhraseWildcardQuery.MultiTerm multiTerm) Creates aPhraseWildcardQuery.TermStatsmap for aPhraseWildcardQuery.MultiTerm. -
collectMultiTermDataForSegment
protected List<PhraseWildcardQuery.TermBytesTermState> collectMultiTermDataForSegment(PhraseWildcardQuery.MultiTerm multiTerm, LeafReaderContext leafReaderContext, int remainingExpansions, MutableValueBool shouldStopSegmentIteration, Map<BytesRef, PhraseWildcardQuery.TermStats> termStatsMap) throws IOExceptionCollects theTermStatelist andTermStatisticsfor a multi-term on a specific index segment.- Parameters:
remainingExpansions- the number of remaining expansions allowed for the segment.shouldStopSegmentIteration- to be set to true to stop the segment iteration calling this method repeatedly.termStatsMap- receives the collectedPhraseWildcardQuery.TermStatsacross all segments.- Throws:
IOException
-
createTermsEnum
protected TermsEnum createTermsEnum(PhraseWildcardQuery.MultiTerm multiTerm, LeafReaderContext leafReaderContext) throws IOException Creates theTermsEnumfor the givenPhraseWildcardQuery.MultiTermand segment.- Returns:
- null if there is no term for this query field in the segment.
- Throws:
IOException
-
collectMultiTermStats
protected void collectMultiTermStats(IndexSearcher searcher, Map<BytesRef, PhraseWildcardQuery.TermStats> termStatsMap, PhraseWildcardQuery.TermsData termsData, PhraseWildcardQuery.TermData termData) throws IOExceptionCollect the term stats across all segments.- Parameters:
termStatsMap- input map of already collectedPhraseWildcardQuery.TermStats.termsData- receives theTermStatisticscomputed for allPhraseWildcardQuery.TermStats.termData- receives all the collectedTerm.- Throws:
IOException
-
checkTermsHavePositions
-