Class PhraseQuery

  • All Implemented Interfaces:
    java.lang.Cloneable
    Direct Known Subclasses:
    NGramPhraseQuery

    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.

    • Constructor Summary

      Constructors 
      Constructor Description
      PhraseQuery()
      Constructs an empty phrase query.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(Term term)
      Adds a term to the end of the query phrase.
      void add​(Term term, int position)
      Adds a term to the end of the query phrase.
      Weight createWeight​(IndexSearcher searcher)
      Expert: Constructs an appropriate Weight implementation for this query.
      boolean equals​(java.lang.Object o)
      Returns true iff o is equal to this.
      void extractTerms​(java.util.Set<Term> queryTerms)
      Expert: adds all terms occurring in this query to the terms set.
      int[] getPositions()
      Returns the relative positions of terms in this phrase.
      int getSlop()
      Returns the slop.
      Term[] getTerms()
      Returns the set 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)
      Sets the number of other words permitted between words in query phrase.
      java.lang.String toString​(java.lang.String f)
      Prints a user-readable version of this query.
      • Methods inherited from class java.lang.Object

        getClass, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • PhraseQuery

        public PhraseQuery()
        Constructs an empty phrase query.
    • Method Detail

      • setSlop

        public void setSlop​(int s)
        Sets the number of other words permitted between words in query phrase. If zero, then this is an exact phrase search. For larger values this works like a WITHIN or NEAR operator.

        The slop is in fact an edit-distance, where the units correspond to moves of terms in the query phrase out of position. For example, to switch the order of two words requires two moves (the first move places the words atop one another), so to permit re-orderings of phrases, the slop must be at least two.

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

        The slop is zero by default, requiring exact matches.

      • getSlop

        public int getSlop()
        Returns the slop. See setSlop().
      • add

        public void add​(Term 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

        public void add​(Term term,
                        int position)
        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).
      • getTerms

        public Term[] getTerms()
        Returns the set 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 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
      • createWeight

        public Weight createWeight​(IndexSearcher searcher)
                            throws java.io.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
        Throws:
        java.io.IOException
      • extractTerms

        public void extractTerms​(java.util.Set<Term> queryTerms)
        Description copied from class: Query
        Expert: adds all terms occurring in this query to the terms set. Only works if this query is in its rewritten form.
        Overrides:
        extractTerms in class Query
        See Also:
        Query.extractTerms(Set)
      • toString

        public java.lang.String toString​(java.lang.String f)
        Prints a user-readable version of this query.
        Specified by:
        toString in class Query
      • equals

        public boolean equals​(java.lang.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