org.apache.lucene.search

## Class PhraseQuery

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

- All Implemented Interfaces:
    Cloneable

      public class PhraseQuery
      extends Query

  A Query that matches documents containing a particular sequence of terms. A PhraseQuery is built by QueryParser for input like `"new york"`.

  This query may be combined with other terms or queries with a `BooleanQuery`.

  NOTE: All terms in the phrase must match, even those at the same position. If you have terms at the same position, perhaps synonyms, you probably want `MultiPhraseQuery` instead which only requires one term at a position to match.
  Also, Leading holes don't have any particular meaning for this query and will be ignored. For instance this query:

       PhraseQuery.Builder builder = new PhraseQuery.Builder();
       builder.add(new Term("body", "one"), 4);
       builder.add(new Term("body", "two"), 5);
       PhraseQuery pq = builder.build();

  is equivalent to the below query:

       PhraseQuery.Builder builder = new PhraseQuery.Builder();
       builder.add(new Term("body", "one"), 0);
       builder.add(new Term("body", "two"), 1);
       PhraseQuery pq = builder.build();

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `static class` | `PhraseQuery.Builder` A builder for phrase queries.

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `PhraseQuery()` Deprecated. Use the `PhraseQuery.Builder` class to build phrase queries.
    `PhraseQuery(int slop, String field, BytesRef... terms)` Create a phrase query which will match documents that contain the given list of terms at consecutive positions in `field`, and at a maximum edit distance of `slop`.
    `PhraseQuery(int slop, String field, String... terms)` Create a phrase query which will match documents that contain the given list of terms at consecutive positions in `field`, and at a maximum edit distance of `slop`.
    `PhraseQuery(String field, BytesRef... terms)` Create a phrase query which will match documents that contain the given list of terms at consecutive positions in `field`.
    `PhraseQuery(String field, String... terms)` Create a phrase query which will match documents that contain the given list of terms at consecutive positions in `field`.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `add(Term term)` Deprecated. Phrase queries should be created once with `PhraseQuery.Builder` and then considered immutable. See `PhraseQuery.Builder.add(Term)`.
    `void` | `add(Term term, int position)` Deprecated. Phrase queries should be created once with `PhraseQuery.Builder` and then considered immutable. See `PhraseQuery.Builder.add(Term, int)`.
    `Weight` | `createWeight(IndexSearcher searcher, boolean needsScores)` Expert: Constructs an appropriate Weight implementation for this query.
    `boolean` | `equals(Object o)` Returns true iff `o` is equal to this.
    `int[]` | `getPositions()` Returns the relative positions of terms in this phrase.
    `int` | `getSlop()` Return the slop for this `PhraseQuery`.
    `Term[]` | `getTerms()` Returns the list of terms in this 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)` Deprecated. Phrase queries should be created once with `PhraseQuery.Builder` and then considered immutable. See `PhraseQuery.Builder.setSlop(int)`.
    `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

    - #### PhraseQuery

          public PhraseQuery(int slop,
                     String field,
                     String... terms)

      Create a phrase query which will match documents that contain the given list of terms at consecutive positions in `field`, and at a maximum edit distance of `slop`. For more complicated use-cases, use `PhraseQuery.Builder`.
      See Also:
        `getSlop()`

    - #### PhraseQuery

          public PhraseQuery(String field,
                     String... terms)

      Create a phrase query which will match documents that contain the given list of terms at consecutive positions in `field`.

    - #### PhraseQuery

          public PhraseQuery(int slop,
                     String field,
                     BytesRef... terms)

      Create a phrase query which will match documents that contain the given list of terms at consecutive positions in `field`, and at a maximum edit distance of `slop`. For more complicated use-cases, use `PhraseQuery.Builder`.
      See Also:
        `getSlop()`

    - #### PhraseQuery

          public PhraseQuery(String field,
                     BytesRef... terms)

      Create a phrase query which will match documents that contain the given list of terms at consecutive positions in `field`.

    - #### PhraseQuery

          @Deprecated
          public PhraseQuery()

      Deprecated. Use the `PhraseQuery.Builder` class to build phrase queries.
      Constructs an empty phrase query.

  - ### Method Detail

    - #### getSlop

          public int getSlop()

      Return the slop for this `PhraseQuery`.

      The slop is an edit distance between respective positions of terms as defined in this `PhraseQuery` and the positions of terms in a document.

      For instance, when searching for `"quick fox"`, it is expected that the difference between the positions of `fox` and `quick` is 1. So `"a quick brown fox"` would be at an edit distance of 1 since the difference of the positions of `fox` and `quick` is 2. Similarly, `"the fox is quick"` would be at an edit distance of 3 since the difference of the positions of `fox` and `quick` is -2. The slop defines the maximum edit distance for a document to match.

      More exact matches are scored higher than sloppier matches, thus search results are sorted by exactness.

    - #### getTerms

          public Term[] getTerms()

      Returns the list of terms in this phrase.

    - #### 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 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 iff `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`

    - #### setSlop

          @Deprecated
          public void setSlop(int s)

      Deprecated. Phrase queries should be created once with `PhraseQuery.Builder` and then considered immutable. See `PhraseQuery.Builder.setSlop(int)`.
      Set the slop.
      See Also:
        `getSlop()`

    - #### add

          @Deprecated
          public void add(Term term)

      Deprecated. Phrase queries should be created once with `PhraseQuery.Builder` and then considered immutable. See `PhraseQuery.Builder.add(Term)`.
      Adds a term to the end of the query phrase. The relative position of the term is the one immediately after the last term added.

    - #### add

          @Deprecated
          public void add(Term term,
                            int position)

      Deprecated. Phrase queries should be created once with `PhraseQuery.Builder` and then considered immutable. See `PhraseQuery.Builder.add(Term, int)`.
      Adds a term to the end of the query phrase. The relative position of the term within the phrase is specified explicitly. This allows e.g. phrases with more than one term at the same position or phrases with gaps (e.g. in connection with stopwords).

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