01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */package org.apache.solr.search;
17:
18: import org.apache.lucene.index.IndexReader;
19: import org.apache.lucene.index.Term;
20: import org.apache.lucene.search.Query;
21: import org.apache.lucene.search.ConstantScoreQuery;
22:
23: import java.io.IOException;
24:
25: /**
26: * @author yonik
27: * @version $Id: ConstantScorePrefixQuery.java 472574 2006-11-08 18:25:52Z yonik $
28: */
29: public class ConstantScorePrefixQuery extends Query {
30: private final Term prefix;
31:
32: public ConstantScorePrefixQuery(Term prefix) {
33: this .prefix = prefix;
34: }
35:
36: /** Returns the prefix for this query */
37: public Term getPrefix() {
38: return prefix;
39: }
40:
41: public Query rewrite(IndexReader reader) throws IOException {
42: // TODO: if number of terms are low enough, rewrite to a BooleanQuery
43: // for potentially faster execution.
44: // TODO: cache the bitset somewhere instead of regenerating it
45: Query q = new ConstantScoreQuery(new PrefixFilter(prefix));
46: q.setBoost(getBoost());
47: return q;
48: }
49:
50: /** Prints a user-readable version of this query. */
51: public String toString(String field) {
52: StringBuffer buffer = new StringBuffer();
53: if (!prefix.field().equals(field)) {
54: buffer.append(prefix.field());
55: buffer.append(":");
56: }
57: buffer.append(prefix.text());
58: buffer.append('*');
59: if (getBoost() != 1.0f) {
60: buffer.append("^");
61: buffer.append(Float.toString(getBoost()));
62: }
63: return buffer.toString();
64: }
65:
66: /** Returns true if <code>o</code> is equal to this. */
67: public boolean equals(Object o) {
68: if (this == o)
69: return true;
70: if (!(o instanceof ConstantScorePrefixQuery))
71: return false;
72: ConstantScorePrefixQuery other = (ConstantScorePrefixQuery) o;
73: return this .prefix.equals(other.prefix)
74: && this .getBoost() == other.getBoost();
75: }
76:
77: /** Returns a hash code value for this object.*/
78: public int hashCode() {
79: int h = prefix.hashCode() ^ Float.floatToIntBits(getBoost());
80: h ^= (h << 14) | (h >>> 19); // reversible (1 to 1) transformation unique to ConstantScorePrefixQuery
81: return h;
82: }
83:
84: }
|