Class MultiTermQuery
- java.lang.Object
-
- org.apache.lucene.search.Query
-
- org.apache.lucene.search.MultiTermQuery
-
- All Implemented Interfaces:
Serializable,Cloneable
- Direct Known Subclasses:
FuzzyQuery,NumericRangeQuery,PrefixQuery,TermRangeQuery,WildcardQuery
public abstract class MultiTermQuery extends Query
An abstractQuerythat matches documents containing a subset of terms provided by aFilteredTermEnumenumeration.This query cannot be used directly; you must subclass it and define
getEnum(org.apache.lucene.index.IndexReader)to provide aFilteredTermEnumthat iterates through the terms to be matched.NOTE: if
setRewriteMethod(org.apache.lucene.search.MultiTermQuery.RewriteMethod)is eitherCONSTANT_SCORE_BOOLEAN_QUERY_REWRITEorSCORING_BOOLEAN_QUERY_REWRITE, you may encounter aBooleanQuery.TooManyClausesexception during searching, which happens when the number of terms to be searched exceedsBooleanQuery.getMaxClauseCount(). SettingsetRewriteMethod(org.apache.lucene.search.MultiTermQuery.RewriteMethod)toCONSTANT_SCORE_FILTER_REWRITEprevents this.The recommended rewrite method is
CONSTANT_SCORE_AUTO_REWRITE_DEFAULT: it doesn't spend CPU computing unhelpful scores, and it tries to pick the most performant rewrite method given the query. If you need scoring (likeFuzzyQuery, useMultiTermQuery.TopTermsScoringBooleanQueryRewritewhich uses a priority queue to only collect competitive terms and not hit this limitation. Note thatQueryParserproduces MultiTermQueries usingCONSTANT_SCORE_AUTO_REWRITE_DEFAULTby default.- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classMultiTermQuery.ConstantScoreAutoRewriteA rewrite method that tries to pick the best constant-score rewrite method based on term and document counts from the query.static classMultiTermQuery.RewriteMethodAbstract class that defines how the query is rewritten.static classMultiTermQuery.TopTermsBoostOnlyBooleanQueryRewriteA rewrite method that first translates each term intoBooleanClause.Occur.SHOULDclause in a BooleanQuery, but the scores are only computed as the boost.static classMultiTermQuery.TopTermsScoringBooleanQueryRewriteA rewrite method that first translates each term intoBooleanClause.Occur.SHOULDclause in a BooleanQuery, and keeps the scores as computed by the query.
-
Field Summary
Fields Modifier and Type Field Description static MultiTermQuery.RewriteMethodCONSTANT_SCORE_AUTO_REWRITE_DEFAULTRead-only default instance ofMultiTermQuery.ConstantScoreAutoRewrite, withConstantScoreAutoRewrite.setTermCountCutoff(int)set toConstantScoreAutoRewrite.DEFAULT_TERM_COUNT_CUTOFFandConstantScoreAutoRewrite.setDocCountPercent(double)set toConstantScoreAutoRewrite.DEFAULT_DOC_COUNT_PERCENT.static MultiTermQuery.RewriteMethodCONSTANT_SCORE_BOOLEAN_QUERY_REWRITELikeSCORING_BOOLEAN_QUERY_REWRITEexcept scores are not computed.static MultiTermQuery.RewriteMethodCONSTANT_SCORE_FILTER_REWRITEA rewrite method that first creates a private Filter, by visiting each term in sequence and marking all docs for that term.protected MultiTermQuery.RewriteMethodrewriteMethodstatic MultiTermQuery.RewriteMethodSCORING_BOOLEAN_QUERY_REWRITEA rewrite method that first translates each term intoBooleanClause.Occur.SHOULDclause in a BooleanQuery, and keeps the scores as computed by the query.
-
Constructor Summary
Constructors Constructor Description MultiTermQuery()Constructs a query matching terms that cannot be represented with a single Term.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidclearTotalNumberOfTerms()Deprecated.Don't use this method, as its not thread safe and useless.booleanequals(Object obj)protected abstract FilteredTermEnumgetEnum(IndexReader reader)Construct the enumeration to be used, expanding the pattern term.MultiTermQuery.RewriteMethodgetRewriteMethod()intgetTotalNumberOfTerms()Deprecated.Don't use this method, as its not thread safe and useless.inthashCode()protected voidincTotalNumberOfTerms(int inc)Deprecated.Don't use this method, as its not thread safe and useless.Queryrewrite(IndexReader reader)To rewrite to a simpler form, instead return a simpler enum fromgetEnum(IndexReader).voidsetRewriteMethod(MultiTermQuery.RewriteMethod method)Sets the rewrite method to be used when executing the query.-
Methods inherited from class org.apache.lucene.search.Query
clone, combine, createWeight, extractTerms, getBoost, getSimilarity, mergeBooleanQueries, setBoost, toString, toString, weight
-
-
-
-
Field Detail
-
rewriteMethod
protected MultiTermQuery.RewriteMethod rewriteMethod
-
CONSTANT_SCORE_FILTER_REWRITE
public static final MultiTermQuery.RewriteMethod CONSTANT_SCORE_FILTER_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.
-
SCORING_BOOLEAN_QUERY_REWRITE
public static final MultiTermQuery.RewriteMethod SCORING_BOOLEAN_QUERY_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_AUTO_REWRITE_DEFAULTinstead.NOTE: This rewrite method will hit
BooleanQuery.TooManyClausesif the number of terms exceedsBooleanQuery.getMaxClauseCount().
-
CONSTANT_SCORE_BOOLEAN_QUERY_REWRITE
public static final MultiTermQuery.RewriteMethod CONSTANT_SCORE_BOOLEAN_QUERY_REWRITE
LikeSCORING_BOOLEAN_QUERY_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().
-
CONSTANT_SCORE_AUTO_REWRITE_DEFAULT
public static final MultiTermQuery.RewriteMethod CONSTANT_SCORE_AUTO_REWRITE_DEFAULT
Read-only default instance ofMultiTermQuery.ConstantScoreAutoRewrite, withConstantScoreAutoRewrite.setTermCountCutoff(int)set toConstantScoreAutoRewrite.DEFAULT_TERM_COUNT_CUTOFFandConstantScoreAutoRewrite.setDocCountPercent(double)set toConstantScoreAutoRewrite.DEFAULT_DOC_COUNT_PERCENT. Note that you cannot alter the configuration of this instance; you'll need to create a private instance instead.
-
-
Method Detail
-
getEnum
protected abstract FilteredTermEnum getEnum(IndexReader reader) throws IOException
Construct the enumeration to be used, expanding the pattern term.- Throws:
IOException
-
getTotalNumberOfTerms
@Deprecated public int getTotalNumberOfTerms()
Deprecated.Don't use this method, as its not thread safe and useless.Expert: Return the number of unique terms visited during execution of the query. If there are many of them, you may consider using another query type or reduce your total term count in index.This method is not thread safe, be sure to only call it when no query is running! If you re-use the same query instance for another search, be sure to first reset the term counter with
clearTotalNumberOfTerms().On single-segment indexes / no MultiReaders, you get the correct number of unique terms for the whole index. Use this number to compare different queries. For multi-segment indexes this number can also be achieved in non-constant-score mode. In constant-score mode you get the total number of terms seeked for all segments / sub-readers.
- See Also:
clearTotalNumberOfTerms()
-
clearTotalNumberOfTerms
@Deprecated public void clearTotalNumberOfTerms()
Deprecated.Don't use this method, as its not thread safe and useless.Expert: Resets the counting of unique terms. Do this before executing the query/filter.- See Also:
getTotalNumberOfTerms()
-
incTotalNumberOfTerms
@Deprecated protected void incTotalNumberOfTerms(int inc)
Deprecated.Don't use this method, as its not thread safe and useless.
-
rewrite
public final Query rewrite(IndexReader reader) throws IOException
To rewrite to a simpler form, instead return a simpler enum fromgetEnum(IndexReader). For example, to rewrite to a single term, return aSingleTermEnum- Overrides:
rewritein classQuery- Throws:
IOException
-
getRewriteMethod
public MultiTermQuery.RewriteMethod getRewriteMethod()
-
setRewriteMethod
public void setRewriteMethod(MultiTermQuery.RewriteMethod method)
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.
-
-