KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
27  * A Query that matches documents within an exclusive range. A RangeQuery
28  * is built by QueryParser for input like <code>[010 TO 120]</code>.
29  *
30  * @version $Id: RangeQuery.java 358693 2005-12-23 03:37:50Z yonik $
31  */

32 public class RangeQuery extends Query
33 {
34     private Term lowerTerm;
35     private Term upperTerm;
36     private boolean inclusive;
37
38     /** Constructs a query selecting all terms greater than
39      * <code>lowerTerm</code> but less than <code>upperTerm</code>.
40      * There must be at least one term and either term may be null,
41      * in which case there is no bound on that side, but if there are
42      * two terms, both terms <b>must</b> be for the same field.
43      */

44     public RangeQuery(Term lowerTerm, Term upperTerm, boolean inclusive)
45     {
46         if (lowerTerm == null && upperTerm == null)
47         {
48             throw new IllegalArgumentException JavaDoc("At least one term must be non-null");
49         }
50         if (lowerTerm != null && upperTerm != null && lowerTerm.field() != upperTerm.field())
51         {
52             throw new IllegalArgumentException JavaDoc("Both terms must be for the same field");
53         }
54
55         // if we have a lowerTerm, start there. otherwise, start at beginning
56
if (lowerTerm != null) {
57             this.lowerTerm = lowerTerm;
58         }
59         else {
60             this.lowerTerm = new Term(upperTerm.field(), "");
61         }
62
63         this.upperTerm = upperTerm;
64         this.inclusive = inclusive;
65     }
66
67     public Query rewrite(IndexReader reader) throws IOException JavaDoc {
68
69         BooleanQuery query = new BooleanQuery(true);
70         TermEnum enumerator = reader.terms(lowerTerm);
71
72         try {
73
74             boolean checkLower = false;
75             if (!inclusive) // make adjustments to set to exclusive
76
checkLower = true;
77
78             String JavaDoc testField = getField();
79
80             do {
81                 Term term = enumerator.term();
82                 if (term != null && term.field() == testField) {
83                     if (!checkLower || term.text().compareTo(lowerTerm.text()) > 0) {
84                         checkLower = false;
85                         if (upperTerm != null) {
86                             int compare = upperTerm.text().compareTo(term.text());
87                             /* if beyond the upper term, or is exclusive and
88                              * this is equal to the upper term, break out */

89                             if ((compare < 0) || (!inclusive && compare == 0))
90                                 break;
91                         }
92                         TermQuery tq = new TermQuery(term); // found a match
93
tq.setBoost(getBoost()); // set the boost
94
query.add(tq, BooleanClause.Occur.SHOULD); // add to query
95
}
96                 }
97                 else {
98                     break;
99                 }
100             }
101             while (enumerator.next());
102         }
103         finally {
104             enumerator.close();
105         }
106         return query;
107     }
108
109     /** Returns the field name for this query */
110     public String JavaDoc getField() {
111       return (lowerTerm != null ? lowerTerm.field() : upperTerm.field());
112     }
113
114     /** Returns the lower term of this range query */
115     public Term getLowerTerm() { return lowerTerm; }
116
117     /** Returns the upper term of this range query */
118     public Term getUpperTerm() { return upperTerm; }
119
120     /** Returns <code>true</code> if the range query is inclusive */
121     public boolean isInclusive() { return inclusive; }
122
123
124     /** Prints a user-readable version of this query. */
125     public String JavaDoc toString(String JavaDoc field)
126     {
127         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
128         if (!getField().equals(field))
129         {
130             buffer.append(getField());
131             buffer.append(":");
132         }
133         buffer.append(inclusive ? "[" : "{");
134         buffer.append(lowerTerm != null ? lowerTerm.text() : "null");
135         buffer.append(" TO ");
136         buffer.append(upperTerm != null ? upperTerm.text() : "null");
137         buffer.append(inclusive ? "]" : "}");
138         buffer.append(ToStringUtils.boost(getBoost()));
139         return buffer.toString();
140     }
141
142     /** Returns true iff <code>o</code> is equal to this. */
143     public boolean equals(Object JavaDoc o) {
144         if (this == o) return true;
145         if (!(o instanceof RangeQuery)) return false;
146
147         final RangeQuery other = (RangeQuery) o;
148         if (this.getBoost() != other.getBoost()) return false;
149         if (this.inclusive != other.inclusive) return false;
150         // one of lowerTerm and upperTerm can be null
151
if (this.lowerTerm != null ? !this.lowerTerm.equals(other.lowerTerm) : other.lowerTerm != null) return false;
152         if (this.upperTerm != null ? !this.upperTerm.equals(other.upperTerm) : other.upperTerm != null) return false;
153         return true;
154     }
155
156     /** Returns a hash code value for this object.*/
157     public int hashCode() {
158       int h = Float.floatToIntBits(getBoost());
159       h ^= lowerTerm != null ? lowerTerm.hashCode() : 0;
160       // reversible mix to make lower and upper position dependent and
161
// to prevent them from cancelling out.
162
h ^= (h << 25) | (h >>> 8);
163       h ^= upperTerm != null ? upperTerm.hashCode() : 0;
164       h ^= this.inclusive ? 0x2742E74A : 0;
165       return h;
166     }
167 }
168
Popular Tags