diff --git a/src/main/java/org/apache/lucene/queryParser/MapperQueryParser.java b/src/main/java/org/apache/lucene/queryParser/MapperQueryParser.java index ff4c322aafa..a52194af67f 100644 --- a/src/main/java/org/apache/lucene/queryParser/MapperQueryParser.java +++ b/src/main/java/org/apache/lucene/queryParser/MapperQueryParser.java @@ -64,6 +64,8 @@ public class MapperQueryParser extends QueryParser { private final QueryParseContext parseContext; + private boolean forcedAnalyzer; + private FieldMapper currentMapper; private boolean analyzeWildcard; @@ -74,14 +76,15 @@ public class MapperQueryParser extends QueryParser { } public MapperQueryParser(QueryParserSettings settings, QueryParseContext parseContext) { - super(Lucene.QUERYPARSER_VERSION, settings.defaultField(), settings.analyzer()); + super(Lucene.QUERYPARSER_VERSION, settings.defaultField(), settings.defaultAnalyzer()); this.parseContext = parseContext; reset(settings); } public void reset(QueryParserSettings settings) { this.field = settings.defaultField(); - this.analyzer = settings.analyzer(); + this.forcedAnalyzer = settings.forcedAnalyzer() != null; + this.analyzer = forcedAnalyzer ? settings.forcedAnalyzer() : settings.defaultAnalyzer(); setMultiTermRewriteMethod(settings.rewriteMethod()); setEnablePositionIncrements(settings.enablePositionIncrements()); setAutoGeneratePhraseQueries(settings.autoGeneratePhraseQueries()); @@ -121,7 +124,9 @@ public class MapperQueryParser extends QueryParser { try { MapperService.SmartNameFieldMappers fieldMappers = parseContext.smartFieldMappers(field); if (fieldMappers != null) { - analyzer = fieldMappers.searchAnalyzer(); + if (!forcedAnalyzer) { + analyzer = fieldMappers.searchAnalyzer(); + } currentMapper = fieldMappers.fieldMappers().mapper(); if (currentMapper != null) { Query query = null; @@ -182,7 +187,9 @@ public class MapperQueryParser extends QueryParser { try { MapperService.SmartNameFieldMappers fieldMappers = parseContext.smartFieldMappers(field); if (fieldMappers != null) { - analyzer = fieldMappers.searchAnalyzer(); + if (!forcedAnalyzer) { + analyzer = fieldMappers.searchAnalyzer(); + } currentMapper = fieldMappers.fieldMappers().mapper(); if (currentMapper != null) { indexedNameField = currentMapper.names().indexName(); @@ -256,7 +263,9 @@ public class MapperQueryParser extends QueryParser { try { MapperService.SmartNameFieldMappers fieldMappers = parseContext.smartFieldMappers(field); if (fieldMappers != null) { - analyzer = fieldMappers.searchAnalyzer(); + if (!forcedAnalyzer) { + analyzer = fieldMappers.searchAnalyzer(); + } currentMapper = fieldMappers.fieldMappers().mapper(); if (currentMapper != null) { indexedNameField = currentMapper.names().indexName(); diff --git a/src/main/java/org/apache/lucene/queryParser/QueryParserSettings.java b/src/main/java/org/apache/lucene/queryParser/QueryParserSettings.java index eaef33cd479..110e02c9712 100644 --- a/src/main/java/org/apache/lucene/queryParser/QueryParserSettings.java +++ b/src/main/java/org/apache/lucene/queryParser/QueryParserSettings.java @@ -44,7 +44,8 @@ public class QueryParserSettings { private int fuzzyPrefixLength = FuzzyQuery.defaultPrefixLength; private boolean analyzeWildcard = DEFAULT_ANALYZE_WILDCARD; private boolean escape = false; - private Analyzer analyzer = null; + private Analyzer defaultAnalyzer = null; + private Analyzer forcedAnalyzer = null; private MultiTermQuery.RewriteMethod rewriteMethod = MultiTermQuery.CONSTANT_SCORE_AUTO_REWRITE_DEFAULT; private String minimumShouldMatch; @@ -144,12 +145,20 @@ public class QueryParserSettings { this.escape = escape; } - public Analyzer analyzer() { - return analyzer; + public Analyzer defaultAnalyzer() { + return defaultAnalyzer; } - public void analyzer(Analyzer analyzer) { - this.analyzer = analyzer; + public void defaultAnalyzer(Analyzer defaultAnalyzer) { + this.defaultAnalyzer = defaultAnalyzer; + } + + public Analyzer forcedAnalyzer() { + return forcedAnalyzer; + } + + public void forcedAnalyzer(Analyzer forcedAnalyzer) { + this.forcedAnalyzer = forcedAnalyzer; } public boolean analyzeWildcard() { @@ -193,7 +202,10 @@ public class QueryParserSettings { if (fuzzyPrefixLength != that.fuzzyPrefixLength) return false; if (lowercaseExpandedTerms != that.lowercaseExpandedTerms) return false; if (phraseSlop != that.phraseSlop) return false; - if (analyzer != null ? !analyzer.equals(that.analyzer) : that.analyzer != null) return false; + if (defaultAnalyzer != null ? !defaultAnalyzer.equals(that.defaultAnalyzer) : that.defaultAnalyzer != null) + return false; + if (forcedAnalyzer != null ? !forcedAnalyzer.equals(that.forcedAnalyzer) : that.forcedAnalyzer != null) + return false; if (defaultField != null ? !defaultField.equals(that.defaultField) : that.defaultField != null) return false; if (defaultOperator != that.defaultOperator) return false; if (queryString != null ? !queryString.equals(that.queryString) : that.queryString != null) return false; @@ -219,7 +231,8 @@ public class QueryParserSettings { result = 31 * result + (fuzzyMinSim != +0.0f ? Float.floatToIntBits(fuzzyMinSim) : 0); result = 31 * result + fuzzyPrefixLength; result = 31 * result + (escape ? 1 : 0); - result = 31 * result + (analyzer != null ? analyzer.hashCode() : 0); + result = 31 * result + (defaultAnalyzer != null ? defaultAnalyzer.hashCode() : 0); + result = 31 * result + (forcedAnalyzer != null ? forcedAnalyzer.hashCode() : 0); result = 31 * result + (analyzeWildcard ? 1 : 0); return result; } diff --git a/src/main/java/org/elasticsearch/index/query/FieldQueryParser.java b/src/main/java/org/elasticsearch/index/query/FieldQueryParser.java index 2ba602ddcfa..a9e2e1606e3 100644 --- a/src/main/java/org/elasticsearch/index/query/FieldQueryParser.java +++ b/src/main/java/org/elasticsearch/index/query/FieldQueryParser.java @@ -91,7 +91,7 @@ public class FieldQueryParser implements QueryParser { } else if ("phrase_slop".equals(currentFieldName) || "phraseSlop".equals(currentFieldName)) { qpSettings.phraseSlop(parser.intValue()); } else if ("analyzer".equals(currentFieldName)) { - qpSettings.analyzer(parseContext.analysisService().analyzer(parser.text())); + qpSettings.forcedAnalyzer(parseContext.analysisService().analyzer(parser.text())); } else if ("default_operator".equals(currentFieldName) || "defaultOperator".equals(currentFieldName)) { String op = parser.text(); if ("or".equalsIgnoreCase(op)) { @@ -123,9 +123,7 @@ public class FieldQueryParser implements QueryParser { parser.nextToken(); } - if (qpSettings.analyzer() == null) { - qpSettings.analyzer(parseContext.mapperService().searchAnalyzer()); - } + qpSettings.defaultAnalyzer(parseContext.mapperService().searchAnalyzer()); if (qpSettings.queryString() == null) { throw new QueryParsingException(parseContext.index(), "No value specified for term query"); diff --git a/src/main/java/org/elasticsearch/index/query/QueryStringQueryParser.java b/src/main/java/org/elasticsearch/index/query/QueryStringQueryParser.java index 7e2d7bd94bf..bb2d48f1e36 100644 --- a/src/main/java/org/elasticsearch/index/query/QueryStringQueryParser.java +++ b/src/main/java/org/elasticsearch/index/query/QueryStringQueryParser.java @@ -135,7 +135,7 @@ public class QueryStringQueryParser implements QueryParser { throw new QueryParsingException(parseContext.index(), "Query default operator [" + op + "] is not allowed"); } } else if ("analyzer".equals(currentFieldName)) { - qpSettings.analyzer(parseContext.analysisService().analyzer(parser.text())); + qpSettings.forcedAnalyzer(parseContext.analysisService().analyzer(parser.text())); } else if ("allow_leading_wildcard".equals(currentFieldName) || "allowLeadingWildcard".equals(currentFieldName)) { qpSettings.allowLeadingWildcard(parser.booleanValue()); } else if ("auto_generate_phrase_queries".equals(currentFieldName) || "autoGeneratePhraseQueries".equals(currentFieldName)) { @@ -170,9 +170,7 @@ public class QueryStringQueryParser implements QueryParser { if (qpSettings.queryString() == null) { throw new QueryParsingException(parseContext.index(), "query_string must be provided with a [query]"); } - if (qpSettings.analyzer() == null) { - qpSettings.analyzer(parseContext.mapperService().searchAnalyzer()); - } + qpSettings.defaultAnalyzer(parseContext.mapperService().searchAnalyzer()); if (qpSettings.escape()) { qpSettings.queryString(org.apache.lucene.queryParser.QueryParser.escape(qpSettings.queryString()));