public abstract class MultiTermQuery extends Query
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.
Modifier and Type | Class and Description |
---|---|
static class |
MultiTermQuery.RewriteMethod
Abstract class that defines how the query is rewritten.
|
static class |
MultiTermQuery.TopTermsBlendedFreqScoringRewrite
A rewrite method that first translates each term into
BooleanClause.Occur.SHOULD clause 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 class |
MultiTermQuery.TopTermsBoostOnlyBooleanQueryRewrite
A rewrite method that first translates each term into
BooleanClause.Occur.SHOULD clause in a BooleanQuery, but the scores
are only computed as the boost. |
static class |
MultiTermQuery.TopTermsScoringBooleanQueryRewrite
A rewrite method that first translates each term into
BooleanClause.Occur.SHOULD clause in a BooleanQuery, and keeps the
scores as computed by the query. |
Modifier and Type | Field and Description |
---|---|
static MultiTermQuery.RewriteMethod |
CONSTANT_SCORE_BOOLEAN_REWRITE
Like
SCORING_BOOLEAN_REWRITE except
scores are not computed. |
static MultiTermQuery.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.
|
protected String |
field |
protected MultiTermQuery.RewriteMethod |
rewriteMethod |
static MultiTermQuery.RewriteMethod |
SCORING_BOOLEAN_REWRITE
A rewrite method that first translates each term into
BooleanClause.Occur.SHOULD clause in a
BooleanQuery, and keeps the scores as computed by the
query. |
Constructor and Description |
---|
MultiTermQuery(String field)
Constructs a query matching terms that cannot be represented with a single
Term.
|
Modifier and Type | Method and Description |
---|---|
boolean |
equals(Object other)
Override and implement query instance equivalence properly in a subclass.
|
String |
getField()
Returns the field name for this query
|
MultiTermQuery.RewriteMethod |
getRewriteMethod() |
protected TermsEnum |
getTermsEnum(Terms terms)
Convenience method, if no attributes are needed:
This simply passes empty attributes and is equal to:
getTermsEnum(terms, new AttributeSource()) |
protected abstract TermsEnum |
getTermsEnum(Terms terms,
AttributeSource atts)
Construct the enumeration to be used, expanding the
pattern term.
|
int |
hashCode()
Override and implement query hash code properly in a subclass.
|
Query |
rewrite(IndexReader reader)
To rewrite to a simpler form, instead return a simpler
enum from
getTermsEnum(Terms, AttributeSource) . |
void |
setRewriteMethod(MultiTermQuery.RewriteMethod method)
Sets the rewrite method to be used when executing the
query.
|
classHash, createWeight, sameClassAs, toString, toString, visit
protected final String field
protected MultiTermQuery.RewriteMethod rewriteMethod
public static final MultiTermQuery.RewriteMethod CONSTANT_SCORE_REWRITE
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.TooManyClauses
exception.
public static final MultiTermQuery.RewriteMethod SCORING_BOOLEAN_REWRITE
BooleanClause.Occur.SHOULD
clause 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 use CONSTANT_SCORE_REWRITE
instead.
NOTE: This rewrite method will hit BooleanQuery.TooManyClauses
if the number of terms
exceeds BooleanQuery.getMaxClauseCount()
.
public static final MultiTermQuery.RewriteMethod CONSTANT_SCORE_BOOLEAN_REWRITE
SCORING_BOOLEAN_REWRITE
except
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.TooManyClauses
if the number of terms
exceeds BooleanQuery.getMaxClauseCount()
.
public MultiTermQuery(String field)
public final String getField()
protected abstract TermsEnum getTermsEnum(Terms terms, AttributeSource atts) throws IOException
TermsEnum.EMPTY
if no
terms match). The TermsEnum must already be
positioned to the first matching term.
The given AttributeSource
is passed by the MultiTermQuery.RewriteMethod
to
provide attributes, the rewrite method uses to inform about e.g. maximum competitive boosts.
This is currently only used by TopTermsRewrite
IOException
protected final TermsEnum getTermsEnum(Terms terms) throws IOException
getTermsEnum(terms, new AttributeSource())
IOException
public final Query rewrite(IndexReader reader) throws IOException
getTermsEnum(Terms, AttributeSource)
. For example,
to rewrite to a single term, return a SingleTermsEnum
rewrite
in class Query
IOException
public MultiTermQuery.RewriteMethod getRewriteMethod()
public void setRewriteMethod(MultiTermQuery.RewriteMethod method)
MultiTermQuery.RewriteMethod
.public int hashCode()
Query
QueryCache
works properly.hashCode
in class Query
Query.equals(Object)
public boolean equals(Object other)
Query
QueryCache
works 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.equals
in class Query
Query.sameClassAs(Object)
,
Query.classHash()
Copyright © 2000-2019 Apache Software Foundation. All Rights Reserved.