QueryResponses.cs, DumpIndex.cs, IQueryResult.cs, QueryExecutor.cs, QueryResult.cs...
[beagle.git] / beagled / Lucene.Net / Search / RangeQuery.cs
blobfa8a97e74e1e4ce09df59a21d9139ac45a8c4b1a
1 /*
2 * Copyright 2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 using System;
18 using IndexReader = Lucene.Net.Index.IndexReader;
19 using Term = Lucene.Net.Index.Term;
20 using TermEnum = Lucene.Net.Index.TermEnum;
21 using ToStringUtils = Lucene.Net.Util.ToStringUtils;
23 namespace Lucene.Net.Search
26 /// <summary> A Query that matches documents within an exclusive range. A RangeQuery
27 /// is built by QueryParser for input like <code>[010 TO 120]</code>.
28 ///
29 /// </summary>
30 /// <version> $Id: RangeQuery.cs,v 1.5 2006/10/02 18:18:01 joeshaw Exp $
31 /// </version>
32 [Serializable]
33 public class RangeQuery : Query
35 private Term lowerTerm;
36 private Term upperTerm;
37 private bool inclusive;
39 /// <summary>Constructs a query selecting all terms greater than
40 /// <code>lowerTerm</code> but less than <code>upperTerm</code>.
41 /// There must be at least one term and either term may be null,
42 /// in which case there is no bound on that side, but if there are
43 /// two terms, both terms <b>must</b> be for the same field.
44 /// </summary>
45 public RangeQuery(Term lowerTerm, Term upperTerm, bool inclusive)
47 if (lowerTerm == null && upperTerm == null)
49 throw new System.ArgumentException("At least one term must be non-null");
51 if (lowerTerm != null && upperTerm != null && lowerTerm.Field() != upperTerm.Field())
53 throw new System.ArgumentException("Both terms must be for the same field");
56 // if we have a lowerTerm, start there. otherwise, start at beginning
57 if (lowerTerm != null)
59 this.lowerTerm = lowerTerm;
61 else
63 this.lowerTerm = new Term(upperTerm.Field(), "");
66 this.upperTerm = upperTerm;
67 this.inclusive = inclusive;
70 public override Query Rewrite(IndexReader reader)
73 BooleanQuery query = new BooleanQuery(true);
74 TermEnum enumerator = reader.Terms(lowerTerm);
76 try
79 bool checkLower = false;
80 if (!inclusive)
81 // make adjustments to set to exclusive
82 checkLower = true;
84 System.String testField = GetField();
86 do
88 Term term = enumerator.Term();
89 if (term != null && term.Field() == testField)
91 if (!checkLower || String.CompareOrdinal(term.Text(), lowerTerm.Text()) > 0)
93 checkLower = false;
94 if (upperTerm != null)
96 int compare = String.CompareOrdinal(upperTerm.Text(), term.Text());
97 /* if beyond the upper term, or is exclusive and
98 * this is equal to the upper term, break out */
99 if ((compare < 0) || (!inclusive && compare == 0))
100 break;
102 TermQuery tq = new TermQuery(term); // found a match
103 tq.SetBoost(GetBoost()); // set the boost
104 query.Add(tq, BooleanClause.Occur.SHOULD); // add to query
107 else
109 break;
112 while (enumerator.Next());
114 finally
116 enumerator.Close();
118 return query;
121 /// <summary>Returns the field name for this query </summary>
122 public virtual System.String GetField()
124 return (lowerTerm != null?lowerTerm.Field():upperTerm.Field());
127 /// <summary>Returns the lower term of this range query </summary>
128 public virtual Term GetLowerTerm()
130 return lowerTerm;
133 /// <summary>Returns the upper term of this range query </summary>
134 public virtual Term GetUpperTerm()
136 return upperTerm;
139 /// <summary>Returns <code>true</code> if the range query is inclusive </summary>
140 public virtual bool IsInclusive()
142 return inclusive;
146 /// <summary>Prints a user-readable version of this query. </summary>
147 public override System.String ToString(System.String field)
149 System.Text.StringBuilder buffer = new System.Text.StringBuilder();
150 if (!GetField().Equals(field))
152 buffer.Append(GetField());
153 buffer.Append(":");
155 buffer.Append(inclusive?"[":"{");
156 buffer.Append(lowerTerm != null ? lowerTerm.Text() : "null");
157 buffer.Append(" TO ");
158 buffer.Append(upperTerm != null ? upperTerm.Text() : "null");
159 buffer.Append(inclusive ? "]" : "}");
160 buffer.Append(ToStringUtils.Boost(GetBoost()));
161 return buffer.ToString();
164 /// <summary>Returns true iff <code>o</code> is equal to this. </summary>
165 public override bool Equals(System.Object o)
167 if (this == o)
168 return true;
169 if (!(o is RangeQuery))
170 return false;
172 RangeQuery other = (RangeQuery) o;
173 if (this.GetBoost() != other.GetBoost())
174 return false;
175 if (this.inclusive != other.inclusive)
176 return false;
177 // one of lowerTerm and upperTerm can be null
178 if (this.lowerTerm != null?!this.lowerTerm.Equals(other.lowerTerm):other.lowerTerm != null)
179 return false;
180 if (this.upperTerm != null?!this.upperTerm.Equals(other.upperTerm):other.upperTerm != null)
181 return false;
182 return true;
185 /// <summary>Returns a hash code value for this object.</summary>
186 public override int GetHashCode()
188 return BitConverter.ToInt32(BitConverter.GetBytes(GetBoost()), 0) ^ (lowerTerm != null ? lowerTerm.GetHashCode():0) ^ (upperTerm != null?upperTerm.GetHashCode() : 0) ^ (this.inclusive ? 1 : 0);