X-Git-Url: https://git.mdrn.pl/pylucene.git/blobdiff_plain/a2e61f0c04805cfcb8706176758d1283c7e3a55c..aaeed5504b982cf3545252ab528713250aa33eed:/lucene-java-3.5.0/lucene/contrib/queryparser/src/java/org/apache/lucene/queryParser/standard/StandardQueryParser.java diff --git a/lucene-java-3.5.0/lucene/contrib/queryparser/src/java/org/apache/lucene/queryParser/standard/StandardQueryParser.java b/lucene-java-3.5.0/lucene/contrib/queryparser/src/java/org/apache/lucene/queryParser/standard/StandardQueryParser.java new file mode 100644 index 0000000..d0d1789 --- /dev/null +++ b/lucene-java-3.5.0/lucene/contrib/queryparser/src/java/org/apache/lucene/queryParser/standard/StandardQueryParser.java @@ -0,0 +1,654 @@ +package org.apache.lucene.queryParser.standard; + +/** + * 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.text.Collator; +import java.util.Locale; +import java.util.Map; +import java.util.TooManyListenersException; + +import org.apache.lucene.analysis.Analyzer; +import org.apache.lucene.document.DateTools.Resolution; +import org.apache.lucene.document.DateTools; +import org.apache.lucene.queryParser.core.QueryNodeException; +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.FuzzyConfig; +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.NumericConfig; +import org.apache.lucene.queryParser.standard.config.PositionIncrementsAttribute; +import org.apache.lucene.queryParser.standard.config.RangeCollatorAttribute; +import org.apache.lucene.queryParser.standard.config.StandardQueryConfigHandler; +import org.apache.lucene.queryParser.standard.config.StandardQueryConfigHandler.Operator; +import org.apache.lucene.queryParser.standard.config.StandardQueryConfigHandler.ConfigurationKeys; +import org.apache.lucene.queryParser.standard.nodes.RangeQueryNode; +import org.apache.lucene.queryParser.standard.parser.StandardSyntaxParser; +import org.apache.lucene.queryParser.standard.processors.StandardQueryNodeProcessorPipeline; +import org.apache.lucene.search.FuzzyQuery; +import org.apache.lucene.search.MultiTermQuery; +import org.apache.lucene.search.Query; + +/** + *
+ * This class is a helper that enables users to easily use the Lucene query + * parser. + *
+ *+ * To construct a Query object from a query string, use the + * {@link #parse(String, String)} method: + *
+ * To change any configuration before parsing the query string do, for example: + *
+ *+ * The syntax for query strings is as follows (copied from the old QueryParser + * javadoc): + *
+
) or a minus (-
) sign, indicating that
+ * the clause is required or prohibited respectively; or
+ * +
/-
prefix to require any of a set of terms.
+ * + * Query ::= ( Clause )* + * Clause ::= ["+", "-"] [<TERM> ":"] ( <TERM> | "(" Query ")" ) + *+ * + *
+ * Examples of appropriately formatted queries can be found in the query syntax + * documentation. + *
+ * + *+ * The text parser used by this helper is a {@link StandardSyntaxParser}. + *
+ *+ * The query node processor used by this helper is a + * {@link StandardQueryNodeProcessorPipeline}. + *
+ *+ * The builder used by this helper is a {@link StandardQueryTreeBuilder}. + *
+ * + * @see StandardQueryParser + * @see StandardQueryConfigHandler + * @see StandardSyntaxParser + * @see StandardQueryNodeProcessorPipeline + * @see StandardQueryTreeBuilder + */ +public class StandardQueryParser extends QueryParserHelper { + + /** + * Constructs a {@link StandardQueryParser} object. + */ + public StandardQueryParser() { + super(new StandardQueryConfigHandler(), new StandardSyntaxParser(), + new StandardQueryNodeProcessorPipeline(null), + new StandardQueryTreeBuilder()); + } + + /** + * Constructs a {@link StandardQueryParser} object and sets an + * {@link Analyzer} to it. The same as: + * + *capital of Hungary
is equal to
+ * capital OR of OR Hungary
.capital AND of AND Hungary
+ *
+ * @deprecated
+ */
+ @Deprecated
+ public void setDefaultOperator(DefaultOperatorAttribute.Operator operator) {
+ DefaultOperatorAttribute attr = getQueryConfigHandler().getAttribute(DefaultOperatorAttribute.class);
+ attr.setOperator(operator);
+ }
+
+ /**
+ * Sets the boolean operator of the QueryParser. In default mode (
+ * {@link Operator#OR}) terms without any modifiers are considered optional:
+ * for example capital of Hungary
is equal to
+ * capital OR of OR Hungary
.capital AND of AND Hungary
+ */
+ public void setDefaultOperator(
+ org.apache.lucene.queryParser.standard.config.StandardQueryConfigHandler.Operator operator) {
+
+ DefaultOperatorAttribute.Operator attrOperator;
+
+ if (operator == org.apache.lucene.queryParser.standard.config.StandardQueryConfigHandler.Operator.AND) {
+ attrOperator = DefaultOperatorAttribute.Operator.AND;
+ } else {
+ attrOperator = DefaultOperatorAttribute.Operator.OR;
+ }
+
+ setDefaultOperator(attrOperator);
+
+ // uncomment code below when deprecated query parser attributes are removed
+ // getQueryConfigHandler().set(ConfigurationKeys.DEFAULT_OPERATOR, operator);
+ }
+
+ /**
+ * Set to true
to allow leading wildcard characters.
+ *
+ * When set, *
or ?
are allowed as the first
+ * character of a PrefixQuery and WildcardQuery. Note that this can produce
+ * very slow queries on big indexes.
+ *
+ * Default: false.
+ */
+ public void setLowercaseExpandedTerms(boolean lowercaseExpandedTerms) {
+ LowercaseExpandedTermsAttribute attr = getQueryConfigHandler()
+ .getAttribute(LowercaseExpandedTermsAttribute.class);
+ attr.setLowercaseExpandedTerms(lowercaseExpandedTerms);
+ // uncomment code below when deprecated query parser attributes are removed
+ // getQueryConfigHandler().set(ConfigurationKeys.LOWERCASE_EXPANDED_TERMS,
+ // lowercaseExpandedTerms);
+ }
+
+ /**
+ * @see #setLowercaseExpandedTerms(boolean)
+ */
+ public boolean getLowercaseExpandedTerms() {
+ Boolean lowercaseExpandedTerms = getQueryConfigHandler().get(
+ ConfigurationKeys.LOWERCASE_EXPANDED_TERMS);
+
+ if (lowercaseExpandedTerms == null) {
+ return true;
+
+ } else {
+ return lowercaseExpandedTerms;
+ }
+
+ }
+
+ /**
+ * Set to true
to allow leading wildcard characters.
+ *
+ * When set, *
or ?
are allowed as the first
+ * character of a PrefixQuery and WildcardQuery. Note that this can produce
+ * very slow queries on big indexes.
+ *
+ * Default: false.
+ */
+ public void setAllowLeadingWildcard(boolean allowLeadingWildcard) {
+ AllowLeadingWildcardAttribute attr = getQueryConfigHandler().getAttribute(
+ AllowLeadingWildcardAttribute.class);
+ attr.setAllowLeadingWildcard(allowLeadingWildcard);
+ // uncomment code below when deprecated query parser attributes are removed
+ // getQueryConfigHandler().set(ConfigurationKeys.ALLOW_LEADING_WILDCARD,
+ // allowLeadingWildcard);
+ }
+
+ /**
+ * Set to true
to enable position increments in result query.
+ *
+ * When set, result phrase and multi-phrase queries will be aware of position + * increments. Useful when e.g. a StopFilter increases the position increment + * of the token that follows an omitted token. + *
+ * Default: false.
+ */
+ public void setEnablePositionIncrements(boolean enabled) {
+ PositionIncrementsAttribute attr = getQueryConfigHandler().getAttribute(
+ PositionIncrementsAttribute.class);
+ attr.setPositionIncrementsEnabled(enabled);
+ // uncomment code below when deprecated query parser attributes are removed
+ // getQueryConfigHandler().set(ConfigurationKeys.ENABLE_POSITION_INCREMENTS,
+ // enabled);
+ }
+
+ /**
+ * @see #setEnablePositionIncrements(boolean)
+ */
+ public boolean getEnablePositionIncrements() {
+ Boolean enablePositionsIncrements = getQueryConfigHandler().get(
+ ConfigurationKeys.ENABLE_POSITION_INCREMENTS);
+
+ if (enablePositionsIncrements == null) {
+ return false;
+
+ } else {
+ return enablePositionsIncrements;
+ }
+
+ }
+
+ /**
+ * By default, it uses
+ * {@link MultiTermQuery#CONSTANT_SCORE_AUTO_REWRITE_DEFAULT} when creating a
+ * prefix, wildcard and range queries. This implementation is generally
+ * preferable because it a) Runs faster b) Does not have the scarcity of terms
+ * unduly influence score c) avoids any {@link TooManyListenersException}
+ * exception. However, if your application really needs to use the
+ * old-fashioned boolean queries expansion rewriting and the above points are
+ * not relevant then use this change the rewrite method.
+ */
+ public void setMultiTermRewriteMethod(MultiTermQuery.RewriteMethod method) {
+ MultiTermRewriteMethodAttribute attr = getQueryConfigHandler()
+ .getAttribute(MultiTermRewriteMethodAttribute.class);
+ attr.setMultiTermRewriteMethod(method);
+ // uncomment code below when deprecated query parser attributes are removed
+ // getQueryConfigHandler().set(ConfigurationKeys.MULTI_TERM_REWRITE_METHOD,
+ // method);
+ }
+
+ /**
+ * @see #setMultiTermRewriteMethod(org.apache.lucene.search.MultiTermQuery.RewriteMethod)
+ */
+ public MultiTermQuery.RewriteMethod getMultiTermRewriteMethod() {
+ return getQueryConfigHandler().get(
+ ConfigurationKeys.MULTI_TERM_REWRITE_METHOD);
+ }
+
+ /**
+ * Set the fields a query should be expanded to when the field is
+ * null
+ *
+ * @param fields the fields used to expand the query
+ */
+ public void setMultiFields(CharSequence[] fields) {
+
+ if (fields == null) {
+ fields = new CharSequence[0];
+ }
+
+ MultiFieldAttribute attr = getQueryConfigHandler().addAttribute(
+ MultiFieldAttribute.class);
+ attr.setFields(fields);
+ // uncomment code below when deprecated query parser attributes are removed
+ // getQueryConfigHandler().set(ConfigurationKeys.MULTI_FIELDS, fields);
+
+ }
+
+ /**
+ * Returns the fields used to expand the query when the field for a certain
+ * query is null
+ *
+ * @param fields the fields used to expand the query
+ */
+ public void getMultiFields(CharSequence[] fields) {
+ getQueryConfigHandler().get(ConfigurationKeys.MULTI_FIELDS);
+ }
+
+ /**
+ * Set the prefix length for fuzzy queries. Default is 0.
+ *
+ * @param fuzzyPrefixLength The fuzzyPrefixLength to set.
+ */
+ public void setFuzzyPrefixLength(int fuzzyPrefixLength) {
+ FuzzyAttribute attr = getQueryConfigHandler().addAttribute(
+ FuzzyAttribute.class);
+ attr.setPrefixLength(fuzzyPrefixLength);
+
+ // uncomment code below when deprecated query parser attributes are removed
+ /*
+ * 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);
+ */
+ }
+
+ public void setNumericConfigMap(Map