KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.lucene.index.IndexReader;
20
21 import java.io.IOException JavaDoc;
22
23 /**
24  * A range query that returns a constant score equal to it's boost for
25  * all documents in the range.
26  * <p>
27  * It does not have an upper bound on the number of clauses covered in the range.
28  * <p>
29  * If an endpoint is null, it is said to be "open".
30  * Either or both endpoints may be open. Open endpoints may not be exclusive
31  * (you can't select all but the first or last term without explicitly specifying the term to exclude.)
32  *
33  * @author yonik
34  * @version $Id$
35  */

36
37 public class ConstantScoreRangeQuery extends Query
38 {
39   private final String JavaDoc fieldName;
40   private final String JavaDoc lowerVal;
41   private final String JavaDoc upperVal;
42   private final boolean includeLower;
43   private final boolean includeUpper;
44
45
46   public ConstantScoreRangeQuery(String JavaDoc fieldName, String JavaDoc lowerVal, String JavaDoc upperVal, boolean includeLower, boolean includeUpper)
47   {
48     // do a little bit of normalization...
49
// open ended range queries should always be inclusive.
50
if (lowerVal==null) {
51       includeLower=true;
52     } else if (includeLower && lowerVal.equals("")) {
53       lowerVal=null;
54     }
55     if (upperVal==null) {
56       includeUpper=true;
57     }
58
59
60     this.fieldName = fieldName.intern(); // intern it, just like terms...
61
this.lowerVal = lowerVal;
62     this.upperVal = upperVal;
63     this.includeLower = includeLower;
64     this.includeUpper = includeUpper;
65   }
66
67   /** Returns the field name for this query */
68   public String JavaDoc getField() { return fieldName; }
69   /** Returns the value of the lower endpoint of this range query, null if open ended */
70   public String JavaDoc getLowerVal() { return lowerVal; }
71   /** Returns the value of the upper endpoint of this range query, null if open ended */
72   public String JavaDoc getUpperVal() { return upperVal; }
73   /** Returns <code>true</code> if the lower endpoint is inclusive */
74   public boolean includesLower() { return includeLower; }
75   /** Returns <code>true</code> if the upper endpoint is inclusive */
76   public boolean includesUpper() { return includeUpper; }
77
78   public Query rewrite(IndexReader reader) throws IOException JavaDoc {
79     // Map to RangeFilter semantics which are slightly different...
80
RangeFilter rangeFilt = new RangeFilter(fieldName,
81             lowerVal!=null?lowerVal:"",
82             upperVal, lowerVal==""?false:includeLower, upperVal==null?false:includeUpper);
83     Query q = new ConstantScoreQuery(rangeFilt);
84     q.setBoost(getBoost());
85     return q;
86   }
87
88     /** Prints a user-readable version of this query. */
89     public String JavaDoc toString(String JavaDoc field)
90     {
91         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
92         if (!getField().equals(field))
93         {
94             buffer.append(getField());
95             buffer.append(":");
96         }
97         buffer.append(includeLower ? '[' : '{');
98         buffer.append(lowerVal != null ? lowerVal : "*");
99         buffer.append(" TO ");
100         buffer.append(upperVal != null ? upperVal : "*");
101         buffer.append(includeUpper ? ']' : '}');
102         if (getBoost() != 1.0f)
103         {
104             buffer.append("^");
105             buffer.append(Float.toString(getBoost()));
106         }
107         return buffer.toString();
108     }
109
110     /** Returns true if <code>o</code> is equal to this. */
111     public boolean equals(Object JavaDoc o) {
112         if (this == o) return true;
113         if (!(o instanceof ConstantScoreRangeQuery)) return false;
114         ConstantScoreRangeQuery other = (ConstantScoreRangeQuery) o;
115
116         if (this.fieldName != other.fieldName // interned comparison
117
|| this.includeLower != other.includeLower
118             || this.includeUpper != other.includeUpper
119            ) { return false; }
120         if (this.lowerVal != null ? !this.lowerVal.equals(other.lowerVal) : other.lowerVal != null) return false;
121         if (this.upperVal != null ? !this.upperVal.equals(other.upperVal) : other.upperVal != null) return false;
122         return this.getBoost() == other.getBoost();
123     }
124
125     /** Returns a hash code value for this object.*/
126     public int hashCode() {
127       int h = Float.floatToIntBits(getBoost()) ^ fieldName.hashCode();
128       // hashCode of "" is 0, so don't use that for null...
129
h ^= lowerVal != null ? lowerVal.hashCode() : 0x965a965a;
130       // don't just XOR upperVal with out mixing either it or h, as it will cancel
131
// out lowerVal if they are equal.
132
h ^= (h << 17) | (h >>> 16); // a reversible (one to one) 32 bit mapping mix
133
h ^= (upperVal != null ? (upperVal.hashCode()) : 0x5a695a69);
134       h ^= (includeLower ? 0x665599aa : 0)
135          ^ (includeUpper ? 0x99aa5566 : 0);
136       return h;
137     }
138 }
139
Popular Tags