2 * Copyright 2004 The Apache Software Foundation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>.
30 /// <version> $Id: RangeQuery.cs,v 1.5 2006/10/02 18:18:01 joeshaw Exp $
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.
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
;
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
);
79 bool checkLower
= false;
81 // make adjustments to set to exclusive
84 System
.String testField
= GetField();
88 Term term
= enumerator
.Term();
89 if (term
!= null && term
.Field() == testField
)
91 if (!checkLower
|| String
.CompareOrdinal(term
.Text(), lowerTerm
.Text()) > 0)
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))
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
112 while (enumerator
.Next());
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()
133 /// <summary>Returns the upper term of this range query </summary>
134 public virtual Term
GetUpperTerm()
139 /// <summary>Returns <code>true</code> if the range query is inclusive </summary>
140 public virtual bool IsInclusive()
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());
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
)
169 if (!(o
is RangeQuery
))
172 RangeQuery other
= (RangeQuery
) o
;
173 if (this.GetBoost() != other
.GetBoost())
175 if (this.inclusive
!= other
.inclusive
)
177 // one of lowerTerm and upperTerm can be null
178 if (this.lowerTerm
!= null?!this.lowerTerm
.Equals(other
.lowerTerm
):other
.lowerTerm
!= null)
180 if (this.upperTerm
!= null?!this.upperTerm
.Equals(other
.upperTerm
):other
.upperTerm
!= null)
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);