1 package org.apache.lucene.search;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 import org.apache.lucene.analysis.NumericTokenStream; // for javadocs
21 import org.apache.lucene.document.NumericField; // for javadocs
22 import org.apache.lucene.util.NumericUtils; // for javadocs
25 * A {@link Filter} that only accepts numeric values within
26 * a specified range. To use this, you must first index the
27 * numeric values using {@link NumericField} (expert: {@link
28 * NumericTokenStream}).
30 * <p>You create a new NumericRangeFilter with the static
31 * factory methods, eg:
34 * Filter f = NumericRangeFilter.newFloatRange("weight", 0.03f, 0.10f, true, true);
37 * accepts all documents whose float valued "weight" field
38 * ranges from 0.03 to 0.10, inclusive.
39 * See {@link NumericRangeQuery} for details on how Lucene
40 * indexes and searches numeric valued fields.
44 public final class NumericRangeFilter<T extends Number> extends MultiTermQueryWrapperFilter<NumericRangeQuery<T>> {
46 private NumericRangeFilter(final NumericRangeQuery<T> query) {
51 * Factory that creates a <code>NumericRangeFilter</code>, that filters a <code>long</code>
52 * range using the given <a href="NumericRangeQuery.html#precisionStepDesc"><code>precisionStep</code></a>.
53 * You can have half-open ranges (which are in fact </≤ or >/≥ queries)
54 * by setting the min or max value to <code>null</code>. By setting inclusive to false, it will
55 * match all documents excluding the bounds, with inclusive on, the boundaries are hits, too.
57 public static NumericRangeFilter<Long> newLongRange(final String field, final int precisionStep,
58 Long min, Long max, final boolean minInclusive, final boolean maxInclusive
60 return new NumericRangeFilter<Long>(
61 NumericRangeQuery.newLongRange(field, precisionStep, min, max, minInclusive, maxInclusive)
66 * Factory that creates a <code>NumericRangeFilter</code>, that queries a <code>long</code>
67 * range using the default <code>precisionStep</code> {@link NumericUtils#PRECISION_STEP_DEFAULT} (4).
68 * You can have half-open ranges (which are in fact </≤ or >/≥ queries)
69 * by setting the min or max value to <code>null</code>. By setting inclusive to false, it will
70 * match all documents excluding the bounds, with inclusive on, the boundaries are hits, too.
72 public static NumericRangeFilter<Long> newLongRange(final String field,
73 Long min, Long max, final boolean minInclusive, final boolean maxInclusive
75 return new NumericRangeFilter<Long>(
76 NumericRangeQuery.newLongRange(field, min, max, minInclusive, maxInclusive)
81 * Factory that creates a <code>NumericRangeFilter</code>, that filters a <code>int</code>
82 * range using the given <a href="NumericRangeQuery.html#precisionStepDesc"><code>precisionStep</code></a>.
83 * You can have half-open ranges (which are in fact </≤ or >/≥ queries)
84 * by setting the min or max value to <code>null</code>. By setting inclusive to false, it will
85 * match all documents excluding the bounds, with inclusive on, the boundaries are hits, too.
87 public static NumericRangeFilter<Integer> newIntRange(final String field, final int precisionStep,
88 Integer min, Integer max, final boolean minInclusive, final boolean maxInclusive
90 return new NumericRangeFilter<Integer>(
91 NumericRangeQuery.newIntRange(field, precisionStep, min, max, minInclusive, maxInclusive)
96 * Factory that creates a <code>NumericRangeFilter</code>, that queries a <code>int</code>
97 * range using the default <code>precisionStep</code> {@link NumericUtils#PRECISION_STEP_DEFAULT} (4).
98 * You can have half-open ranges (which are in fact </≤ or >/≥ queries)
99 * by setting the min or max value to <code>null</code>. By setting inclusive to false, it will
100 * match all documents excluding the bounds, with inclusive on, the boundaries are hits, too.
102 public static NumericRangeFilter<Integer> newIntRange(final String field,
103 Integer min, Integer max, final boolean minInclusive, final boolean maxInclusive
105 return new NumericRangeFilter<Integer>(
106 NumericRangeQuery.newIntRange(field, min, max, minInclusive, maxInclusive)
111 * Factory that creates a <code>NumericRangeFilter</code>, that filters a <code>double</code>
112 * range using the given <a href="NumericRangeQuery.html#precisionStepDesc"><code>precisionStep</code></a>.
113 * You can have half-open ranges (which are in fact </≤ or >/≥ queries)
114 * by setting the min or max value to <code>null</code>.
115 * {@link Double#NaN} will never match a half-open range, to hit {@code NaN} use a query
116 * with {@code min == max == Double.NaN}. By setting inclusive to false, it will
117 * match all documents excluding the bounds, with inclusive on, the boundaries are hits, too.
119 public static NumericRangeFilter<Double> newDoubleRange(final String field, final int precisionStep,
120 Double min, Double max, final boolean minInclusive, final boolean maxInclusive
122 return new NumericRangeFilter<Double>(
123 NumericRangeQuery.newDoubleRange(field, precisionStep, min, max, minInclusive, maxInclusive)
128 * Factory that creates a <code>NumericRangeFilter</code>, that queries a <code>double</code>
129 * range using the default <code>precisionStep</code> {@link NumericUtils#PRECISION_STEP_DEFAULT} (4).
130 * You can have half-open ranges (which are in fact </≤ or >/≥ queries)
131 * by setting the min or max value to <code>null</code>.
132 * {@link Double#NaN} will never match a half-open range, to hit {@code NaN} use a query
133 * with {@code min == max == Double.NaN}. By setting inclusive to false, it will
134 * match all documents excluding the bounds, with inclusive on, the boundaries are hits, too.
136 public static NumericRangeFilter<Double> newDoubleRange(final String field,
137 Double min, Double max, final boolean minInclusive, final boolean maxInclusive
139 return new NumericRangeFilter<Double>(
140 NumericRangeQuery.newDoubleRange(field, min, max, minInclusive, maxInclusive)
145 * Factory that creates a <code>NumericRangeFilter</code>, that filters a <code>float</code>
146 * range using the given <a href="NumericRangeQuery.html#precisionStepDesc"><code>precisionStep</code></a>.
147 * You can have half-open ranges (which are in fact </≤ or >/≥ queries)
148 * by setting the min or max value to <code>null</code>.
149 * {@link Float#NaN} will never match a half-open range, to hit {@code NaN} use a query
150 * with {@code min == max == Float.NaN}. By setting inclusive to false, it will
151 * match all documents excluding the bounds, with inclusive on, the boundaries are hits, too.
153 public static NumericRangeFilter<Float> newFloatRange(final String field, final int precisionStep,
154 Float min, Float max, final boolean minInclusive, final boolean maxInclusive
156 return new NumericRangeFilter<Float>(
157 NumericRangeQuery.newFloatRange(field, precisionStep, min, max, minInclusive, maxInclusive)
162 * Factory that creates a <code>NumericRangeFilter</code>, that queries a <code>float</code>
163 * range using the default <code>precisionStep</code> {@link NumericUtils#PRECISION_STEP_DEFAULT} (4).
164 * You can have half-open ranges (which are in fact </≤ or >/≥ queries)
165 * by setting the min or max value to <code>null</code>.
166 * {@link Float#NaN} will never match a half-open range, to hit {@code NaN} use a query
167 * with {@code min == max == Float.NaN}. By setting inclusive to false, it will
168 * match all documents excluding the bounds, with inclusive on, the boundaries are hits, too.
170 public static NumericRangeFilter<Float> newFloatRange(final String field,
171 Float min, Float max, final boolean minInclusive, final boolean maxInclusive
173 return new NumericRangeFilter<Float>(
174 NumericRangeQuery.newFloatRange(field, min, max, minInclusive, maxInclusive)
178 /** Returns the field name for this filter */
179 public String getField() { return query.getField(); }
181 /** Returns <code>true</code> if the lower endpoint is inclusive */
182 public boolean includesMin() { return query.includesMin(); }
184 /** Returns <code>true</code> if the upper endpoint is inclusive */
185 public boolean includesMax() { return query.includesMax(); }
187 /** Returns the lower value of this range filter */
188 public T getMin() { return query.getMin(); }
190 /** Returns the upper value of this range filter */
191 public T getMax() { return query.getMax(); }
193 /** Returns the precision step. */
194 public int getPrecisionStep() { return query.getPrecisionStep(); }