Index: contrib/queryparser/src/java/org/apache/lucene/queryParser/core/config/AbstractQueryConfig.java =================================================================== --- contrib/queryparser/src/java/org/apache/lucene/queryParser/core/config/AbstractQueryConfig.java (revision 0) +++ contrib/queryparser/src/java/org/apache/lucene/queryParser/core/config/AbstractQueryConfig.java (revision 0) @@ -0,0 +1,118 @@ +package org.apache.lucene.queryParser.core.config; + +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.util.HashMap; + +/** + *
+ * This class is the base of {@link QueryConfigHandler} and {@link FieldConfig}. + * It has operations to set, unset and get configuration values. + *
+ *+ * Each configuration is is a key->value pair. The key should be an unique + * {@link ConfigurationKey} instance and it also holds the value's type. + *
+ * + * @see ConfigurationKey + */ +public abstract class AbstractQueryConfig { + + final private HashMapnull
+ *
+ * @return the value held by the given key
+ */
+ @SuppressWarnings("unchecked")
+ public null
+ * @return true if there is a value set with the given key, otherwise false
+ */
+ public null
+ * @param value
+ */
+ public null, indicating there is no configuration for that
- * field.
- *
- * @param fieldName
- * the field name
- * @return a {@link FieldConfig} object containing the field name
- * configuration or null, if the implemented
- * {@link QueryConfigHandler} has no configuration for that field
- */
- public FieldConfig getFieldConfig(CharSequence fieldName) {
- FieldConfig fieldConfig = new FieldConfig(fieldName);
-
- for (FieldConfigListener listener : this.listeners) {
- listener.buildFieldConfig(fieldConfig);
- }
-
- return fieldConfig;
-
- }
-
- /**
- * Adds a listener. The added listeners are called in the order they are
- * added.
- *
- * @param listener
- * the listener to be added
- */
- public void addFieldConfigListener(FieldConfigListener listener) {
- this.listeners.add(listener);
- }
-
-}
Index: contrib/queryparser/src/java/org/apache/lucene/queryParser/core/config/QueryConfigHandler.java
===================================================================
--- contrib/queryparser/src/java/org/apache/lucene/queryParser/core/config/QueryConfigHandler.java (revision 0)
+++ contrib/queryparser/src/java/org/apache/lucene/queryParser/core/config/QueryConfigHandler.java (revision 1096393)
@@ -0,0 +1,80 @@
+package org.apache.lucene.queryParser.core.config;
+
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import java.util.LinkedList;
+
+import org.apache.lucene.queryParser.core.processors.QueryNodeProcessor;
+import org.apache.lucene.queryParser.core.util.StringUtils;
+
+/**
+ * This class can be used to hold any query configuration and no field
+ * configuration. For field configuration, it creates an empty
+ * {@link FieldConfig} object and delegate it to field config listeners,
+ * these are responsible for setting up all the field configuration.
+ *
+ * {@link QueryConfigHandler} should be extended by classes that intends to
+ * provide configuration to {@link QueryNodeProcessor} objects.
+ *
+ * The class that extends {@link QueryConfigHandler} should also provide
+ * {@link FieldConfig} objects for each collection field.
+ *
+ * @see FieldConfig
+ * @see FieldConfigListener
+ * @see QueryConfigHandler
+ */
+public abstract class QueryConfigHandler extends AbstractQueryConfig {
+
+ final private LinkedListnull, indicating there is no configuration for that
+ * field.
+ *
+ * @param fieldName
+ * the field name
+ * @return a {@link FieldConfig} object containing the field name
+ * configuration or null, if the implemented
+ * {@link QueryConfigHandler} has no configuration for that field
+ */
+ public FieldConfig getFieldConfig(String fieldName) {
+ FieldConfig fieldConfig = new FieldConfig(StringUtils.toString(fieldName));
+
+ for (FieldConfigListener listener : this.listeners) {
+ listener.buildFieldConfig(fieldConfig);
+ }
+
+ return fieldConfig;
+
+ }
+
+ /**
+ * Adds a listener. The added listeners are called in the order they are
+ * added.
+ *
+ * @param listener
+ * the listener to be added
+ */
+ public void addFieldConfigListener(FieldConfigListener listener) {
+ this.listeners.add(listener);
+ }
+
+}
Index: contrib/queryparser/src/java/org/apache/lucene/queryParser/core/config/package.html
===================================================================
--- contrib/queryparser/src/java/org/apache/lucene/queryParser/core/config/package.html (revision 1142535)
+++ contrib/queryparser/src/java/org/apache/lucene/queryParser/core/config/package.html (working copy)
@@ -33,14 +33,7 @@
implementation.
-{@link org.apache.lucene.queryParser.core.config.FieldConfig} and {@link org.apache.lucene.queryParser.core.config.QueryConfigHandler} - should use {@link org.apache.lucene.util.Attribute} to store all attributes -required by the config implementation. See org.apache.lucene.queryParser.standard.config.*Attribute -for reference implementation. -
--The {@link org.apache.lucene.queryParser.core.config.QueryConfigHandler}, {@link org.apache.lucene.queryParser.core.config.FieldConfig}, - and {@link org.apache.lucene.util.Attribute}s are used in the processors to access config +The {@link org.apache.lucene.queryParser.core.config.QueryConfigHandler} and {@link org.apache.lucene.queryParser.core.config.FieldConfig} are used in the processors to access config information in a flexible and independent way. See {@link org.apache.lucene.queryParser.standard.processors.ParametricRangeQueryNodeProcessor} for a reference implementation. Index: contrib/queryparser/src/java/org/apache/lucene/queryParser/precedence/processors/BooleanModifiersQueryNodeProcessor.java =================================================================== --- contrib/queryparser/src/java/org/apache/lucene/queryParser/precedence/processors/BooleanModifiersQueryNodeProcessor.java (revision 1142535) +++ contrib/queryparser/src/java/org/apache/lucene/queryParser/precedence/processors/BooleanModifiersQueryNodeProcessor.java (working copy) @@ -29,8 +29,9 @@ import org.apache.lucene.queryParser.core.nodes.ModifierQueryNode.Modifier; import org.apache.lucene.queryParser.core.processors.QueryNodeProcessorImpl; import org.apache.lucene.queryParser.precedence.PrecedenceQueryParser; -import org.apache.lucene.queryParser.standard.config.DefaultOperatorAttribute; -import org.apache.lucene.queryParser.standard.config.DefaultOperatorAttribute.Operator; +import org.apache.lucene.queryParser.standard.config.StandardQueryConfigHandler; +import org.apache.lucene.queryParser.standard.config.StandardQueryConfigHandler.ConfigurationKeys; +import org.apache.lucene.queryParser.standard.config.StandardQueryConfigHandler.Operator; /** *
@@ -44,7 +45,7 @@ * if it is, the same operation when an {@link AndQueryNode} is found is applied to it. *
* - * @see DefaultOperatorAttribute + * @see StandardQueryConfigHandler.ConfigurationKeys#DEFAULT_OPERATOR * @see PrecedenceQueryParser#setDefaultOperator */ public class BooleanModifiersQueryNodeProcessor extends QueryNodeProcessorImpl { @@ -59,14 +60,14 @@ @Override public QueryNode process(QueryNode queryTree) throws QueryNodeException { - - if (!getQueryConfigHandler().hasAttribute(DefaultOperatorAttribute.class)) { + Operator op = getQueryConfigHandler().get(ConfigurationKeys.DEFAULT_OPERATOR); + + if (op == null) { throw new IllegalArgumentException( - "DefaultOperatorAttribute should be set on the QueryConfigHandler"); + "StandardQueryConfigHandler.ConfigurationKeys.DEFAULT_OPERATOR should be set on the QueryConfigHandler"); } - this.usingAnd = Operator.AND == getQueryConfigHandler().getAttribute( - DefaultOperatorAttribute.class).getOperator(); + this.usingAnd = StandardQueryConfigHandler.Operator.AND == op; return super.process(queryTree); Index: contrib/queryparser/src/java/org/apache/lucene/queryParser/standard/StandardQueryParser.java =================================================================== --- contrib/queryparser/src/java/org/apache/lucene/queryParser/standard/StandardQueryParser.java (revision 1142535) +++ contrib/queryparser/src/java/org/apache/lucene/queryParser/standard/StandardQueryParser.java (working copy) @@ -27,21 +27,10 @@ import org.apache.lucene.queryParser.core.QueryParserHelper; import org.apache.lucene.queryParser.core.config.QueryConfigHandler; import org.apache.lucene.queryParser.standard.builders.StandardQueryTreeBuilder; -import org.apache.lucene.queryParser.standard.config.AllowLeadingWildcardAttribute; -import org.apache.lucene.queryParser.standard.config.AnalyzerAttribute; -import org.apache.lucene.queryParser.standard.config.DateResolutionAttribute; -import org.apache.lucene.queryParser.standard.config.DefaultOperatorAttribute; -import org.apache.lucene.queryParser.standard.config.DefaultPhraseSlopAttribute; -import org.apache.lucene.queryParser.standard.config.FieldBoostMapAttribute; -import org.apache.lucene.queryParser.standard.config.FieldDateResolutionMapAttribute; -import org.apache.lucene.queryParser.standard.config.FuzzyAttribute; -import org.apache.lucene.queryParser.standard.config.LocaleAttribute; -import org.apache.lucene.queryParser.standard.config.LowercaseExpandedTermsAttribute; -import org.apache.lucene.queryParser.standard.config.MultiFieldAttribute; -import org.apache.lucene.queryParser.standard.config.MultiTermRewriteMethodAttribute; -import org.apache.lucene.queryParser.standard.config.PositionIncrementsAttribute; +import org.apache.lucene.queryParser.standard.config.FuzzyConfig; import org.apache.lucene.queryParser.standard.config.StandardQueryConfigHandler; -import org.apache.lucene.queryParser.standard.config.DefaultOperatorAttribute.Operator; +import org.apache.lucene.queryParser.standard.config.StandardQueryConfigHandler.ConfigurationKeys; +import org.apache.lucene.queryParser.standard.config.StandardQueryConfigHandler.Operator; import org.apache.lucene.queryParser.standard.parser.StandardSyntaxParser; import org.apache.lucene.queryParser.standard.processors.StandardQueryNodeProcessorPipeline; import org.apache.lucene.search.FuzzyQuery; @@ -179,9 +168,8 @@ * Gets implicit operator setting, which will be either {@link Operator#AND} * or {@link Operator#OR}. */ - public Operator getDefaultOperator() { - DefaultOperatorAttribute attr = getQueryConfigHandler().getAttribute(DefaultOperatorAttribute.class); - return attr.getOperator(); + public StandardQueryConfigHandler.Operator getDefaultOperator() { + return getQueryConfigHandler().get(ConfigurationKeys.DEFAULT_OPERATOR); } /** @@ -192,9 +180,8 @@ * In {@link Operator#AND} mode terms are considered to be in conjunction: the * above mentioned query is parsed ascapital AND of AND Hungary
*/
- public void setDefaultOperator(Operator operator) {
- DefaultOperatorAttribute attr = getQueryConfigHandler().getAttribute(DefaultOperatorAttribute.class);
- attr.setOperator(operator);
+ public void setDefaultOperator(StandardQueryConfigHandler.Operator operator) {
+ getQueryConfigHandler().set(ConfigurationKeys.DEFAULT_OPERATOR, operator);
}
/**
@@ -207,16 +194,22 @@
* Default: false.
*/
public void setLowercaseExpandedTerms(boolean lowercaseExpandedTerms) {
- LowercaseExpandedTermsAttribute attr = getQueryConfigHandler().getAttribute(LowercaseExpandedTermsAttribute.class);
- attr.setLowercaseExpandedTerms(lowercaseExpandedTerms);
+ getQueryConfigHandler().set(ConfigurationKeys.LOWERCASE_EXPANDED_TERMS, lowercaseExpandedTerms);
}
/**
* @see #setLowercaseExpandedTerms(boolean)
*/
public boolean getLowercaseExpandedTerms() {
- LowercaseExpandedTermsAttribute attr = getQueryConfigHandler().getAttribute(LowercaseExpandedTermsAttribute.class);
- return attr.isLowercaseExpandedTerms();
+ Boolean lowercaseExpandedTerms = getQueryConfigHandler().get(ConfigurationKeys.LOWERCASE_EXPANDED_TERMS);
+
+ if (lowercaseExpandedTerms == null) {
+ return true;
+
+ } else {
+ return lowercaseExpandedTerms;
+ }
+
}
/**
@@ -229,8 +222,7 @@
* Default: false.
*/
public void setAllowLeadingWildcard(boolean allowLeadingWildcard) {
- AllowLeadingWildcardAttribute attr = getQueryConfigHandler().getAttribute(AllowLeadingWildcardAttribute.class);
- attr.setAllowLeadingWildcard(allowLeadingWildcard);
+ getQueryConfigHandler().set(ConfigurationKeys.ALLOW_LEADING_WILDCARD, allowLeadingWildcard);
}
/**
@@ -243,16 +235,22 @@
* Default: false.
*/
public void setEnablePositionIncrements(boolean enabled) {
- PositionIncrementsAttribute attr = getQueryConfigHandler().getAttribute(PositionIncrementsAttribute.class);
- attr.setPositionIncrementsEnabled(enabled);
+ getQueryConfigHandler().set(ConfigurationKeys.ENABLE_POSITION_INCREMENTS, enabled);
}
/**
* @see #setEnablePositionIncrements(boolean)
*/
public boolean getEnablePositionIncrements() {
- PositionIncrementsAttribute attr = getQueryConfigHandler().getAttribute(PositionIncrementsAttribute.class);
- return attr.isPositionIncrementsEnabled();
+ Boolean enablePositionsIncrements = getQueryConfigHandler().get(ConfigurationKeys.ENABLE_POSITION_INCREMENTS);
+
+ if (enablePositionsIncrements == null) {
+ return false;
+
+ } else {
+ return enablePositionsIncrements;
+ }
+
}
/**
@@ -266,16 +264,14 @@
* not relevant then use this change the rewrite method.
*/
public void setMultiTermRewriteMethod(MultiTermQuery.RewriteMethod method) {
- MultiTermRewriteMethodAttribute attr = getQueryConfigHandler().getAttribute(MultiTermRewriteMethodAttribute.class);
- attr.setMultiTermRewriteMethod(method);
+ getQueryConfigHandler().set(ConfigurationKeys.MULTI_TERM_REWRITE_METHOD, method);
}
/**
* @see #setMultiTermRewriteMethod(org.apache.lucene.search.MultiTermQuery.RewriteMethod)
*/
public MultiTermQuery.RewriteMethod getMultiTermRewriteMethod() {
- MultiTermRewriteMethodAttribute attr = getQueryConfigHandler().getAttribute(MultiTermRewriteMethodAttribute.class);
- return attr.getMultiTermRewriteMethod();
+ return getQueryConfigHandler().get(ConfigurationKeys.MULTI_TERM_REWRITE_METHOD);
}
public void setMultiFields(CharSequence[] fields) {
@@ -284,8 +280,7 @@
fields = new CharSequence[0];
}
- MultiFieldAttribute attr = getQueryConfigHandler().addAttribute(MultiFieldAttribute.class);
- attr.setFields(fields);
+ getQueryConfigHandler().set(ConfigurationKeys.MULTI_FIELDS, fields);
}
@@ -296,24 +291,30 @@
* The fuzzyPrefixLength to set.
*/
public void setFuzzyPrefixLength(int fuzzyPrefixLength) {
- FuzzyAttribute attr = getQueryConfigHandler().addAttribute(FuzzyAttribute.class);
- attr.setPrefixLength(fuzzyPrefixLength);
+ QueryConfigHandler config = getQueryConfigHandler();
+ FuzzyConfig fuzzyConfig = config.get(ConfigurationKeys.FUZZY_CONFIG);
+
+ if (fuzzyConfig == null) {
+ fuzzyConfig = new FuzzyConfig();
+ config.set(ConfigurationKeys.FUZZY_CONFIG, fuzzyConfig);
+ }
+
+ fuzzyConfig.setPrefixLength(fuzzyPrefixLength);
+
}
/**
* Set locale used by date range parsing.
*/
public void setLocale(Locale locale) {
- LocaleAttribute attr = getQueryConfigHandler().addAttribute(LocaleAttribute.class);
- attr.setLocale(locale);
+ getQueryConfigHandler().set(ConfigurationKeys.LOCALE, locale);
}
/**
* Returns current locale, allowing access by subclasses.
*/
public Locale getLocale() {
- LocaleAttribute attr = getQueryConfigHandler().addAttribute(LocaleAttribute.class);
- return attr.getLocale();
+ return getQueryConfigHandler().get(ConfigurationKeys.LOCALE);
}
/**
@@ -321,40 +322,42 @@
* required. Default value is zero.
*/
public void setDefaultPhraseSlop(int defaultPhraseSlop) {
- DefaultPhraseSlopAttribute attr = getQueryConfigHandler().addAttribute(DefaultPhraseSlopAttribute.class);
- attr.setDefaultPhraseSlop(defaultPhraseSlop);
+ getQueryConfigHandler().set(ConfigurationKeys.DEFAULT_PHRASE_SLOP, defaultPhraseSlop);
}
public void setAnalyzer(Analyzer analyzer) {
- AnalyzerAttribute attr = getQueryConfigHandler().getAttribute(AnalyzerAttribute.class);
- attr.setAnalyzer(analyzer);
+ getQueryConfigHandler().set(ConfigurationKeys.ANALYZER, analyzer);
}
public Analyzer getAnalyzer() {
- QueryConfigHandler config = this.getQueryConfigHandler();
-
- if ( config.hasAttribute(AnalyzerAttribute.class)) {
- AnalyzerAttribute attr = config.getAttribute(AnalyzerAttribute.class);
- return attr.getAnalyzer();
- }
-
- return null;
+ return getQueryConfigHandler().get(ConfigurationKeys.ANALYZER);
}
/**
* @see #setAllowLeadingWildcard(boolean)
*/
public boolean getAllowLeadingWildcard() {
- AllowLeadingWildcardAttribute attr = getQueryConfigHandler().addAttribute(AllowLeadingWildcardAttribute.class);
- return attr.isAllowLeadingWildcard();
+ Boolean allowLeadingWildcard = getQueryConfigHandler().get(ConfigurationKeys.ALLOW_LEADING_WILDCARD);
+
+ if (allowLeadingWildcard == null) {
+ return false;
+
+ } else {
+ return allowLeadingWildcard;
+ }
}
/**
* Get the minimal similarity for fuzzy queries.
*/
public float getFuzzyMinSim() {
- FuzzyAttribute attr = getQueryConfigHandler().addAttribute(FuzzyAttribute.class);
- return attr.getFuzzyMinSimilarity();
+ FuzzyConfig fuzzyConfig = getQueryConfigHandler().get(ConfigurationKeys.FUZZY_CONFIG);
+
+ if (fuzzyConfig == null) {
+ return FuzzyQuery.defaultMinSimilarity;
+ } else {
+ return fuzzyConfig.getMinSimilarity();
+ }
}
/**
@@ -363,16 +366,27 @@
* @return Returns the fuzzyPrefixLength.
*/
public int getFuzzyPrefixLength() {
- FuzzyAttribute attr = getQueryConfigHandler().addAttribute(FuzzyAttribute.class);
- return attr.getPrefixLength();
+ FuzzyConfig fuzzyConfig = getQueryConfigHandler().get(ConfigurationKeys.FUZZY_CONFIG);
+
+ if (fuzzyConfig == null) {
+ return FuzzyQuery.defaultPrefixLength;
+ } else {
+ return fuzzyConfig.getPrefixLength();
+ }
}
/**
* Gets the default slop for phrases.
*/
public int getPhraseSlop() {
- DefaultPhraseSlopAttribute attr = getQueryConfigHandler().addAttribute(DefaultPhraseSlopAttribute.class);
- return attr.getDefaultPhraseSlop();
+ Integer phraseSlop = getQueryConfigHandler().get(ConfigurationKeys.DEFAULT_PHRASE_SLOP);
+
+ if (phraseSlop == null) {
+ return 0;
+
+ } else {
+ return phraseSlop;
+ }
}
/**
@@ -380,23 +394,27 @@
* {@link FuzzyQuery#defaultMinSimilarity}.
*/
public void setFuzzyMinSim(float fuzzyMinSim) {
- FuzzyAttribute attr = getQueryConfigHandler().addAttribute(FuzzyAttribute.class);
- attr.setFuzzyMinSimilarity(fuzzyMinSim);
+ QueryConfigHandler config = getQueryConfigHandler();
+ FuzzyConfig fuzzyConfig = config.get(ConfigurationKeys.FUZZY_CONFIG);
+
+ if (fuzzyConfig == null) {
+ fuzzyConfig = new FuzzyConfig();
+ config.set(ConfigurationKeys.FUZZY_CONFIG, fuzzyConfig);
+ }
+
+ fuzzyConfig.setMinSimilarity(fuzzyMinSim);
}
public void setFieldsBoost(Map