Class MultiTermQuery
- Direct Known Subclasses:
AutomatonQuery,FuzzyQuery,TermsQuery
Query that matches documents
containing a subset of terms provided by a FilteredTermsEnum enumeration.
This query cannot be used directly; you must subclass
it and define getTermsEnum(Terms,AttributeSource) to provide a FilteredTermsEnum that iterates through the terms to be
matched.
NOTE: if setRewriteMethod(org.apache.lucene.search.MultiTermQuery.RewriteMethod) is either
CONSTANT_SCORE_BOOLEAN_REWRITE or SCORING_BOOLEAN_REWRITE, you may encounter a
BooleanQuery.TooManyClauses exception during
searching, which happens when the number of terms to be
searched exceeds BooleanQuery.getMaxClauseCount(). Setting setRewriteMethod(org.apache.lucene.search.MultiTermQuery.RewriteMethod) to CONSTANT_SCORE_REWRITE
prevents this.
The recommended rewrite method is CONSTANT_SCORE_REWRITE: it doesn't spend CPU
computing unhelpful scores, and is the most
performant rewrite method given the query. If you
need scoring (like FuzzyQuery, use
MultiTermQuery.TopTermsScoringBooleanQueryRewrite which uses
a priority queue to only collect competitive terms
and not hit this limitation.
Note that org.apache.lucene.queryparser.classic.QueryParser produces
MultiTermQueries using CONSTANT_SCORE_REWRITE
by default.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classAbstract class that defines how the query is rewritten.static final classA rewrite method that first translates each term intoBooleanClause.Occur.SHOULDclause in a BooleanQuery, but adjusts the frequencies used for scoring to be blended across the terms, otherwise the rarest term typically ranks highest (often not useful eg in the set of expanded terms in a FuzzyQuery).static final classA rewrite method that first translates each term intoBooleanClause.Occur.SHOULDclause in a BooleanQuery, but the scores are only computed as the boost.static final classA rewrite method that first translates each term intoBooleanClause.Occur.SHOULDclause in a BooleanQuery, and keeps the scores as computed by the query. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final MultiTermQuery.RewriteMethodLikeSCORING_BOOLEAN_REWRITEexcept scores are not computed.static final MultiTermQuery.RewriteMethodA rewrite method that first creates a private Filter, by visiting each term in sequence and marking all docs for that term.protected final Stringprotected MultiTermQuery.RewriteMethodstatic final MultiTermQuery.RewriteMethodA rewrite method that first translates each term intoBooleanClause.Occur.SHOULDclause in a BooleanQuery, and keeps the scores as computed by the query. -
Constructor Summary
ConstructorsConstructorDescriptionMultiTermQuery(String field) Constructs a query matching terms that cannot be represented with a single Term. -
Method Summary
Modifier and TypeMethodDescriptionbooleanOverride and implement query instance equivalence properly in a subclass.private booleanequalsTo(MultiTermQuery other) final StringgetField()Returns the field name for this queryfinal TermsEnumgetTermsEnum(Terms terms) Constructs an enumeration that expands the pattern term.protected abstract TermsEnumgetTermsEnum(Terms terms, AttributeSource atts) Construct the enumeration to be used, expanding the pattern term.inthashCode()Override and implement query hash code properly in a subclass.final Queryrewrite(IndexReader reader) To rewrite to a simpler form, instead return a simpler enum fromgetTermsEnum(Terms, AttributeSource).voidSets the rewrite method to be used when executing the query.Methods inherited from class org.apache.lucene.search.Query
classHash, createWeight, sameClassAs, toString, toString, visit
-
Field Details
-
field
-
rewriteMethod
-
CONSTANT_SCORE_REWRITE
A rewrite method that first creates a private Filter, by visiting each term in sequence and marking all docs for that term. Matching documents are assigned a constant score equal to the query's boost.This method is faster than the BooleanQuery rewrite methods when the number of matched terms or matched documents is non-trivial. Also, it will never hit an errant
BooleanQuery.TooManyClausesexception.- See Also:
-
SCORING_BOOLEAN_REWRITE
A rewrite method that first translates each term intoBooleanClause.Occur.SHOULDclause in a BooleanQuery, and keeps the scores as computed by the query. Note that typically such scores are meaningless to the user, and require non-trivial CPU to compute, so it's almost always better to useCONSTANT_SCORE_REWRITEinstead.NOTE: This rewrite method will hit
BooleanQuery.TooManyClausesif the number of terms exceedsBooleanQuery.getMaxClauseCount().- See Also:
-
CONSTANT_SCORE_BOOLEAN_REWRITE
LikeSCORING_BOOLEAN_REWRITEexcept scores are not computed. Instead, each matching document receives a constant score equal to the query's boost.NOTE: This rewrite method will hit
BooleanQuery.TooManyClausesif the number of terms exceedsBooleanQuery.getMaxClauseCount().- See Also:
-
-
Constructor Details
-
MultiTermQuery
Constructs a query matching terms that cannot be represented with a single Term.
-
-
Method Details
-
getField
Returns the field name for this query -
getTermsEnum
Construct the enumeration to be used, expanding the pattern term. This method should only be called if the field exists (ie, implementations can assume the field does exist). This method should not return null (should instead returnTermsEnum.EMPTYif no terms match). The TermsEnum must already be positioned to the first matching term. The givenAttributeSourceis passed by theMultiTermQuery.RewriteMethodto share information between segments, for exampleTopTermsRewriteuses it to share maximum competitive boosts- Throws:
IOException
-
getTermsEnum
Constructs an enumeration that expands the pattern term. This method should only be called if the field exists (ie, implementations can assume the field does exist). This method never returns null. The returned TermsEnum is positioned to the first matching term.- Throws:
IOException
-
rewrite
To rewrite to a simpler form, instead return a simpler enum fromgetTermsEnum(Terms, AttributeSource). For example, to rewrite to a single term, return aSingleTermsEnum- Overrides:
rewritein classQuery- Throws:
IOException
-
getRewriteMethod
- See Also:
-
setRewriteMethod
Sets the rewrite method to be used when executing the query. You can use one of the four core methods, or implement your own subclass ofMultiTermQuery.RewriteMethod. -
hashCode
public int hashCode()Description copied from class:QueryOverride and implement query hash code properly in a subclass. This is required so thatQueryCacheworks properly. -
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 that other instance. Utility methods are provided for certain repetitive code. -
equalsTo
-