FileDocCategorySizeDatePackage
PhrasePrefixQuery.javaAPI DocApache Lucene 1.98515Mon Feb 20 09:20:04 GMT 2006org.apache.lucene.search

PhrasePrefixQuery

public class PhrasePrefixQuery extends Query
PhrasePrefixQuery is a generalized version of PhraseQuery, with an added method {@link #add(Term[])}. To use this class, to search for the phrase "Microsoft app*" first use add(Term) on the term "Microsoft", then find all terms that has "app" as prefix using IndexReader.terms(Term), and use PhrasePrefixQuery.add(Term[] terms) to add them to the query.
deprecated
use {@link org.apache.lucene.search.MultiPhraseQuery} instead
author
Anders Nielsen
version
1.0

Fields Summary
private String
field
private ArrayList
termArrays
private Vector
positions
private int
slop
Constructors Summary
Methods Summary
public voidadd(org.apache.lucene.index.Term term)
Add a single term at the next position in the phrase.

see
PhraseQuery#add(Term)

 add(new Term[]{term}); 
public voidadd(org.apache.lucene.index.Term[] terms)
Add multiple terms at the next position in the phrase. Any of the terms may match.

see
PhraseQuery#add(Term)

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

    add(terms, position);
  
public voidadd(org.apache.lucene.index.Term[] terms, int position)
Allows to specify the relative position of terms within the phrase.

see
PhraseQuery#add(Term, int)
param
terms
param
position

    if (termArrays.size() == 0)
      field = terms[0].field();

    for (int i = 0; i < terms.length; i++) {
      if (terms[i].field() != field) {
        throw new IllegalArgumentException(
            "All phrase terms must be in the same field (" + field + "): "
                + terms[i]);
      }
    }

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

    if (termArrays.size() == 1) {                 // optimize one-term case
      Term[] terms = (Term[])termArrays.get(0);
      BooleanQuery boq = new BooleanQuery(true);
      for (int i=0; i<terms.length; i++) {
        boq.add(new TermQuery(terms[i]), BooleanClause.Occur.SHOULD);
      }
      boq.setBoost(getBoost());
      return boq.createWeight(searcher);
    }
    return new PhrasePrefixWeight(searcher);
  
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()
Sets the phrase slop for this query.

see
PhraseQuery#getSlop()

 return slop; 
public voidsetSlop(int s)
Sets the phrase slop for this query.

see
PhraseQuery#setSlop(int)


              
       slop = s; 
public final 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("\"");
    Iterator i = termArrays.iterator();
    while (i.hasNext()) {
      Term[] terms = (Term[])i.next();
      buffer.append(terms[0].text() + (terms.length > 1 ? "*" : ""));
      if (i.hasNext())
        buffer.append(" ");
    }
    buffer.append("\"");

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

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

    return buffer.toString();