Query DSL: Allow to default certain settings in query_string / field queries, closes #1540.
This commit is contained in:
parent
679bd1c877
commit
d2c1ec5f25
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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());
|
||||
|
|
Loading…
Reference in New Issue