KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > search > PrefixQuery


1 package org.apache.lucene.search;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.io.IOException JavaDoc;
20
21 import org.apache.lucene.index.Term;
22 import org.apache.lucene.index.TermEnum;
23 import org.apache.lucene.index.IndexReader;
24 import org.apache.lucene.util.ToStringUtils;
25
26 /** A Query that matches documents containing terms with a specified prefix. A PrefixQuery
27  * is built by QueryParser for input like <code>app*</code>. */

28 public class PrefixQuery extends Query {
29   private Term prefix;
30
31   /** Constructs a query for terms starting with <code>prefix</code>. */
32   public PrefixQuery(Term prefix) {
33     this.prefix = prefix;
34   }
35
36   /** Returns the prefix of this query. */
37   public Term getPrefix() { return prefix; }
38
39   public Query rewrite(IndexReader reader) throws IOException JavaDoc {
40     BooleanQuery query = new BooleanQuery(true);
41     TermEnum enumerator = reader.terms(prefix);
42     try {
43       String JavaDoc prefixText = prefix.text();
44       String JavaDoc prefixField = prefix.field();
45       do {
46         Term term = enumerator.term();
47         if (term != null &&
48             term.text().startsWith(prefixText) &&
49             term.field() == prefixField) {
50           TermQuery tq = new TermQuery(term); // found a match
51
tq.setBoost(getBoost()); // set the boost
52
query.add(tq, BooleanClause.Occur.SHOULD); // add to query
53
//System.out.println("added " + term);
54
} else {
55           break;
56         }
57       } while (enumerator.next());
58     } finally {
59       enumerator.close();
60     }
61     return query;
62   }
63
64   /** Prints a user-readable version of this query. */
65   public String JavaDoc toString(String JavaDoc field) {
66     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
67     if (!prefix.field().equals(field)) {
68       buffer.append(prefix.field());
69       buffer.append(":");
70     }
71     buffer.append(prefix.text());
72     buffer.append('*');
73     buffer.append(ToStringUtils.boost(getBoost()));
74     return buffer.toString();
75   }
76
77   /** Returns true iff <code>o</code> is equal to this. */
78   public boolean equals(Object JavaDoc o) {
79     if (!(o instanceof PrefixQuery))
80       return false;
81     PrefixQuery other = (PrefixQuery)o;
82     return (this.getBoost() == other.getBoost())
83       && this.prefix.equals(other.prefix);
84   }
85
86   /** Returns a hash code value for this object.*/
87   public int hashCode() {
88     return Float.floatToIntBits(getBoost()) ^ prefix.hashCode() ^ 0x6634D93C;
89   }
90 }
91
Popular Tags