Class MultiTermQuery

java.lang.Object
org.apache.lucene.search.Query
org.apache.lucene.search.MultiTermQuery
Direct Known Subclasses:
AutomatonQuery, FuzzyQuery, TermInSetQuery

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 MultiTermQuery.RewriteMethod is either CONSTANT_SCORE_BOOLEAN_REWRITE or SCORING_BOOLEAN_REWRITE, you may encounter a IndexSearcher.TooManyClauses exception during searching, which happens when the number of terms to be searched exceeds IndexSearcher.getMaxClauseCount(). Setting MultiTermQuery.RewriteMethod to CONSTANT_SCORE_BLENDED_REWRITE or CONSTANT_SCORE_REWRITE prevents this.

The recommended rewrite method is CONSTANT_SCORE_BLENDED_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.

  • Field Details

  • Constructor Details

    • MultiTermQuery

      public MultiTermQuery(String field, MultiTermQuery.RewriteMethod rewriteMethod)
      Constructs a query matching terms that cannot be represented with a single Term.
  • Method Details

    • 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 share information between segments, for example TopTermsRewrite uses it to share maximum competitive boosts
      Throws:
      IOException
    • getTermsEnum

      public final TermsEnum getTermsEnum(Terms terms) throws IOException
      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
    • getTermsCount

      public long getTermsCount() throws IOException
      Return the number of unique terms contained in this query, if known up-front. If not known, -1 will be returned.
      Throws:
      IOException
    • rewrite

      public final Query rewrite(IndexSearcher indexSearcher) 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
      See Also:
    • getRewriteMethod

      public MultiTermQuery.RewriteMethod getRewriteMethod()
    • setRewriteMethod

      @Deprecated public void setRewriteMethod(MultiTermQuery.RewriteMethod method)
      Deprecated.
      set this using a constructor instead
      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()
      Description copied from class: Query
      Override and implement query hash code properly in a subclass. This is required so that QueryCache works properly.
      Specified by:
      hashCode in class Query
      See Also:
    • equals

      public boolean equals(Object other)
      Description copied from class: Query
      Override and implement query instance equivalence properly in a subclass. This is required so that 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 to those of the other instance. Utility methods are provided for certain repetitive code.

      Specified by:
      equals in class Query
      See Also: