001: package org.apache.lucene.search;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import java.io.IOException;
021:
022: import org.apache.lucene.index.Term;
023: import org.apache.lucene.index.TermEnum;
024: import org.apache.lucene.index.IndexReader;
025: import org.apache.lucene.util.ToStringUtils;
026:
027: /**
028: * A Query that matches documents within an exclusive range. A RangeQuery
029: * is built by QueryParser for input like <code>[010 TO 120]</code> but only if the QueryParser has
030: * the useOldRangeQuery property set to true. The QueryParser default behaviour is to use
031: * the newer ConstantScoreRangeQuery class. This is generally preferable because:
032: * <ul>
033: * <li>It is faster than RangeQuery</li>
034: * <li>Unlike RangeQuery, it does not cause a BooleanQuery.TooManyClauses exception if the range of values is large</li>
035: * <li>Unlike RangeQuery it does not influence scoring based on the scarcity of individual terms that may match</li>
036: * </ul>
037: *
038: *
039: * @see ConstantScoreRangeQuery
040: *
041: *
042: * @version $Id: RangeQuery.java 520891 2007-03-21 13:58:47Z yonik $
043: */
044: public class RangeQuery extends Query {
045: private Term lowerTerm;
046: private Term upperTerm;
047: private boolean inclusive;
048:
049: /** Constructs a query selecting all terms greater than
050: * <code>lowerTerm</code> but less than <code>upperTerm</code>.
051: * There must be at least one term and either term may be null,
052: * in which case there is no bound on that side, but if there are
053: * two terms, both terms <b>must</b> be for the same field.
054: */
055: public RangeQuery(Term lowerTerm, Term upperTerm, boolean inclusive) {
056: if (lowerTerm == null && upperTerm == null) {
057: throw new IllegalArgumentException(
058: "At least one term must be non-null");
059: }
060: if (lowerTerm != null && upperTerm != null
061: && lowerTerm.field() != upperTerm.field()) {
062: throw new IllegalArgumentException(
063: "Both terms must be for the same field");
064: }
065:
066: // if we have a lowerTerm, start there. otherwise, start at beginning
067: if (lowerTerm != null) {
068: this .lowerTerm = lowerTerm;
069: } else {
070: this .lowerTerm = new Term(upperTerm.field(), "");
071: }
072:
073: this .upperTerm = upperTerm;
074: this .inclusive = inclusive;
075: }
076:
077: public Query rewrite(IndexReader reader) throws IOException {
078:
079: BooleanQuery query = new BooleanQuery(true);
080: TermEnum enumerator = reader.terms(lowerTerm);
081:
082: try {
083:
084: boolean checkLower = false;
085: if (!inclusive) // make adjustments to set to exclusive
086: checkLower = true;
087:
088: String testField = getField();
089:
090: do {
091: Term term = enumerator.term();
092: if (term != null && term.field() == testField) { // interned comparison
093: if (!checkLower
094: || term.text().compareTo(lowerTerm.text()) > 0) {
095: checkLower = false;
096: if (upperTerm != null) {
097: int compare = upperTerm.text().compareTo(
098: term.text());
099: /* if beyond the upper term, or is exclusive and
100: * this is equal to the upper term, break out */
101: if ((compare < 0)
102: || (!inclusive && compare == 0))
103: break;
104: }
105: TermQuery tq = new TermQuery(term); // found a match
106: tq.setBoost(getBoost()); // set the boost
107: query.add(tq, BooleanClause.Occur.SHOULD); // add to query
108: }
109: } else {
110: break;
111: }
112: } while (enumerator.next());
113: } finally {
114: enumerator.close();
115: }
116: return query;
117: }
118:
119: /** Returns the field name for this query */
120: public String getField() {
121: return (lowerTerm != null ? lowerTerm.field() : upperTerm
122: .field());
123: }
124:
125: /** Returns the lower term of this range query */
126: public Term getLowerTerm() {
127: return lowerTerm;
128: }
129:
130: /** Returns the upper term of this range query */
131: public Term getUpperTerm() {
132: return upperTerm;
133: }
134:
135: /** Returns <code>true</code> if the range query is inclusive */
136: public boolean isInclusive() {
137: return inclusive;
138: }
139:
140: /** Prints a user-readable version of this query. */
141: public String toString(String field) {
142: StringBuffer buffer = new StringBuffer();
143: if (!getField().equals(field)) {
144: buffer.append(getField());
145: buffer.append(":");
146: }
147: buffer.append(inclusive ? "[" : "{");
148: buffer.append(lowerTerm != null ? lowerTerm.text() : "null");
149: buffer.append(" TO ");
150: buffer.append(upperTerm != null ? upperTerm.text() : "null");
151: buffer.append(inclusive ? "]" : "}");
152: buffer.append(ToStringUtils.boost(getBoost()));
153: return buffer.toString();
154: }
155:
156: /** Returns true iff <code>o</code> is equal to this. */
157: public boolean equals(Object o) {
158: if (this == o)
159: return true;
160: if (!(o instanceof RangeQuery))
161: return false;
162:
163: final RangeQuery other = (RangeQuery) o;
164: if (this .getBoost() != other.getBoost())
165: return false;
166: if (this .inclusive != other.inclusive)
167: return false;
168: // one of lowerTerm and upperTerm can be null
169: if (this .lowerTerm != null ? !this .lowerTerm
170: .equals(other.lowerTerm) : other.lowerTerm != null)
171: return false;
172: if (this .upperTerm != null ? !this .upperTerm
173: .equals(other.upperTerm) : other.upperTerm != null)
174: return false;
175: return true;
176: }
177:
178: /** Returns a hash code value for this object.*/
179: public int hashCode() {
180: int h = Float.floatToIntBits(getBoost());
181: h ^= lowerTerm != null ? lowerTerm.hashCode() : 0;
182: // reversible mix to make lower and upper position dependent and
183: // to prevent them from cancelling out.
184: h ^= (h << 25) | (h >>> 8);
185: h ^= upperTerm != null ? upperTerm.hashCode() : 0;
186: h ^= this .inclusive ? 0x2742E74A : 0;
187: return h;
188: }
189: }
|