FileDocCategorySizeDatePackage
PrefixQuery.javaAPI DocApache Lucene 1.4.32540Tue Mar 30 00:48:04 BST 2004org.apache.lucene.search

PrefixQuery

public class PrefixQuery extends Query
A Query that matches documents containing terms with a specified prefix.

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 org.apache.lucene.search.Querycombine(org.apache.lucene.search.Query[] queries)

    return Query.mergeBooleanQueries(queries);
  
public org.apache.lucene.index.TermgetPrefix()
Returns the prefix of this query.

 return prefix; 
public org.apache.lucene.search.Queryrewrite(org.apache.lucene.index.IndexReader reader)

    BooleanQuery query = new BooleanQuery();
    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) {
          TermQuery tq = new TermQuery(term);	  // found a match
          tq.setBoost(getBoost());                // set the boost
          query.add(tq, false, false);		  // 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('*");
    if (getBoost() != 1.0f) {
      buffer.append("^");
      buffer.append(Float.toString(getBoost()));
    }
    return buffer.toString();