take into account field mapped analyzers in simple_query_string

need to use the correct analyzer here, that will automatically choose the correct analyzer per field
This commit is contained in:
Shay Banon 2013-12-17 14:34:05 +01:00
parent a4f97bed9d
commit be860c8004
1 changed files with 17 additions and 18 deletions

View File

@ -19,6 +19,7 @@
package org.elasticsearch.index.query; package org.elasticsearch.index.query;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.queryparser.XSimpleQueryParser; import org.apache.lucene.queryparser.XSimpleQueryParser;
import org.apache.lucene.search.BooleanClause; import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.Query; import org.apache.lucene.search.Query;
@ -27,7 +28,6 @@ import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.regex.Regex; import org.elasticsearch.common.regex.Regex;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.analysis.NamedAnalyzer;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
@ -37,23 +37,23 @@ import java.util.Map;
* SimpleQueryStringParser is a query parser that acts similar to a query_string * SimpleQueryStringParser is a query parser that acts similar to a query_string
* query, but won't throw exceptions for any weird string syntax. It supports * query, but won't throw exceptions for any weird string syntax. It supports
* the following: * the following:
* * <p/>
* <ul> * <ul>
* <li>'{@code +}' specifies {@code AND} operation: <tt>token1+token2</tt> * <li>'{@code +}' specifies {@code AND} operation: <tt>token1+token2</tt>
* <li>'{@code |}' specifies {@code OR} operation: <tt>token1|token2</tt> * <li>'{@code |}' specifies {@code OR} operation: <tt>token1|token2</tt>
* <li>'{@code -}' negates a single token: <tt>-token0</tt> * <li>'{@code -}' negates a single token: <tt>-token0</tt>
* <li>'{@code "}' creates phrases of terms: <tt>"term1 term2 ..."</tt> * <li>'{@code "}' creates phrases of terms: <tt>"term1 term2 ..."</tt>
* <li>'{@code *}' at the end of terms specifies prefix query: <tt>term*</tt> * <li>'{@code *}' at the end of terms specifies prefix query: <tt>term*</tt>
* <li>'{@code (}' and '{@code )}' specifies precedence: <tt>token1 + (token2 | token3)</tt> * <li>'{@code (}' and '{@code)}' specifies precedence: <tt>token1 + (token2 | token3)</tt>
* </ul> * </ul>
* * <p/>
* See: {@link XSimpleQueryParser} for more information. * See: {@link XSimpleQueryParser} for more information.
* * <p/>
* This query supports these options: * This query supports these options:
* * <p/>
* Required: * Required:
* {@code query} - query text to be converted into other queries * {@code query} - query text to be converted into other queries
* * <p/>
* Optional: * Optional:
* {@code analyzer} - anaylzer to be used for analyzing tokens to determine * {@code analyzer} - anaylzer to be used for analyzing tokens to determine
* which kind of query they should be converted into, defaults to "standard" * which kind of query they should be converted into, defaults to "standard"
@ -85,9 +85,9 @@ public class SimpleQueryStringParser implements QueryParser {
String field = null; String field = null;
Map<String, Float> fieldsAndWeights = null; Map<String, Float> fieldsAndWeights = null;
BooleanClause.Occur defaultOperator = null; BooleanClause.Occur defaultOperator = null;
NamedAnalyzer analyzer = null; Analyzer analyzer = null;
XContentParser.Token token = null;
XContentParser.Token token;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) { if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName(); currentFieldName = parser.currentName();
@ -132,8 +132,7 @@ public class SimpleQueryStringParser implements QueryParser {
} else if ("analyzer".equals(currentFieldName)) { } else if ("analyzer".equals(currentFieldName)) {
analyzer = parseContext.analysisService().analyzer(parser.text()); analyzer = parseContext.analysisService().analyzer(parser.text());
if (analyzer == null) { if (analyzer == null) {
throw new QueryParsingException(parseContext.index(), throw new QueryParsingException(parseContext.index(), "[" + NAME + "] analyzer [" + parser.text() + "] not found");
"[" + NAME + "] analyzer [" + parser.text() + "] not found");
} }
} else if ("field".equals(currentFieldName)) { } else if ("field".equals(currentFieldName)) {
field = parser.text(); field = parser.text();
@ -164,13 +163,13 @@ public class SimpleQueryStringParser implements QueryParser {
} }
// Use the default field (_all) if no fields specified // Use the default field (_all) if no fields specified
if (queryBody != null && fieldsAndWeights == null) { if (fieldsAndWeights == null) {
field = parseContext.defaultField(); field = parseContext.defaultField();
} }
// Use standard analyzer by default // Use standard analyzer by default
if (analyzer == null) { if (analyzer == null) {
analyzer = parseContext.analysisService().analyzer("standard"); analyzer = parseContext.mapperService().searchAnalyzer();
} }
XSimpleQueryParser sqp; XSimpleQueryParser sqp;