FileDocCategorySizeDatePackage
PhraseQuery.javaAPI DocApache Lucene 2.1.09782Wed Feb 14 10:46:40 GMT 2007org.apache.lucene.search

PhraseQuery

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 {@link BooleanQuery}.

Fields Summary
private String
field
private Vector
terms
private Vector
positions
private int
slop
Constructors Summary
public PhraseQuery()
Constructs an empty phrase query.


        
    
Methods Summary
public voidadd(org.apache.lucene.index.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.

    int position = 0;
    if(positions.size() > 0)
        position = ((Integer) positions.lastElement()).intValue() + 1;

    add(term, position);
  
public voidadd(org.apache.lucene.index.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).

param
term
param
position

      if (terms.size() == 0)
          field = term.field();
      else if (term.field() != field)
          throw new IllegalArgumentException("All phrase terms must be in the same field: " + term);

      terms.addElement(term);
      positions.addElement(new Integer(position));
  
protected org.apache.lucene.search.WeightcreateWeight(org.apache.lucene.search.Searcher searcher)

    if (terms.size() == 1) {			  // optimize one-term case
      Term term = (Term)terms.elementAt(0);
      Query termQuery = new TermQuery(term);
      termQuery.setBoost(getBoost());
      return termQuery.createWeight(searcher);
    }
    return new PhraseWeight(searcher);
  
public booleanequals(java.lang.Object o)
Returns true iff o is equal to this.

    if (!(o instanceof PhraseQuery))
      return false;
    PhraseQuery other = (PhraseQuery)o;
    return (this.getBoost() == other.getBoost())
      && (this.slop == other.slop)
      &&  this.terms.equals(other.terms)
      && this.positions.equals(other.positions);
  
public voidextractTerms(java.util.Set queryTerms)

see
org.apache.lucene.search.Query#extractTerms(java.util.Set)

    queryTerms.addAll(terms);
  
public int[]getPositions()
Returns the relative positions of terms in this phrase.

      int[] result = new int[positions.size()];
      for(int i = 0; i < positions.size(); i++)
          result[i] = ((Integer) positions.elementAt(i)).intValue();
      return result;
  
public intgetSlop()
Returns the slop. See setSlop().

 return slop; 
public org.apache.lucene.index.Term[]getTerms()
Returns the set of terms in this phrase.

    return (Term[])terms.toArray(new Term[0]);
  
public inthashCode()
Returns a hash code value for this object.

    return Float.floatToIntBits(getBoost())
      ^ slop
      ^ terms.hashCode()
      ^ positions.hashCode();
  
public voidsetSlop(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.

 slop = s; 
public java.lang.StringtoString(java.lang.String f)
Prints a user-readable version of this query.

    StringBuffer buffer = new StringBuffer();
    if (!field.equals(f)) {
      buffer.append(field);
      buffer.append(":");
    }

    buffer.append("\"");
    for (int i = 0; i < terms.size(); i++) {
      buffer.append(((Term)terms.elementAt(i)).text());
      if (i != terms.size()-1)
  buffer.append(" ");
    }
    buffer.append("\"");

    if (slop != 0) {
      buffer.append("~");
      buffer.append(slop);
    }

    buffer.append(ToStringUtils.boost(getBoost()));

    return buffer.toString();