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 TermDocs
= Lucene
.Net
.Index
.TermDocs
;
21 using TermEnum
= Lucene
.Net
.Index
.TermEnum
;
23 namespace Lucene
.Net
.Search
26 /// <summary> A Filter that restricts search results to a range of values in a given
30 /// This code borrows heavily from {@link RangeQuery}, but is implemented as a Filter
31 /// (much like {@link DateFilter}).
35 public class RangeFilter
: Filter
38 private System
.String fieldName
;
39 private System
.String lowerTerm
;
40 private System
.String upperTerm
;
41 private bool includeLower
;
42 private bool includeUpper
;
44 /// <param name="fieldName">The field this range applies to
46 /// <param name="lowerTerm">The lower bound on this range
48 /// <param name="upperTerm">The upper bound on this range
50 /// <param name="includeLower">Does this range include the lower bound?
52 /// <param name="includeUpper">Does this range include the upper bound?
54 /// <throws> IllegalArgumentException if both terms are null or if </throws>
55 /// <summary> lowerTerm is null and includeLower is true (similar for upperTerm
58 public RangeFilter(System
.String fieldName
, System
.String lowerTerm
, System
.String upperTerm
, bool includeLower
, bool includeUpper
)
60 this.fieldName
= fieldName
;
61 this.lowerTerm
= lowerTerm
;
62 this.upperTerm
= upperTerm
;
63 this.includeLower
= includeLower
;
64 this.includeUpper
= includeUpper
;
66 if (null == lowerTerm
&& null == upperTerm
)
68 throw new System
.ArgumentException("At least one value must be non-null");
70 if (includeLower
&& null == lowerTerm
)
72 throw new System
.ArgumentException("The lower bound must be non-null to be inclusive");
74 if (includeUpper
&& null == upperTerm
)
76 throw new System
.ArgumentException("The upper bound must be non-null to be inclusive");
80 /// <summary> Constructs a filter for field <code>fieldName</code> matching
81 /// less than or equal to <code>upperTerm</code>.
83 public static RangeFilter
Less(System
.String fieldName
, System
.String upperTerm
)
85 return new RangeFilter(fieldName
, null, upperTerm
, false, true);
88 /// <summary> Constructs a filter for field <code>fieldName</code> matching
89 /// greater than or equal to <code>lowerTerm</code>.
91 public static RangeFilter
More(System
.String fieldName
, System
.String lowerTerm
)
93 return new RangeFilter(fieldName
, lowerTerm
, null, true, false);
96 /// <summary> Returns a BitSet with true for documents which should be
97 /// permitted in search results, and false for those that should
100 public override System
.Collections
.BitArray
Bits(IndexReader reader
)
102 System
.Collections
.BitArray bits
= new System
.Collections
.BitArray((reader
.MaxDoc() % 64 == 0?reader
.MaxDoc() / 64:reader
.MaxDoc() / 64 + 1) * 64);
103 TermEnum enumerator
= (null != lowerTerm
?reader
.Terms(new Term(fieldName
, lowerTerm
)):reader
.Terms(new Term(fieldName
, "")));
108 if (enumerator
.Term() == null)
113 bool checkLower
= false;
115 // make adjustments to set to exclusive
118 TermDocs termDocs
= reader
.TermDocs();
124 Term term
= enumerator
.Term();
125 if (term
!= null && term
.Field().Equals(fieldName
))
127 if (!checkLower
|| null == lowerTerm
|| String
.CompareOrdinal(term
.Text(), lowerTerm
) > 0)
130 if (upperTerm
!= null)
132 int compare
= String
.CompareOrdinal(upperTerm
, term
.Text());
133 /* if beyond the upper term, or is exclusive and
134 * this is equal to the upper term, break out */
135 if ((compare
< 0) || (!includeUpper
&& compare
== 0))
140 /* we have a good term, find the docs */
142 termDocs
.Seek(enumerator
.Term());
143 while (termDocs
.Next())
145 bits
.Set(termDocs
.Doc(), true);
154 while (enumerator
.Next());
169 public override System
.String
ToString()
171 System
.Text
.StringBuilder buffer
= new System
.Text
.StringBuilder();
172 buffer
.Append(fieldName
);
174 buffer
.Append(includeLower
?"[":"{");
175 if (null != lowerTerm
)
177 buffer
.Append(lowerTerm
);
180 if (null != upperTerm
)
182 buffer
.Append(upperTerm
);
184 buffer
.Append(includeUpper
? "]" : "}");
185 return buffer
.ToString();
188 /// <summary>Returns true if <code>o</code> is equal to this. </summary>
189 public override bool Equals(System
.Object o
)
193 if (!(o
is RangeFilter
))
195 RangeFilter other
= (RangeFilter
) o
;
197 if (!this.fieldName
.Equals(other
.fieldName
) || this.includeLower
!= other
.includeLower
|| this.includeUpper
!= other
.includeUpper
)
201 if (this.lowerTerm
!= null ? !this.lowerTerm
.Equals(other
.lowerTerm
) : other
.lowerTerm
!= null)
203 if (this.upperTerm
!= null ? !this.upperTerm
.Equals(other
.upperTerm
) : other
.upperTerm
!= null)
208 /// <summary>Returns a hash code value for this object.</summary>
209 public override int GetHashCode()
211 int h
= fieldName
.GetHashCode();
212 h ^
= (lowerTerm
!= null ? lowerTerm
.GetHashCode() : unchecked((int) 0xB6ECE882)); // {{Aroush-1.9}} is this OK?!
213 h
= (h
<< 1) | (SupportClass
.Number
.URShift(h
, 31)); // rotate to distinguish lower from upper
214 h ^
= (upperTerm
!= null ? (upperTerm
.GetHashCode()) : unchecked((int) 0x91BEC2C2)); // {{Aroush-1.9}} is this OK?!
215 h ^
= (includeLower
? unchecked((int) 0xD484B933) : 0) ^
(includeUpper
? 0x6AE423AC : 0); // {{Aroush-1.9}} is this OK?!