org.apache.lucene.search

## Class MultiTermQuery

- java.lang.Object
- - org.apache.lucene.search.Query
  - - org.apache.lucene.search.MultiTermQuery

All Implemented Interfaces:
  Cloneable

Direct Known Subclasses:
  AutomatonQuery, FuzzyQuery, NumericRangeQuery, PrefixQuery, TermRangeQuery

    public abstract class MultiTermQuery
    extends Query

An abstract `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_QUERY_REWRITE` or `SCORING_BOOLEAN_QUERY_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_FILTER_REWRITE` prevents 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 (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_AUTO_REWRITE_DEFAULT` by default.

### Nested Class Summary

Nested Classes
`static class` `MultiTermQuery.ConstantScoreAutoRewrite` A rewrite method that tries to pick the best constant-score rewrite method based on term and document counts from the query.
`static class` `MultiTermQuery.RewriteMethod` Abstract class that defines how the query is rewritten.
`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.

### Field Summary

Fields
`static MultiTermQuery.RewriteMethod` `CONSTANT_SCORE_AUTO_REWRITE_DEFAULT` Read-only default instance of `MultiTermQuery.ConstantScoreAutoRewrite`, with `ConstantScoreAutoRewrite.setTermCountCutoff(int)` set to `ConstantScoreAutoRewrite.DEFAULT_TERM_COUNT_CUTOFF` and `ConstantScoreAutoRewrite.setDocCountPercent(double)` set to `ConstantScoreAutoRewrite.DEFAULT_DOC_COUNT_PERCENT`.
`static MultiTermQuery.RewriteMethod` `CONSTANT_SCORE_BOOLEAN_QUERY_REWRITE` Like `SCORING_BOOLEAN_QUERY_REWRITE` except scores are not computed.
`static 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.
`protected String` `field`
`protected MultiTermQuery.RewriteMethod` `rewriteMethod`
`static MultiTermQuery.RewriteMethod` `SCORING_BOOLEAN_QUERY_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 Summary

Constructors
Constructor and Description
`MultiTermQuery(String field)` Constructs a query matching terms that cannot be represented with a single Term.

### Method Summary

Methods
`boolean` `equals(Object obj)`
`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()`
`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.

### Methods inherited from class org.apache.lucene.search.Query

`clone, createWeight, extractTerms, getBoost, setBoost, toString, toString`

### Methods inherited from class java.lang.Object

`finalize, getClass, notify, notifyAll, wait, wait, wait`

### Field Detail

#### field

    protected final String field

#### 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.TooManyClauses` exception.

See Also:
  `setRewriteMethod(org.apache.lucene.search.MultiTermQuery.RewriteMethod)`

#### SCORING_BOOLEAN_QUERY_REWRITE

    public static final MultiTermQuery.RewriteMethod SCORING_BOOLEAN_QUERY_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. 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_AUTO_REWRITE_DEFAULT` instead.

NOTE: This rewrite method will hit `BooleanQuery.TooManyClauses` if the number of terms exceeds `BooleanQuery.getMaxClauseCount()`.

See Also:
  `setRewriteMethod(org.apache.lucene.search.MultiTermQuery.RewriteMethod)`

#### CONSTANT_SCORE_BOOLEAN_QUERY_REWRITE

    public static final MultiTermQuery.RewriteMethod CONSTANT_SCORE_BOOLEAN_QUERY_REWRITE

Like `SCORING_BOOLEAN_QUERY_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()`.

See Also:
  `setRewriteMethod(org.apache.lucene.search.MultiTermQuery.RewriteMethod)`

#### CONSTANT_SCORE_AUTO_REWRITE_DEFAULT

    public static final MultiTermQuery.RewriteMethod CONSTANT_SCORE_AUTO_REWRITE_DEFAULT

Read-only default instance of `MultiTermQuery.ConstantScoreAutoRewrite`, with `ConstantScoreAutoRewrite.setTermCountCutoff(int)` set to `ConstantScoreAutoRewrite.DEFAULT_TERM_COUNT_CUTOFF` and `ConstantScoreAutoRewrite.setDocCountPercent(double)` set to `ConstantScoreAutoRewrite.DEFAULT_DOC_COUNT_PERCENT`. Note that you cannot alter the configuration of this instance; you'll need to create a private instance instead.

### Constructor Detail

#### MultiTermQuery

    public MultiTermQuery(String field)

Constructs a query matching terms that cannot be represented with a single Term.

### Method Detail

#### getField

    public final String getField()

Returns the field name for this query

#### getTermsEnum

    protected abstract TermsEnum getTermsEnum(Terms terms,
                         AttributeSource atts)
                                       throws IOException

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 return `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`

Throws:
  `IOException`

#### getTermsEnum

    protected final TermsEnum getTermsEnum(Terms terms)
                                    throws IOException

Convenience method, if no attributes are needed: This simply passes empty attributes and is equal to: `getTermsEnum(terms, new AttributeSource())`

Throws:
  `IOException`

#### rewrite

    public final Query rewrite(IndexReader reader)
                        throws IOException

To rewrite to a simpler form, instead return a simpler enum from `getTermsEnum(Terms, AttributeSource)`. For example, to rewrite to a single term, return a `SingleTermsEnum`

Overrides:
  `rewrite` in class `Query`
Throws:
  `IOException`

#### getRewriteMethod

    public MultiTermQuery.RewriteMethod getRewriteMethod()

See Also:
  `setRewriteMethod(org.apache.lucene.search.MultiTermQuery.RewriteMethod)`

#### 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 of `MultiTermQuery.RewriteMethod`.

#### hashCode

    public int hashCode()

Overrides:
  `hashCode` in class `Query`

#### equals

    public boolean equals(Object obj)

Overrides:
  `equals` in class `Query`