1 package org.apache.lucene.queryParser.standard.processors;
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 java.text.NumberFormat;
21 import java.text.ParseException;
22 import java.util.List;
24 import org.apache.lucene.messages.MessageImpl;
25 import org.apache.lucene.queryParser.core.QueryNodeException;
26 import org.apache.lucene.queryParser.core.QueryNodeParseException;
27 import org.apache.lucene.queryParser.core.config.FieldConfig;
28 import org.apache.lucene.queryParser.core.config.QueryConfigHandler;
29 import org.apache.lucene.queryParser.core.messages.QueryParserMessages;
30 import org.apache.lucene.queryParser.core.nodes.ParametricQueryNode;
31 import org.apache.lucene.queryParser.core.nodes.ParametricRangeQueryNode;
32 import org.apache.lucene.queryParser.core.nodes.QueryNode;
33 import org.apache.lucene.queryParser.core.nodes.ParametricQueryNode.CompareOperator;
34 import org.apache.lucene.queryParser.core.processors.QueryNodeProcessorImpl;
35 import org.apache.lucene.queryParser.core.util.StringUtils;
36 import org.apache.lucene.queryParser.standard.config.NumericConfig;
37 import org.apache.lucene.queryParser.standard.config.StandardQueryConfigHandler.ConfigurationKeys;
38 import org.apache.lucene.queryParser.standard.nodes.NumericQueryNode;
39 import org.apache.lucene.queryParser.standard.nodes.NumericRangeQueryNode;
42 * This processor is used to convert {@link ParametricRangeQueryNode}s to
43 * {@link NumericRangeQueryNode}s. It looks for
44 * {@link ConfigurationKeys#NUMERIC_CONFIG} set in the {@link FieldConfig} of
45 * every {@link ParametricRangeQueryNode} found. If
46 * {@link ConfigurationKeys#NUMERIC_CONFIG} is found, it considers that
47 * {@link ParametricRangeQueryNode} to be a numeric range query and convert it to
48 * {@link NumericRangeQueryNode}.
50 * @see ConfigurationKeys#NUMERIC_CONFIG
51 * @see ParametricRangeQueryNode
53 * @see NumericRangeQueryNode
55 public class NumericRangeQueryNodeProcessor extends QueryNodeProcessorImpl {
58 * Constructs an empty {@link NumericRangeQueryNode} object.
60 public NumericRangeQueryNodeProcessor() {
65 protected QueryNode postProcessNode(QueryNode node) throws QueryNodeException {
67 if (node instanceof ParametricRangeQueryNode) {
68 QueryConfigHandler config = getQueryConfigHandler();
71 ParametricRangeQueryNode parametricRangeNode = (ParametricRangeQueryNode) node;
72 FieldConfig fieldConfig = config.getFieldConfig(StringUtils
73 .toString(parametricRangeNode.getField()));
75 if (fieldConfig != null) {
77 NumericConfig numericConfig = fieldConfig
78 .get(ConfigurationKeys.NUMERIC_CONFIG);
80 if (numericConfig != null) {
82 ParametricQueryNode lower = parametricRangeNode.getLowerBound();
83 ParametricQueryNode upper = parametricRangeNode.getUpperBound();
85 NumberFormat numberFormat = numericConfig.getNumberFormat();
86 Number lowerNumber, upperNumber;
89 lowerNumber = numberFormat.parse(lower.getTextAsString());
91 } catch (ParseException e) {
92 throw new QueryNodeParseException(new MessageImpl(
93 QueryParserMessages.COULD_NOT_PARSE_NUMBER, lower
94 .getTextAsString(), numberFormat.getClass()
95 .getCanonicalName()), e);
99 upperNumber = numberFormat.parse(upper.getTextAsString());
101 } catch (ParseException e) {
102 throw new QueryNodeParseException(new MessageImpl(
103 QueryParserMessages.COULD_NOT_PARSE_NUMBER, upper
104 .getTextAsString(), numberFormat.getClass()
105 .getCanonicalName()), e);
108 switch (numericConfig.getType()) {
110 upperNumber = upperNumber.longValue();
111 lowerNumber = lowerNumber.longValue();
114 upperNumber = upperNumber.intValue();
115 lowerNumber = lowerNumber.intValue();
118 upperNumber = upperNumber.doubleValue();
119 lowerNumber = lowerNumber.doubleValue();
122 upperNumber = upperNumber.floatValue();
123 lowerNumber = lowerNumber.floatValue();
126 NumericQueryNode lowerNode = new NumericQueryNode(
127 parametricRangeNode.getField(), lowerNumber, numberFormat);
128 NumericQueryNode upperNode = new NumericQueryNode(
129 parametricRangeNode.getField(), upperNumber, numberFormat);
131 boolean upperInclusive = upper.getOperator() == CompareOperator.LE;
132 boolean lowerInclusive = lower.getOperator() == CompareOperator.GE;
134 return new NumericRangeQueryNode(lowerNode, upperNode,
135 lowerInclusive, upperInclusive, numericConfig);
150 protected QueryNode preProcessNode(QueryNode node) throws QueryNodeException {
155 protected List<QueryNode> setChildrenOrder(List<QueryNode> children)
156 throws QueryNodeException {