01: package org.apache.lucene.search;
02:
03: /**
04: * Licensed to the Apache Software Foundation (ASF) under one or more
05: * contributor license agreements. See the NOTICE file distributed with
06: * this work for additional information regarding copyright ownership.
07: * The ASF licenses this file to You under the Apache License, Version 2.0
08: * (the "License"); you may not use this file except in compliance with
09: * the License. You may obtain a copy of the License at
10: *
11: * http://www.apache.org/licenses/LICENSE-2.0
12: *
13: * Unless required by applicable law or agreed to in writing, software
14: * distributed under the License is distributed on an "AS IS" BASIS,
15: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16: * See the License for the specific language governing permissions and
17: * limitations under the License.
18: */
19:
20: import java.io.IOException;
21:
22: import org.apache.lucene.index.Term;
23: import org.apache.lucene.index.TermEnum;
24: import org.apache.lucene.index.IndexReader;
25: import org.apache.lucene.util.ToStringUtils;
26:
27: /** A Query that matches documents containing terms with a specified prefix. A PrefixQuery
28: * is built by QueryParser for input like <code>app*</code>. */
29: public class PrefixQuery extends Query {
30: private Term prefix;
31:
32: /** Constructs a query for terms starting with <code>prefix</code>. */
33: public PrefixQuery(Term prefix) {
34: this .prefix = prefix;
35: }
36:
37: /** Returns the prefix of this query. */
38: public Term getPrefix() {
39: return prefix;
40: }
41:
42: public Query rewrite(IndexReader reader) throws IOException {
43: BooleanQuery query = new BooleanQuery(true);
44: TermEnum enumerator = reader.terms(prefix);
45: try {
46: String prefixText = prefix.text();
47: String prefixField = prefix.field();
48: do {
49: Term term = enumerator.term();
50: if (term != null && term.text().startsWith(prefixText)
51: && term.field() == prefixField) // interned comparison
52: {
53: TermQuery tq = new TermQuery(term); // found a match
54: tq.setBoost(getBoost()); // set the boost
55: query.add(tq, BooleanClause.Occur.SHOULD); // add to query
56: //System.out.println("added " + term);
57: } else {
58: break;
59: }
60: } while (enumerator.next());
61: } finally {
62: enumerator.close();
63: }
64: return query;
65: }
66:
67: /** Prints a user-readable version of this query. */
68: public String toString(String field) {
69: StringBuffer buffer = new StringBuffer();
70: if (!prefix.field().equals(field)) {
71: buffer.append(prefix.field());
72: buffer.append(":");
73: }
74: buffer.append(prefix.text());
75: buffer.append('*');
76: buffer.append(ToStringUtils.boost(getBoost()));
77: return buffer.toString();
78: }
79:
80: /** Returns true iff <code>o</code> is equal to this. */
81: public boolean equals(Object o) {
82: if (!(o instanceof PrefixQuery))
83: return false;
84: PrefixQuery other = (PrefixQuery) o;
85: return (this .getBoost() == other.getBoost())
86: && this .prefix.equals(other.prefix);
87: }
88:
89: /** Returns a hash code value for this object.*/
90: public int hashCode() {
91: return Float.floatToIntBits(getBoost()) ^ prefix.hashCode()
92: ^ 0x6634D93C;
93: }
94: }
|