FileDocCategorySizeDatePackage
PrefixQuery.javaAPI DocApache Lucene 2.2.03193Sat Jun 16 22:20:34 BST 2007org.apache.lucene.search

PrefixQuery

public class PrefixQuery extends Query
A Query that matches documents containing terms with a specified prefix. A PrefixQuery is built by QueryParser for input like app*.

Fields Summary
private Term
prefix
Constructors Summary
public PrefixQuery(Term prefix)
Constructs a query for terms starting with prefix.

    this.prefix = prefix;
  
Methods Summary
public booleanequals(java.lang.Object o)
Returns true iff o is equal to this.

    if (!(o instanceof PrefixQuery))
      return false;
    PrefixQuery other = (PrefixQuery)o;
    return (this.getBoost() == other.getBoost())
      && this.prefix.equals(other.prefix);
  
public org.apache.lucene.index.TermgetPrefix()
Returns the prefix of this query.

 return prefix; 
public inthashCode()
Returns a hash code value for this object.

    return Float.floatToIntBits(getBoost()) ^ prefix.hashCode() ^ 0x6634D93C;
  
public org.apache.lucene.search.Queryrewrite(org.apache.lucene.index.IndexReader reader)

    BooleanQuery query = new BooleanQuery(true);
    TermEnum enumerator = reader.terms(prefix);
    try {
      String prefixText = prefix.text();
      String prefixField = prefix.field();
      do {
        Term term = enumerator.term();
        if (term != null &&
            term.text().startsWith(prefixText) &&
            term.field() == prefixField) // interned comparison 
        {
          TermQuery tq = new TermQuery(term);	  // found a match
          tq.setBoost(getBoost());                // set the boost
          query.add(tq, BooleanClause.Occur.SHOULD);		  // add to query
          //System.out.println("added " + term);
        } else {
          break;
        }
      } while (enumerator.next());
    } finally {
      enumerator.close();
    }
    return query;
  
public java.lang.StringtoString(java.lang.String field)
Prints a user-readable version of this query.

    StringBuffer buffer = new StringBuffer();
    if (!prefix.field().equals(field)) {
      buffer.append(prefix.field());
      buffer.append(":");
    }
    buffer.append(prefix.text());
    buffer.append('*");
    buffer.append(ToStringUtils.boost(getBoost()));
    return buffer.toString();