1
package org.apache.lucene.xmlparser.builders;
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.search.NumericRangeQuery;
21
import org.apache.lucene.search.Query;
22
import org.apache.lucene.util.NumericUtils;
23
import org.apache.lucene.xmlparser.DOMUtils;
24
import org.apache.lucene.xmlparser.ParserException;
25
import org.apache.lucene.xmlparser.QueryBuilder;
26
import org.w3c.dom.Element;
29
* Creates a {@link NumericRangeQuery}. The table below specifies the required
30
* attributes and the defaults if optional attributes are omitted. For more
31
* detail on what each of the attributes actually do, consult the documentation
32
* for {@link NumericRangeQuery}:
35
* <th>Attribute name</th>
48
* <td>Specified by <tt>type</tt></td>
54
* <td>Specified by <tt>type</tt></td>
60
* <td>int, long, float, double</td>
65
* <td>includeLower</td>
66
* <td>true, false</td>
71
* <td>includeUpper</td>
72
* <td>true, false</td>
77
* <td>precisionStep</td>
84
* A {@link ParserException} will be thrown if an error occurs parsing the
85
* supplied <tt>lowerTerm</tt> or <tt>upperTerm</tt> into the numeric type
86
* specified by <tt>type</tt>.
88
public class NumericRangeQueryBuilder implements QueryBuilder {
90
public Query getQuery(Element e) throws ParserException {
91
String field = DOMUtils.getAttributeWithInheritanceOrFail(e, "fieldName");
92
String lowerTerm = DOMUtils.getAttributeOrFail(e, "lowerTerm");
93
String upperTerm = DOMUtils.getAttributeOrFail(e, "upperTerm");
94
boolean lowerInclusive = DOMUtils.getAttribute(e, "includeLower", true);
95
boolean upperInclusive = DOMUtils.getAttribute(e, "includeUpper", true);
96
int precisionStep = DOMUtils.getAttribute(e, "precisionStep", NumericUtils.PRECISION_STEP_DEFAULT);
98
String type = DOMUtils.getAttribute(e, "type", "int");
101
if (type.equalsIgnoreCase("int")) {
102
filter = NumericRangeQuery.newIntRange(field, precisionStep, Integer
103
.valueOf(lowerTerm), Integer.valueOf(upperTerm), lowerInclusive,
105
} else if (type.equalsIgnoreCase("long")) {
106
filter = NumericRangeQuery.newLongRange(field, precisionStep, Long
107
.valueOf(lowerTerm), Long.valueOf(upperTerm), lowerInclusive,
109
} else if (type.equalsIgnoreCase("double")) {
110
filter = NumericRangeQuery.newDoubleRange(field, precisionStep, Double
111
.valueOf(lowerTerm), Double.valueOf(upperTerm), lowerInclusive,
113
} else if (type.equalsIgnoreCase("float")) {
114
filter = NumericRangeQuery.newFloatRange(field, precisionStep, Float
115
.valueOf(lowerTerm), Float.valueOf(upperTerm), lowerInclusive,
118
throw new ParserException(
119
"type attribute must be one of: [long, int, double, float]");
122
} catch (NumberFormatException nfe) {
123
throw new ParserException(
124
"Could not parse lowerTerm or upperTerm into a number", nfe);