From d2c1ec5f25e0442458f1e0db6d93eebf198ab222 Mon Sep 17 00:00:00 2001 From: Shay Banon Date: Tue, 13 Dec 2011 19:02:01 +0200 Subject: [PATCH] Query DSL: Allow to default certain settings in query_string / field queries, closes #1540. --- .../lucene/queryParser/QueryParserSettings.java | 7 +++++-- .../elasticsearch/index/query/FieldQueryParser.java | 11 ++++++++++- .../index/query/QueryStringQueryParser.java | 11 ++++++++++- .../indices/query/IndicesQueriesRegistry.java | 4 ++-- 4 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/apache/lucene/queryParser/QueryParserSettings.java b/src/main/java/org/apache/lucene/queryParser/QueryParserSettings.java index 825cc0fc4bb..eaef33cd479 100644 --- a/src/main/java/org/apache/lucene/queryParser/QueryParserSettings.java +++ b/src/main/java/org/apache/lucene/queryParser/QueryParserSettings.java @@ -28,18 +28,21 @@ import org.apache.lucene.search.MultiTermQuery; */ public class QueryParserSettings { + public static final boolean DEFAULT_ALLOW_LEADING_WILDCARD = true; + public static final boolean DEFAULT_ANALYZE_WILDCARD = false; + private String queryString; private String defaultField; private float boost = 1.0f; private MapperQueryParser.Operator defaultOperator = QueryParser.Operator.OR; private boolean autoGeneratePhraseQueries = false; - private boolean allowLeadingWildcard = true; + private boolean allowLeadingWildcard = DEFAULT_ALLOW_LEADING_WILDCARD; private boolean lowercaseExpandedTerms = true; private boolean enablePositionIncrements = true; private int phraseSlop = 0; private float fuzzyMinSim = FuzzyQuery.defaultMinSimilarity; private int fuzzyPrefixLength = FuzzyQuery.defaultPrefixLength; - private boolean analyzeWildcard = false; + private boolean analyzeWildcard = DEFAULT_ANALYZE_WILDCARD; private boolean escape = false; private Analyzer analyzer = null; private MultiTermQuery.RewriteMethod rewriteMethod = MultiTermQuery.CONSTANT_SCORE_AUTO_REWRITE_DEFAULT; diff --git a/src/main/java/org/elasticsearch/index/query/FieldQueryParser.java b/src/main/java/org/elasticsearch/index/query/FieldQueryParser.java index 4c5a768100b..2ba602ddcfa 100644 --- a/src/main/java/org/elasticsearch/index/query/FieldQueryParser.java +++ b/src/main/java/org/elasticsearch/index/query/FieldQueryParser.java @@ -26,6 +26,7 @@ import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.Query; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.lucene.search.Queries; +import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.query.support.QueryParsers; @@ -41,8 +42,13 @@ public class FieldQueryParser implements QueryParser { public static final String NAME = "field"; + private final boolean defaultAnalyzeWildcard; + private final boolean defaultAllowLeadingWildcard; + @Inject - public FieldQueryParser() { + public FieldQueryParser(Settings settings) { + this.defaultAnalyzeWildcard = settings.getAsBoolean("indices.query.query_string.analyze_wildcard", QueryParserSettings.DEFAULT_ANALYZE_WILDCARD); + this.defaultAllowLeadingWildcard = settings.getAsBoolean("indices.query.query_string.allowLeadingWildcard", QueryParserSettings.DEFAULT_ALLOW_LEADING_WILDCARD); } @Override @@ -60,6 +66,9 @@ public class FieldQueryParser implements QueryParser { QueryParserSettings qpSettings = new QueryParserSettings(); qpSettings.defaultField(fieldName); + qpSettings.analyzeWildcard(defaultAnalyzeWildcard); + qpSettings.allowLeadingWildcard(defaultAllowLeadingWildcard); + token = parser.nextToken(); if (token == XContentParser.Token.START_OBJECT) { String currentFieldName = null; diff --git a/src/main/java/org/elasticsearch/index/query/QueryStringQueryParser.java b/src/main/java/org/elasticsearch/index/query/QueryStringQueryParser.java index ff2df060ed4..7e2d7bd94bf 100644 --- a/src/main/java/org/elasticsearch/index/query/QueryStringQueryParser.java +++ b/src/main/java/org/elasticsearch/index/query/QueryStringQueryParser.java @@ -25,12 +25,14 @@ import gnu.trove.map.hash.TObjectFloatHashMap; import org.apache.lucene.queryParser.MapperQueryParser; import org.apache.lucene.queryParser.MultiFieldQueryParserSettings; import org.apache.lucene.queryParser.ParseException; +import org.apache.lucene.queryParser.QueryParserSettings; import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.Query; import org.elasticsearch.common.Strings; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.regex.Regex; +import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.mapper.internal.AllFieldMapper; import org.elasticsearch.index.query.support.QueryParsers; @@ -47,8 +49,13 @@ public class QueryStringQueryParser implements QueryParser { public static final String NAME = "query_string"; + private final boolean defaultAnalyzeWildcard; + private final boolean defaultAllowLeadingWildcard; + @Inject - public QueryStringQueryParser() { + public QueryStringQueryParser(Settings settings) { + this.defaultAnalyzeWildcard = settings.getAsBoolean("indices.query.query_string.analyze_wildcard", QueryParserSettings.DEFAULT_ANALYZE_WILDCARD); + this.defaultAllowLeadingWildcard = settings.getAsBoolean("indices.query.query_string.allowLeadingWildcard", QueryParserSettings.DEFAULT_ALLOW_LEADING_WILDCARD); } @Override @@ -62,6 +69,8 @@ public class QueryStringQueryParser implements QueryParser { MultiFieldQueryParserSettings qpSettings = new MultiFieldQueryParserSettings(); qpSettings.defaultField(AllFieldMapper.NAME); + qpSettings.analyzeWildcard(defaultAnalyzeWildcard); + qpSettings.allowLeadingWildcard(defaultAllowLeadingWildcard); String currentFieldName = null; XContentParser.Token token; diff --git a/src/main/java/org/elasticsearch/indices/query/IndicesQueriesRegistry.java b/src/main/java/org/elasticsearch/indices/query/IndicesQueriesRegistry.java index 61f9b337a60..ccc0154f28b 100644 --- a/src/main/java/org/elasticsearch/indices/query/IndicesQueriesRegistry.java +++ b/src/main/java/org/elasticsearch/indices/query/IndicesQueriesRegistry.java @@ -45,13 +45,13 @@ public class IndicesQueriesRegistry { addQueryParser(queryParsers, new DisMaxQueryParser()); addQueryParser(queryParsers, new IdsQueryParser()); addQueryParser(queryParsers, new MatchAllQueryParser()); - addQueryParser(queryParsers, new QueryStringQueryParser()); + addQueryParser(queryParsers, new QueryStringQueryParser(settings)); addQueryParser(queryParsers, new BoostingQueryParser()); addQueryParser(queryParsers, new BoolQueryParser(settings)); addQueryParser(queryParsers, new TermQueryParser()); addQueryParser(queryParsers, new TermsQueryParser()); addQueryParser(queryParsers, new FuzzyQueryParser()); - addQueryParser(queryParsers, new FieldQueryParser()); + addQueryParser(queryParsers, new FieldQueryParser(settings)); addQueryParser(queryParsers, new RangeQueryParser()); addQueryParser(queryParsers, new PrefixQueryParser()); addQueryParser(queryParsers, new WildcardQueryParser());