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.FieldQueryNode;
31 import org.apache.lucene.queryParser.core.nodes.ParametricQueryNode;
32 import org.apache.lucene.queryParser.core.nodes.QueryNode;
33 import org.apache.lucene.queryParser.core.processors.QueryNodeProcessorImpl;
34 import org.apache.lucene.queryParser.standard.config.NumericConfig;
35 import org.apache.lucene.queryParser.standard.config.StandardQueryConfigHandler.ConfigurationKeys;
36 import org.apache.lucene.queryParser.standard.nodes.NumericQueryNode;
37 import org.apache.lucene.queryParser.standard.nodes.NumericRangeQueryNode;
40 * This processor is used to convert {@link FieldQueryNode}s to
41 * {@link NumericRangeQueryNode}s. It looks for
42 * {@link ConfigurationKeys#NUMERIC_CONFIG} set in the {@link FieldConfig} of
43 * every {@link FieldQueryNode} found. If
44 * {@link ConfigurationKeys#NUMERIC_CONFIG} is found, it considers that
45 * {@link FieldQueryNode} to be a numeric query and convert it to
46 * {@link NumericRangeQueryNode} with upper and lower inclusive and lower and
47 * upper equals to the value represented by the {@link FieldQueryNode} converted
48 * to {@link Number}. It means that <b>field:1</b> is converted to <b>field:[1 TO
51 * Note that {@link ParametricQueryNode}s are ignored, even being a
52 * {@link FieldQueryNode}.
54 * @see ConfigurationKeys#NUMERIC_CONFIG
57 * @see NumericQueryNode
59 public class NumericQueryNodeProcessor extends QueryNodeProcessorImpl {
62 * Constructs a {@link NumericQueryNodeProcessor} object.
64 public NumericQueryNodeProcessor() {
69 protected QueryNode postProcessNode(QueryNode node) throws QueryNodeException {
71 if (node instanceof FieldQueryNode
72 && !(node instanceof ParametricQueryNode)) {
74 QueryConfigHandler config = getQueryConfigHandler();
77 FieldQueryNode fieldNode = (FieldQueryNode) node;
78 FieldConfig fieldConfig = config.getFieldConfig(fieldNode
81 if (fieldConfig != null) {
82 NumericConfig numericConfig = fieldConfig
83 .get(ConfigurationKeys.NUMERIC_CONFIG);
85 if (numericConfig != null) {
87 NumberFormat numberFormat = numericConfig.getNumberFormat();
91 number = numberFormat.parse(fieldNode.getTextAsString());
93 } catch (ParseException e) {
94 throw new QueryNodeParseException(new MessageImpl(
95 QueryParserMessages.COULD_NOT_PARSE_NUMBER, fieldNode
96 .getTextAsString(), numberFormat.getClass()
97 .getCanonicalName()), e);
100 switch (numericConfig.getType()) {
102 number = number.longValue();
105 number = number.intValue();
108 number = number.doubleValue();
111 number = number.floatValue();
114 NumericQueryNode lowerNode = new NumericQueryNode(fieldNode
115 .getField(), number, numberFormat);
116 NumericQueryNode upperNode = new NumericQueryNode(fieldNode
117 .getField(), number, numberFormat);
119 return new NumericRangeQueryNode(lowerNode, upperNode, true, true,
135 protected QueryNode preProcessNode(QueryNode node) throws QueryNodeException {
140 protected List<QueryNode> setChildrenOrder(List<QueryNode> children)
141 throws QueryNodeException {