1 package org.apache.lucene.queryParser.standard.config;
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.util.HashMap;
22 import java.util.LinkedHashMap;
23 import java.util.Locale;
26 import org.apache.lucene.analysis.Analyzer;
27 import org.apache.lucene.document.DateTools;
28 import org.apache.lucene.document.DateTools.Resolution;
29 import org.apache.lucene.queryParser.core.config.ConfigurationKey;
30 import org.apache.lucene.queryParser.core.config.FieldConfig;
31 import org.apache.lucene.queryParser.core.config.QueryConfigHandler;
32 import org.apache.lucene.queryParser.standard.StandardQueryParser;
33 import org.apache.lucene.queryParser.standard.processors.StandardQueryNodeProcessorPipeline;
34 import org.apache.lucene.search.MultiTermQuery;
35 import org.apache.lucene.search.TermRangeQuery;
36 import org.apache.lucene.search.MultiTermQuery.RewriteMethod;
39 * This query configuration handler is used for almost every processor defined
40 * in the {@link StandardQueryNodeProcessorPipeline} processor pipeline. It
41 * holds attributes that reproduces the configuration that could be set on the
42 * old lucene 2.4 QueryParser class. <br/>
44 * @see StandardQueryNodeProcessorPipeline
46 public class StandardQueryConfigHandler extends QueryConfigHandler {
48 final public static class ConfigurationKeys {
51 * Key used to set whether position increments is enabled
53 * @see StandardQueryParser#setEnablePositionIncrements(boolean)
54 * @see StandardQueryParser#getEnablePositionIncrements()
56 final public static ConfigurationKey<Boolean> ENABLE_POSITION_INCREMENTS = ConfigurationKey
60 * Key used to set whether expanded terms should be expanded
62 * @see StandardQueryParser#setLowercaseExpandedTerms(boolean)
63 * @see StandardQueryParser#getLowercaseExpandedTerms()
65 final public static ConfigurationKey<Boolean> LOWERCASE_EXPANDED_TERMS = ConfigurationKey
69 * Key used to set whether leading wildcards are supported
71 * @see StandardQueryParser#setAllowLeadingWildcard(boolean)
72 * @see StandardQueryParser#getAllowLeadingWildcard()
74 final public static ConfigurationKey<Boolean> ALLOW_LEADING_WILDCARD = ConfigurationKey
78 * Key used to set the {@link Analyzer} used for terms found in the query
80 * @see StandardQueryParser#setAnalyzer(Analyzer)
81 * @see StandardQueryParser#getAnalyzer()
83 final public static ConfigurationKey<Analyzer> ANALYZER = ConfigurationKey
87 * Key used to set the default boolean operator
89 * @see StandardQueryParser#setDefaultOperator(org.apache.lucene.queryParser.standard.config.StandardQueryConfigHandler.Operator)
90 * @see StandardQueryParser#getDefaultOperator()
92 final public static ConfigurationKey<Operator> DEFAULT_OPERATOR = ConfigurationKey
96 * Key used to set the default phrase slop
98 * @see StandardQueryParser#setPhraseSlop(int)
99 * @see StandardQueryParser#getPhraseSlop()
101 final public static ConfigurationKey<Integer> PHRASE_SLOP = ConfigurationKey
105 * Key used to set the {@link Locale} used when parsing the query
107 * @see StandardQueryParser#setLocale(Locale)
108 * @see StandardQueryParser#getLocale()
110 final public static ConfigurationKey<Locale> LOCALE = ConfigurationKey
114 * Key used to set the {@link RewriteMethod} used when creating queries
116 * @see StandardQueryParser#setMultiTermRewriteMethod(org.apache.lucene.search.MultiTermQuery.RewriteMethod)
117 * @see StandardQueryParser#getMultiTermRewriteMethod()
119 final public static ConfigurationKey<MultiTermQuery.RewriteMethod> MULTI_TERM_REWRITE_METHOD = ConfigurationKey
123 * Key used to set the fields a query should be expanded to when the field
124 * is <code>null</code>
126 * @see StandardQueryParser#setMultiFields(CharSequence[])
127 * @see StandardQueryParser#getMultiFields(CharSequence[])
129 final public static ConfigurationKey<CharSequence[]> MULTI_FIELDS = ConfigurationKey
133 * Key used to set a field to boost map that is used to set the boost for
136 * @see StandardQueryParser#setFieldsBoost(Map)
137 * @see StandardQueryParser#getFieldsBoost()
139 final public static ConfigurationKey<Map<String, Float>> FIELD_BOOST_MAP = ConfigurationKey
143 * Key used to set a field to {@link Resolution} map that is used to
144 * normalize each date field value.
146 * @see StandardQueryParser#setDateResolutionMap(Map)
147 * @see StandardQueryParser#getDateResolutionMap()
149 final public static ConfigurationKey<Map<CharSequence, DateTools.Resolution>> FIELD_DATE_RESOLUTION_MAP = ConfigurationKey
153 * Key used to set the {@link FuzzyConfig} used to create fuzzy queries.
155 * @see StandardQueryParser#setFuzzyMinSim(float)
156 * @see StandardQueryParser#setFuzzyPrefixLength(int)
157 * @see StandardQueryParser#getFuzzyMinSim()
158 * @see StandardQueryParser#getFuzzyPrefixLength()
160 final public static ConfigurationKey<FuzzyConfig> FUZZY_CONFIG = ConfigurationKey
164 * Key used to set default {@link Resolution}.
166 * @see StandardQueryParser#setDateResolution(org.apache.lucene.document.DateTools.Resolution)
167 * @see StandardQueryParser#getDateResolution()
169 final public static ConfigurationKey<DateTools.Resolution> DATE_RESOLUTION = ConfigurationKey
173 * Key used to set the boost value in {@link FieldConfig} objects.
175 * @see StandardQueryParser#setFieldsBoost(Map)
176 * @see StandardQueryParser#getFieldsBoost()
178 final public static ConfigurationKey<Float> BOOST = ConfigurationKey
182 * Key used to set a field to its {@link NumericConfig}.
184 * @see StandardQueryParser#setNumericConfigMap(Map)
185 * @see StandardQueryParser#getNumericConfigMap()
187 final public static ConfigurationKey<NumericConfig> NUMERIC_CONFIG = ConfigurationKey
191 * Key used to set the {@link NumericConfig} in {@link FieldConfig} for
194 * @see StandardQueryParser#setNumericConfigMap(Map)
195 * @see StandardQueryParser#getNumericConfigMap()
197 final public static ConfigurationKey<Map<String,NumericConfig>> NUMERIC_CONFIG_MAP = ConfigurationKey
202 * Key used to set the {@link Collator} used when creating {@link TermRangeQuery}s.
204 * @see StandardQueryParser#setRangeCollator(Collator)
205 * @see StandardQueryParser#getRangeCollator()
207 final public static ConfigurationKey<Collator> RANGE_COLLATOR = ConfigurationKey
212 public static enum Operator {
216 @SuppressWarnings("deprecation")
217 public StandardQueryConfigHandler() {
218 // Add listener that will build the FieldConfig.
219 addFieldConfigListener(new FieldBoostMapFCListener(this));
220 addFieldConfigListener(new FieldDateResolutionFCListener(this));
221 addFieldConfigListener(new NumericFieldConfigListener(this));
223 // uncomment code below when deprecated query parser attributes are removed
225 // set(ConfigurationKeys.ALLOW_LEADING_WILDCARD, false); // default in 2.9
226 // set(ConfigurationKeys.ANALYZER, null); // default value 2.4
227 // set(ConfigurationKeys.DEFAULT_OPERATOR, Operator.OR);
228 // set(ConfigurationKeys.PHRASE_SLOP, 0); // default value 2.4
229 // set(ConfigurationKeys.LOWERCASE_EXPANDED_TERMS, true); // default value
231 // set(ConfigurationKeys.ENABLE_POSITION_INCREMENTS, false); // default
234 // set(ConfigurationKeys.FIELD_BOOST_MAP, new LinkedHashMap<String,
236 // set(ConfigurationKeys.FUZZY_CONFIG, new FuzzyConfig());
237 // set(ConfigurationKeys.LOCALE, Locale.getDefault());
238 // set(ConfigurationKeys.MULTI_TERM_REWRITE_METHOD,
239 // MultiTermQuery.CONSTANT_SCORE_AUTO_REWRITE_DEFAULT);
240 // set(ConfigurationKeys.FIELD_DATE_RESOLUTION_MAP,
241 // new HashMap<CharSequence, DateTools.Resolution>());
243 // still add old attributes
244 addAttribute(RangeCollatorAttribute.class);
245 addAttribute(DefaultOperatorAttribute.class);
246 addAttribute(AnalyzerAttribute.class);
247 addAttribute(FuzzyAttribute.class);
248 addAttribute(LowercaseExpandedTermsAttribute.class);
249 addAttribute(MultiTermRewriteMethodAttribute.class);
250 addAttribute(AllowLeadingWildcardAttribute.class);
251 addAttribute(PositionIncrementsAttribute.class);
252 addAttribute(LocaleAttribute.class);
253 addAttribute(DefaultPhraseSlopAttribute.class);
254 addAttribute(MultiTermRewriteMethodAttribute.class);
255 addAttribute(FieldBoostMapAttribute.class);