Skip to content

Commit

Permalink
Query DSL: Allow to default certain settings in query_string / field …
Browse files Browse the repository at this point in the history
…queries, closes elastic#1540.
  • Loading branch information
kimchy committed Dec 13, 2011
1 parent 679bd1c commit d2c1ec5
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,21 @@
*/
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
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;

Expand All @@ -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
Expand All @@ -60,6 +66,9 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars

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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@
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;
Expand All @@ -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
Expand All @@ -62,6 +69,8 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars

MultiFieldQueryParserSettings qpSettings = new MultiFieldQueryParserSettings();
qpSettings.defaultField(AllFieldMapper.NAME);
qpSettings.analyzeWildcard(defaultAnalyzeWildcard);
qpSettings.allowLeadingWildcard(defaultAllowLeadingWildcard);

String currentFieldName = null;
XContentParser.Token token;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ public IndicesQueriesRegistry(Settings settings) {
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());
Expand Down

0 comments on commit d2c1ec5

Please sign in to comment.