2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 using IndexReader = Mono.Lucene.Net.Index.IndexReader;
21 using ToStringUtils = Mono.Lucene.Net.Util.ToStringUtils;
23 namespace Mono.Lucene.Net.Search
26 /// <summary> A Query that matches documents within an exclusive range of terms.
28 /// <p/>This query matches the documents looking for terms that fall into the
29 /// supplied range according to {@link String#compareTo(String)}. It is not intended
30 /// for numerical ranges, use {@link NumericRangeQuery} instead.
32 /// <p/>This query uses the {@link
33 /// MultiTermQuery#CONSTANT_SCORE_AUTO_REWRITE_DEFAULT}
40 public class TermRangeQuery:MultiTermQuery
42 private System.String lowerTerm;
43 private System.String upperTerm;
44 private System.Globalization.CompareInfo collator;
45 private System.String field;
46 private bool includeLower;
47 private bool includeUpper;
50 /// <summary> Constructs a query selecting all terms greater/equal than <code>lowerTerm</code>
51 /// but less/equal than <code>upperTerm</code>.
54 /// If an endpoint is null, it is said
55 /// to be "open". Either or both endpoints may be open. Open endpoints may not
56 /// be exclusive (you can't select all but the first or last term without
57 /// explicitly specifying the term to exclude.)
60 /// <param name="field">The field that holds both lower and upper terms.
62 /// <param name="lowerTerm">The term text at the lower end of the range
64 /// <param name="upperTerm">The term text at the upper end of the range
66 /// <param name="includeLower">If true, the <code>lowerTerm</code> is
67 /// included in the range.
69 /// <param name="includeUpper">If true, the <code>upperTerm</code> is
70 /// included in the range.
72 public TermRangeQuery(System.String field, System.String lowerTerm, System.String upperTerm, bool includeLower, bool includeUpper):this(field, lowerTerm, upperTerm, includeLower, includeUpper, null)
76 /// <summary>Constructs a query selecting all terms greater/equal than
77 /// <code>lowerTerm</code> but less/equal than <code>upperTerm</code>.
79 /// If an endpoint is null, it is said
80 /// to be "open". Either or both endpoints may be open. Open endpoints may not
81 /// be exclusive (you can't select all but the first or last term without
82 /// explicitly specifying the term to exclude.)
84 /// If <code>collator</code> is not null, it will be used to decide whether
85 /// index terms are within the given range, rather than using the Unicode code
86 /// point order in which index terms are stored.
88 /// <strong>WARNING:</strong> Using this constructor and supplying a non-null
89 /// value in the <code>collator</code> parameter will cause every single
90 /// index Term in the Field referenced by lowerTerm and/or upperTerm to be
91 /// examined. Depending on the number of index Terms in this Field, the
92 /// operation could be very slow.
95 /// <param name="lowerTerm">The Term text at the lower end of the range
97 /// <param name="upperTerm">The Term text at the upper end of the range
99 /// <param name="includeLower">If true, the <code>lowerTerm</code> is
100 /// included in the range.
102 /// <param name="includeUpper">If true, the <code>upperTerm</code> is
103 /// included in the range.
105 /// <param name="collator">The collator to use to collate index Terms, to determine
106 /// their membership in the range bounded by <code>lowerTerm</code> and
107 /// <code>upperTerm</code>.
109 public TermRangeQuery(System.String field, System.String lowerTerm, System.String upperTerm, bool includeLower, bool includeUpper, System.Globalization.CompareInfo collator)
112 this.lowerTerm = lowerTerm;
113 this.upperTerm = upperTerm;
114 this.includeLower = includeLower;
115 this.includeUpper = includeUpper;
116 this.collator = collator;
119 /// <summary>Returns the field name for this query </summary>
120 public virtual System.String GetField()
125 /// <summary>Returns the lower value of this range query </summary>
126 public virtual System.String GetLowerTerm()
131 /// <summary>Returns the upper value of this range query </summary>
132 public virtual System.String GetUpperTerm()
137 /// <summary>Returns <code>true</code> if the lower endpoint is inclusive </summary>
138 public virtual bool IncludesLower()
143 /// <summary>Returns <code>true</code> if the upper endpoint is inclusive </summary>
144 public virtual bool IncludesUpper()
149 /// <summary>Returns the collator used to determine range inclusion, if any. </summary>
150 public virtual System.Globalization.CompareInfo GetCollator()
155 public /*protected internal*/ override FilteredTermEnum GetEnum(IndexReader reader)
157 return new TermRangeTermEnum(reader, field, lowerTerm, upperTerm, includeLower, includeUpper, collator);
160 /// <summary>Prints a user-readable version of this query. </summary>
161 public override System.String ToString(System.String field)
163 System.Text.StringBuilder buffer = new System.Text.StringBuilder();
164 if (!GetField().Equals(field))
166 buffer.Append(GetField());
169 buffer.Append(includeLower?'[':'{');
170 buffer.Append(lowerTerm != null?lowerTerm:"*");
171 buffer.Append(" TO ");
172 buffer.Append(upperTerm != null?upperTerm:"*");
173 buffer.Append(includeUpper?']':'}');
174 buffer.Append(ToStringUtils.Boost(GetBoost()));
175 return buffer.ToString();
179 public override int GetHashCode()
182 int result = base.GetHashCode();
183 result = prime * result + ((collator == null)?0:collator.GetHashCode());
184 result = prime * result + ((field == null)?0:field.GetHashCode());
185 result = prime * result + (includeLower?1231:1237);
186 result = prime * result + (includeUpper?1231:1237);
187 result = prime * result + ((lowerTerm == null)?0:lowerTerm.GetHashCode());
188 result = prime * result + ((upperTerm == null)?0:upperTerm.GetHashCode());
193 public override bool Equals(System.Object obj)
197 if (!base.Equals(obj))
199 if (GetType() != obj.GetType())
201 TermRangeQuery other = (TermRangeQuery) obj;
202 if (collator == null)
204 if (other.collator != null)
207 else if (!collator.Equals(other.collator))
211 if (other.field != null)
214 else if (!field.Equals(other.field))
216 if (includeLower != other.includeLower)
218 if (includeUpper != other.includeUpper)
220 if (lowerTerm == null)
222 if (other.lowerTerm != null)
225 else if (!lowerTerm.Equals(other.lowerTerm))
227 if (upperTerm == null)
229 if (other.upperTerm != null)
232 else if (!upperTerm.Equals(other.upperTerm))