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
NumericRangeFilter.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 
20 using NumericTokenStream = Lucene.Net.Analysis.NumericTokenStream;
21 using NumericField = Lucene.Net.Documents.NumericField;
22 using NumericUtils = Lucene.Net.Util.NumericUtils;
23 
24 namespace Lucene.Net.Search
25 {
26 
27  /// <summary> A <see cref="Filter" /> that only accepts numeric values within
28  /// a specified range. To use this, you must first index the
29  /// numeric values using <see cref="NumericField" /> (expert: <see cref="NumericTokenStream" />
30  ///).
31  ///
32  /// <p/>You create a new NumericRangeFilter with the static
33  /// factory methods, eg:
34  ///
35  /// <code>
36  /// Filter f = NumericRangeFilter.newFloatRange("weight",
37  /// new Float(0.3f), new Float(0.10f),
38  /// true, true);
39  /// </code>
40  ///
41  /// accepts all documents whose float valued "weight" field
42  /// ranges from 0.3 to 0.10, inclusive.
43  /// See <see cref="NumericRangeQuery{T}" /> for details on how Lucene
44  /// indexes and searches numeric valued fields.
45  ///
46  /// <p/><font color="red"><b>NOTE:</b> This API is experimental and
47  /// might change in incompatible ways in the next
48  /// release.</font>
49  ///
50  /// </summary>
51  /// <since> 2.9
52  ///
53  /// </since>
54  [Serializable]
55  public sealed class NumericRangeFilter<T> : MultiTermQueryWrapperFilter<NumericRangeQuery<T>>
56  where T : struct, IComparable<T>
57  // real numbers in C# are structs and IComparable with themselves, best constraint we have
58  {
59  internal NumericRangeFilter(NumericRangeQuery<T> query)
60  : base(query)
61  {
62  }
63 
64  /// <summary>Returns the field name for this filter </summary>
65  public string Field
66  {
67  get { return query.Field; }
68  }
69 
70  /// <summary>Returns <c>true</c> if the lower endpoint is inclusive </summary>
71  public bool IncludesMin
72  {
73  get { return query.IncludesMin; }
74  }
75 
76  /// <summary>Returns <c>true</c> if the upper endpoint is inclusive </summary>
77  public bool IncludesMax
78  {
79  get { return query.IncludesMax; }
80  }
81 
82  /// <summary>Returns the lower value of this range filter </summary>
83  public T? Min
84  {
85  get { return query.Min; }
86  }
87 
88  /// <summary>Returns the upper value of this range filter </summary>
89  public T? Max
90  {
91  get { return query.Max; }
92  }
93  }
94 
95  public static class NumericRangeFilter
96  {
97  /// <summary> Factory that creates a <c>NumericRangeFilter</c>, that filters a <c>long</c>
98  /// range using the given <a href="NumericRangeQuery.html#precisionStepDesc"><c>precisionStep</c></a>.
99  /// You can have half-open ranges (which are in fact &lt;/&#8804; or &gt;/&#8805; queries)
100  /// by setting the min or max value to <c>null</c>. By setting inclusive to false, it will
101  /// match all documents excluding the bounds, with inclusive on, the boundaries are hits, too.
102  /// </summary>
103  public static NumericRangeFilter<long> NewLongRange(System.String field, int precisionStep, long? min, long? max, bool minInclusive, bool maxInclusive)
104  {
105  return new NumericRangeFilter<long>(NumericRangeQuery.NewLongRange(field, precisionStep, min, max, minInclusive, maxInclusive));
106  }
107 
108  /// <summary> Factory that creates a <c>NumericRangeFilter</c>, that queries a <c>long</c>
109  /// range using the default <c>precisionStep</c> <see cref="NumericUtils.PRECISION_STEP_DEFAULT" /> (4).
110  /// You can have half-open ranges (which are in fact &lt;/&#8804; or &gt;/&#8805; queries)
111  /// by setting the min or max value to <c>null</c>. By setting inclusive to false, it will
112  /// match all documents excluding the bounds, with inclusive on, the boundaries are hits, too.
113  /// </summary>
114  public static NumericRangeFilter<long> NewLongRange(System.String field, long? min, long? max, bool minInclusive, bool maxInclusive)
115  {
116  return new NumericRangeFilter<long>(NumericRangeQuery.NewLongRange(field, min, max, minInclusive, maxInclusive));
117  }
118 
119  /// <summary> Factory that creates a <c>NumericRangeFilter</c>, that filters a <c>int</c>
120  /// range using the given <a href="NumericRangeQuery.html#precisionStepDesc"><c>precisionStep</c></a>.
121  /// You can have half-open ranges (which are in fact &lt;/&#8804; or &gt;/&#8805; queries)
122  /// by setting the min or max value to <c>null</c>. By setting inclusive to false, it will
123  /// match all documents excluding the bounds, with inclusive on, the boundaries are hits, too.
124  /// </summary>
125  public static NumericRangeFilter<int> NewIntRange(System.String field, int precisionStep, int? min, int? max, bool minInclusive, bool maxInclusive)
126  {
127  return new NumericRangeFilter<int>(NumericRangeQuery.NewIntRange(field, precisionStep, min, max, minInclusive, maxInclusive));
128  }
129 
130  /// <summary> Factory that creates a <c>NumericRangeFilter</c>, that queries a <c>int</c>
131  /// range using the default <c>precisionStep</c> <see cref="NumericUtils.PRECISION_STEP_DEFAULT" /> (4).
132  /// You can have half-open ranges (which are in fact &lt;/&#8804; or &gt;/&#8805; queries)
133  /// by setting the min or max value to <c>null</c>. By setting inclusive to false, it will
134  /// match all documents excluding the bounds, with inclusive on, the boundaries are hits, too.
135  /// </summary>
136  public static NumericRangeFilter<int> NewIntRange(System.String field, int? min, int? max, bool minInclusive, bool maxInclusive)
137  {
138  return new NumericRangeFilter<int>(NumericRangeQuery.NewIntRange(field, min, max, minInclusive, maxInclusive));
139  }
140 
141  /// <summary> Factory that creates a <c>NumericRangeFilter</c>, that filters a <c>double</c>
142  /// range using the given <a href="NumericRangeQuery.html#precisionStepDesc"><c>precisionStep</c></a>.
143  /// You can have half-open ranges (which are in fact &lt;/&#8804; or &gt;/&#8805; queries)
144  /// by setting the min or max value to <c>null</c>. By setting inclusive to false, it will
145  /// match all documents excluding the bounds, with inclusive on, the boundaries are hits, too.
146  /// </summary>
147  public static NumericRangeFilter<double> NewDoubleRange(System.String field, int precisionStep, double? min, double? max, bool minInclusive, bool maxInclusive)
148  {
149  return new NumericRangeFilter<double>(NumericRangeQuery.NewDoubleRange(field, precisionStep, min, max, minInclusive, maxInclusive));
150  }
151 
152  /// <summary> Factory that creates a <c>NumericRangeFilter</c>, that queries a <c>double</c>
153  /// range using the default <c>precisionStep</c> <see cref="NumericUtils.PRECISION_STEP_DEFAULT" /> (4).
154  /// You can have half-open ranges (which are in fact &lt;/&#8804; or &gt;/&#8805; queries)
155  /// by setting the min or max value to <c>null</c>. By setting inclusive to false, it will
156  /// match all documents excluding the bounds, with inclusive on, the boundaries are hits, too.
157  /// </summary>
158  public static NumericRangeFilter<double> NewDoubleRange(System.String field, double? min, double? max, bool minInclusive, bool maxInclusive)
159  {
160  return new NumericRangeFilter<double>(NumericRangeQuery.NewDoubleRange(field, min, max, minInclusive, maxInclusive));
161  }
162 
163  /// <summary> Factory that creates a <c>NumericRangeFilter</c>, that filters a <c>float</c>
164  /// range using the given <a href="NumericRangeQuery.html#precisionStepDesc"><c>precisionStep</c></a>.
165  /// You can have half-open ranges (which are in fact &lt;/&#8804; or &gt;/&#8805; queries)
166  /// by setting the min or max value to <c>null</c>. By setting inclusive to false, it will
167  /// match all documents excluding the bounds, with inclusive on, the boundaries are hits, too.
168  /// </summary>
169  public static NumericRangeFilter<float> NewFloatRange(System.String field, int precisionStep, float? min, float? max, bool minInclusive, bool maxInclusive)
170  {
171  return new NumericRangeFilter<float>(NumericRangeQuery.NewFloatRange(field, precisionStep, min, max, minInclusive, maxInclusive));
172  }
173 
174  /// <summary> Factory that creates a <c>NumericRangeFilter</c>, that queries a <c>float</c>
175  /// range using the default <c>precisionStep</c> <see cref="NumericUtils.PRECISION_STEP_DEFAULT" /> (4).
176  /// You can have half-open ranges (which are in fact &lt;/&#8804; or &gt;/&#8805; queries)
177  /// by setting the min or max value to <c>null</c>. By setting inclusive to false, it will
178  /// match all documents excluding the bounds, with inclusive on, the boundaries are hits, too.
179  /// </summary>
180  public static NumericRangeFilter<float> NewFloatRange(System.String field, float? min, float? max, bool minInclusive, bool maxInclusive)
181  {
182  return new NumericRangeFilter<float>(NumericRangeQuery.NewFloatRange(field, min, max, minInclusive, maxInclusive));
183  }
184  }
185 }