## org.apache.lucene.search
Class MultiTermQuery

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

All Implemented Interfaces:
  java.io.Serializable, java.lang.Cloneable

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

    public abstract class MultiTermQuery

extends Query

An abstract `Query` that matches documents containing a subset of terms provided by a `FilteredTermEnum` enumeration.

This query cannot be used directly; you must subclass it and define `getEnum(org.apache.lucene.index.IndexReader)` to provide a `FilteredTermEnum` 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. Note that `QueryParser` produces MultiTermQueries using `CONSTANT_SCORE_AUTO_REWRITE_DEFAULT` by default.

See Also:
  Serialized Form

Nested Class Summary
`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.

Field Summary

`static MultiTermQuery.RewriteMethod`

`CONSTANT_SCORE_AUTO_REWRITE_DEFAULT`
Read-only default instance of `MultiTermQuery.ConstantScoreAutoRewrite`, with `MultiTermQuery.ConstantScoreAutoRewrite.setTermCountCutoff(int)` set to `MultiTermQuery.ConstantScoreAutoRewrite.DEFAULT_TERM_COUNT_CUTOFF` and `MultiTermQuery.ConstantScoreAutoRewrite.setDocCountPercent(double)` set to `MultiTermQuery.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 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.
`protected Term` `term`

Constructor Summary
`MultiTermQuery()` Constructs a query matching terms that cannot be represented with a single Term.
`MultiTermQuery(Term term)` Deprecated. check sub class for possible term access - the Term does not make sense for all MultiTermQuerys and will be removed.

Method Summary
`void` `clearTotalNumberOfTerms()` Expert: Resets the counting of unique terms.
`boolean` `equals(java.lang.Object obj)`
`protected abstract FilteredTermEnum` `getEnum(IndexReader reader)` Construct the enumeration to be used, expanding the pattern term.
`MultiTermQuery.RewriteMethod` `getRewriteMethod()`
`Term` `getTerm()` Deprecated. check sub class for possible term access - getTerm does not make sense for all MultiTermQuerys and will be removed.
`int` `getTotalNumberOfTerms()` Expert: Return the number of unique terms visited during execution of the query.
`int` `hashCode()`
`protected void` `incTotalNumberOfTerms(int inc)`
`Query` `rewrite(IndexReader reader)` Expert: called to re-write queries into primitive queries.
`void` `setRewriteMethod(MultiTermQuery.RewriteMethod method)` Sets the rewrite method to be used when executing the query.
`java.lang.String` `toString(java.lang.String field)` Prints a query to a string, with `field` assumed to be the default field and omitted.

Methods inherited from class org.apache.lucene.search.Query
`clone, combine, createWeight, extractTerms, getBoost, getSimilarity, mergeBooleanQueries, setBoost, toString, weight`

Methods inherited from class java.lang.Object
`finalize, getClass, notify, notifyAll, wait, wait, wait`

Field Detail

### term

    protected Term term

### 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 `MultiTermQuery.ConstantScoreAutoRewrite.setTermCountCutoff(int)` set to `MultiTermQuery.ConstantScoreAutoRewrite.DEFAULT_TERM_COUNT_CUTOFF` and `MultiTermQuery.ConstantScoreAutoRewrite.setDocCountPercent(double)` set to `MultiTermQuery.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(Term term)

  Deprecated. check sub class for possible term access - the Term does not make sense for all MultiTermQuerys and will be removed.

  Constructs a query for terms matching `term`.

### MultiTermQuery

    public MultiTermQuery()

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

Method Detail

### getTerm

    public Term getTerm()

  Deprecated. check sub class for possible term access - getTerm does not make sense for all MultiTermQuerys and will be removed.

  Returns the pattern term.

### getEnum

    protected abstract FilteredTermEnum getEnum(IndexReader reader)
                                         throws java.io.IOException

  Construct the enumeration to be used, expanding the pattern term.

  Throws:
    `java.io.IOException`

### getTotalNumberOfTerms

    public int getTotalNumberOfTerms()

  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 optimize 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 optimized indexes / no MultiReaders, you get the correct number of unique terms for the whole index. Use this number to compare different queries. For non-optimized 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

    public void clearTotalNumberOfTerms()

  Expert: Resets the counting of unique terms. Do this before executing the query/filter.

  See Also:
    `getTotalNumberOfTerms()`

### incTotalNumberOfTerms

    protected void incTotalNumberOfTerms(int inc)

### rewrite

    public Query rewrite(IndexReader reader)
                  throws java.io.IOException

  Description copied from class: `Query`
  Expert: called to re-write queries into primitive queries. For example, a PrefixQuery will be rewritten into a BooleanQuery that consists of TermQuerys.

  Overrides:
    `rewrite` in class `Query`
  Throws:
    `java.io.IOException`

### toString

    public java.lang.String toString(java.lang.String field)

  Description copied from class: `Query`
  Prints a query to a string, with `field` assumed to be the default field and omitted.

  The representation used is one that is supposed to be readable by `QueryParser`. However, there are the following limitations:

  - If the query was created by the parser, the printed representation may not be exactly what was parsed. For example, characters that need to be escaped will be represented without the required backslash.
  - Some of the more complicated queries (e.g. span queries) don't have a representation that can be parsed by QueryParser.

  Specified by:
    `toString` in class `Query`

### 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(java.lang.Object obj)

  Overrides:
    `equals` in class `Query`

Copyright © 2000-2011 Apache Software Foundation. All Rights Reserved.