org.apache.lucene.search

## Class MultiPhraseQuery

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

- All Implemented Interfaces:
    Cloneable

      public class MultiPhraseQuery
      extends Query

  A generalized version of `PhraseQuery`, with an added method `add(Term[])` for adding more than one term at the same position that are treated as a disjunction (OR). To use this class to search for the phrase "Microsoft app*" first use `add(Term)` on the term "microsoft" (assuming lowercase analysis), then find all terms that have "app" as prefix using `LeafReader.terms(String)`, seeking to "app" then iterating and collecting terms until there is no longer that prefix, and finally use `add(Term[])` to add them to the query.

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `MultiPhraseQuery()`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `add(Term term)` Add a single term at the next position in the phrase.
    `void` | `add(Term[] terms)` Add multiple terms at the next position in the phrase.
    `void` | `add(Term[] terms, int position)` Allows to specify the relative position of terms within the phrase.
    `Weight` | `createWeight(IndexSearcher searcher, boolean needsScores)` Expert: Constructs an appropriate Weight implementation for this query.
    `boolean` | `equals(Object o)` Returns true if `o` is equal to this.
    `int[]` | `getPositions()` Returns the relative positions of terms in this phrase.
    `int` | `getSlop()` Sets the phrase slop for this query.
    `List<Term[]>` | `getTermArrays()` Returns a List of the terms in the multi-phrase.
    `int` | `hashCode()` Returns a hash code value for this object.
    `Query` | `rewrite(IndexReader reader)` Expert: called to re-write queries into primitive queries.
    `void` | `setSlop(int s)` Sets the phrase slop for this query.
    `String` | `toString(String f)` Prints a user-readable version of this query.

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

      `clone, getBoost, setBoost, toString`

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

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

- - ### Constructor Detail

    - #### MultiPhraseQuery

          public MultiPhraseQuery()

  - ### Method Detail

    - #### setSlop

          public void setSlop(int s)

      Sets the phrase slop for this query.
      See Also:
        `PhraseQuery.getSlop()`

    - #### getSlop

          public int getSlop()

      Sets the phrase slop for this query.
      See Also:
        `PhraseQuery.getSlop()`

    - #### add

          public void add(Term term)

      Add a single term at the next position in the phrase.

    - #### add

          public void add(Term[] terms)

      Add multiple terms at the next position in the phrase. Any of the terms may match (a disjunction). The array is not copied or mutated, the caller should consider it immutable subsequent to calling this method.

    - #### add

          public void add(Term[] terms,
                 int position)

      Allows to specify the relative position of terms within the phrase. The array is not copied or mutated, the caller should consider it immutable subsequent to calling this method.

    - #### getTermArrays

          public List<Term[]> getTermArrays()

      Returns a List of the terms in the multi-phrase. Do not modify the List or its contents.

    - #### getPositions

          public int[] getPositions()

      Returns the relative positions of terms in this phrase.

    - #### rewrite

          public Query rewrite(IndexReader reader)
                        throws 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:
        `IOException`

    - #### createWeight

          public Weight createWeight(IndexSearcher searcher,
                            boolean needsScores)
                              throws IOException

      Description copied from class: `Query`
      Expert: Constructs an appropriate Weight implementation for this query.

      Only implemented by primitive queries, which re-write to themselves.

      Overrides:
        `createWeight` in class `Query`
        `needsScores` - True if document scores (`Scorer.score()`) or match frequencies (`Scorer.freq()`) are needed.
      Throws:
        `IOException`

    - #### toString

          public final String toString(String f)

      Prints a user-readable version of this query.
      Specified by:
        `toString` in class `Query`

    - #### equals

          public boolean equals(Object o)

      Returns true if `o` is equal to this.
      Overrides:
        `equals` in class `Query`

    - #### hashCode

          public int hashCode()

      Returns a hash code value for this object.
      Overrides:
        `hashCode` in class `Query`

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