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.Collator;
21 import java.text.DateFormat;
22 import java.util.Calendar;
23 import java.util.Date;
24 import java.util.List;
25 import java.util.Locale;
27 import org.apache.lucene.document.DateField;
28 import org.apache.lucene.document.DateTools;
29 import org.apache.lucene.document.DateTools.Resolution;
30 import org.apache.lucene.queryParser.core.QueryNodeException;
31 import org.apache.lucene.queryParser.core.config.FieldConfig;
32 import org.apache.lucene.queryParser.core.config.QueryConfigHandler;
33 import org.apache.lucene.queryParser.core.nodes.ParametricQueryNode;
34 import org.apache.lucene.queryParser.core.nodes.ParametricRangeQueryNode;
35 import org.apache.lucene.queryParser.core.nodes.QueryNode;
36 import org.apache.lucene.queryParser.core.nodes.ParametricQueryNode.CompareOperator;
37 import org.apache.lucene.queryParser.core.processors.QueryNodeProcessorImpl;
38 import org.apache.lucene.queryParser.standard.config.StandardQueryConfigHandler.ConfigurationKeys;
39 import org.apache.lucene.queryParser.standard.nodes.RangeQueryNode;
42 * This processor converts {@link ParametricRangeQueryNode} objects to
43 * {@link RangeQueryNode} objects. It reads the lower and upper bounds value
44 * from the {@link ParametricRangeQueryNode} object and try to parse their
45 * values using a {@link DateFormat}. If the values cannot be parsed to a date
46 * value, it will only create the {@link RangeQueryNode} using the
47 * non-parsed values. <br/>
49 * If a {@link ConfigurationKeys#LOCALE} is defined in the {@link QueryConfigHandler} it
50 * will be used to parse the date, otherwise {@link Locale#getDefault()} will be
53 * If a {@link ConfigurationKeys#DATE_RESOLUTION} is defined and the {@link Resolution} is
54 * not <code>null</code> it will also be used to parse the date value. <br/>
57 * @see ConfigurationKeys#DATE_RESOLUTION
58 * @see ConfigurationKeys#LOCALE
59 * @see org.apache.lucene.queryParser.standard.nodes.TermRangeQueryNode
60 * @see ParametricRangeQueryNode
62 public class ParametricRangeQueryNodeProcessor extends QueryNodeProcessorImpl {
64 public ParametricRangeQueryNodeProcessor() {
69 protected QueryNode postProcessNode(QueryNode node) throws QueryNodeException {
71 if (node instanceof ParametricRangeQueryNode) {
72 ParametricRangeQueryNode parametricRangeNode = (ParametricRangeQueryNode) node;
73 ParametricQueryNode upper = parametricRangeNode.getUpperBound();
74 ParametricQueryNode lower = parametricRangeNode.getLowerBound();
75 DateTools.Resolution dateRes = null;
76 boolean inclusive = false;
78 Locale locale = getQueryConfigHandler().get(ConfigurationKeys.LOCALE,
81 Collator collator = getQueryConfigHandler().get(
82 ConfigurationKeys.RANGE_COLLATOR);
85 CharSequence field = parametricRangeNode.getField();
86 String fieldStr = null;
89 fieldStr = field.toString();
92 FieldConfig fieldConfig = getQueryConfigHandler()
93 .getFieldConfig(fieldStr);
95 if (fieldConfig != null) {
96 dateRes = fieldConfig.get(ConfigurationKeys.DATE_RESOLUTION);
99 if (upper.getOperator() == CompareOperator.LE) {
102 } else if (lower.getOperator() == CompareOperator.GE) {
106 String part1 = lower.getTextAsString();
107 String part2 = upper.getTextAsString();
110 DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale);
112 Date d1 = df.parse(part1);
113 Date d2 = df.parse(part2);
115 // The user can only specify the date, not the time, so make sure
116 // the time is set to the latest possible time of that date to really
117 // include all documents:
118 Calendar cal = Calendar.getInstance(locale);
120 cal.set(Calendar.HOUR_OF_DAY, 23);
121 cal.set(Calendar.MINUTE, 59);
122 cal.set(Calendar.SECOND, 59);
123 cal.set(Calendar.MILLISECOND, 999);
127 if (dateRes == null) {
128 // no default or field specific date resolution has been set,
129 // use deprecated DateField to maintain compatibilty with
130 // pre-1.9 Lucene versions.
131 part1 = DateField.dateToString(d1);
132 part2 = DateField.dateToString(d2);
135 part1 = DateTools.dateToString(d1, dateRes);
136 part2 = DateTools.dateToString(d2, dateRes);
138 } catch (Exception e) {
142 lower.setText(part1);
143 upper.setText(part2);
145 return new RangeQueryNode(lower, upper, collator);
154 protected QueryNode preProcessNode(QueryNode node) throws QueryNodeException {
161 protected List<QueryNode> setChildrenOrder(List<QueryNode> children)
162 throws QueryNodeException {