Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
TermRangeQuery.cs
Go to the documentation of this file.
1 /*
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
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17 
18 using System;
19 using System.Globalization;
20 using IndexReader = Lucene.Net.Index.IndexReader;
21 using ToStringUtils = Lucene.Net.Util.ToStringUtils;
22 
23 namespace Lucene.Net.Search
24 {
25 
26  /// <summary> A Query that matches documents within an exclusive range of terms.
27  ///
28  /// <p/>This query matches the documents looking for terms that fall into the
29  /// supplied range according to <see cref="String.CompareTo(String)" />. It is not intended
30  /// for numerical ranges, use <see cref="NumericRangeQuery{T}" /> instead.
31  ///
32  /// <p/>This query uses the <see cref="MultiTermQuery.CONSTANT_SCORE_AUTO_REWRITE_DEFAULT" />
33  ///
34  /// rewrite method.
35  /// </summary>
36  /// <since> 2.9
37  /// </since>
38 
39  [Serializable]
41  {
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;
48 
49 
50  /// <summary> Constructs a query selecting all terms greater/equal than <c>lowerTerm</c>
51  /// but less/equal than <c>upperTerm</c>.
52  ///
53  /// <p/>
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.)
58  ///
59  /// </summary>
60  /// <param name="field">The field that holds both lower and upper terms.
61  /// </param>
62  /// <param name="lowerTerm">The term text at the lower end of the range
63  /// </param>
64  /// <param name="upperTerm">The term text at the upper end of the range
65  /// </param>
66  /// <param name="includeLower">If true, the <c>lowerTerm</c> is
67  /// included in the range.
68  /// </param>
69  /// <param name="includeUpper">If true, the <c>upperTerm</c> is
70  /// included in the range.
71  /// </param>
72  public TermRangeQuery(System.String field, System.String lowerTerm, System.String upperTerm, bool includeLower, bool includeUpper):this(field, lowerTerm, upperTerm, includeLower, includeUpper, null)
73  {
74  }
75 
76  /// <summary>Constructs a query selecting all terms greater/equal than
77  /// <c>lowerTerm</c> but less/equal than <c>upperTerm</c>.
78  /// <p/>
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.)
83  /// <p/>
84  /// If <c>collator</c> 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.
87  /// <p/>
88  /// <strong>WARNING:</strong> Using this constructor and supplying a non-null
89  /// value in the <c>collator</c> 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.
93  ///
94  /// </summary>
95  /// <param name="field"></param>
96  /// <param name="lowerTerm">The Term text at the lower end of the range
97  /// </param>
98  /// <param name="upperTerm">The Term text at the upper end of the range
99  /// </param>
100  /// <param name="includeLower">If true, the <c>lowerTerm</c> is
101  /// included in the range.
102  /// </param>
103  /// <param name="includeUpper">If true, the <c>upperTerm</c> is
104  /// included in the range.
105  /// </param>
106  /// <param name="collator">The collator to use to collate index Terms, to determine
107  /// their membership in the range bounded by <c>lowerTerm</c> and
108  /// <c>upperTerm</c>.
109  /// </param>
110  public TermRangeQuery(System.String field, System.String lowerTerm, System.String upperTerm, bool includeLower, bool includeUpper, System.Globalization.CompareInfo collator)
111  {
112  this.field = field;
113  this.lowerTerm = lowerTerm;
114  this.upperTerm = upperTerm;
115  this.includeLower = includeLower;
116  this.includeUpper = includeUpper;
117  this.collator = collator;
118  }
119 
120  /// <summary>Returns the field name for this query </summary>
121  public virtual string Field
122  {
123  get { return field; }
124  }
125 
126  /// <summary>Returns the lower value of this range query </summary>
127  public virtual string LowerTerm
128  {
129  get { return lowerTerm; }
130  }
131 
132  /// <summary>Returns the upper value of this range query </summary>
133  public virtual string UpperTerm
134  {
135  get { return upperTerm; }
136  }
137 
138  /// <summary>Returns <c>true</c> if the lower endpoint is inclusive </summary>
139  public virtual bool IncludesLower
140  {
141  get { return includeLower; }
142  }
143 
144  /// <summary>Returns <c>true</c> if the upper endpoint is inclusive </summary>
145  public virtual bool IncludesUpper
146  {
147  get { return includeUpper; }
148  }
149 
150  /// <summary>Returns the collator used to determine range inclusion, if any. </summary>
151  public virtual CompareInfo Collator
152  {
153  get { return collator; }
154  }
155 
156  protected internal override FilteredTermEnum GetEnum(IndexReader reader)
157  {
158  return new TermRangeTermEnum(reader, field, lowerTerm, upperTerm, includeLower, includeUpper, collator);
159  }
160 
161  /// <summary>Prints a user-readable version of this query. </summary>
162  public override System.String ToString(System.String field)
163  {
164  System.Text.StringBuilder buffer = new System.Text.StringBuilder();
165  if (!Field.Equals(field))
166  {
167  buffer.Append(Field);
168  buffer.Append(":");
169  }
170  buffer.Append(includeLower?'[':'{');
171  buffer.Append(lowerTerm != null?lowerTerm:"*");
172  buffer.Append(" TO ");
173  buffer.Append(upperTerm != null?upperTerm:"*");
174  buffer.Append(includeUpper?']':'}');
175  buffer.Append(ToStringUtils.Boost(Boost));
176  return buffer.ToString();
177  }
178 
179  //@Override
180  public override int GetHashCode()
181  {
182  int prime = 31;
183  int result = base.GetHashCode();
184  result = prime * result + ((collator == null)?0:collator.GetHashCode());
185  result = prime * result + ((field == null)?0:field.GetHashCode());
186  result = prime * result + (includeLower?1231:1237);
187  result = prime * result + (includeUpper?1231:1237);
188  result = prime * result + ((lowerTerm == null)?0:lowerTerm.GetHashCode());
189  result = prime * result + ((upperTerm == null)?0:upperTerm.GetHashCode());
190  return result;
191  }
192 
193  //@Override
194  public override bool Equals(System.Object obj)
195  {
196  if (this == obj)
197  return true;
198  if (!base.Equals(obj))
199  return false;
200  if (GetType() != obj.GetType())
201  return false;
202  TermRangeQuery other = (TermRangeQuery) obj;
203  if (collator == null)
204  {
205  if (other.collator != null)
206  return false;
207  }
208  else if (!collator.Equals(other.collator))
209  return false;
210  if (field == null)
211  {
212  if (other.field != null)
213  return false;
214  }
215  else if (!field.Equals(other.field))
216  return false;
217  if (includeLower != other.includeLower)
218  return false;
219  if (includeUpper != other.includeUpper)
220  return false;
221  if (lowerTerm == null)
222  {
223  if (other.lowerTerm != null)
224  return false;
225  }
226  else if (!lowerTerm.Equals(other.lowerTerm))
227  return false;
228  if (upperTerm == null)
229  {
230  if (other.upperTerm != null)
231  return false;
232  }
233  else if (!upperTerm.Equals(other.upperTerm))
234  return false;
235  return true;
236  }
237  }
238 }