Skip to content

Commit

Permalink
Merge pull request #19564 from qwerty4030/fix/2.4/multi_match_wildcard
Browse files Browse the repository at this point in the history
Fixed QueryParsingException in multi match query
  • Loading branch information
cbuescher authored Jul 28, 2016
2 parents a16a816 + 7ee4ea8 commit 06e2412
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@
import org.elasticsearch.common.regex.Regex;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.query.support.QueryParsers;
import org.elasticsearch.index.search.MatchQuery;
import org.elasticsearch.index.search.MultiMatchQuery;

import java.io.IOException;
import java.util.Map;
import java.util.TreeMap;

/**
* Same as {@link MatchQueryParser} but has support for multiple fields.
Expand Down Expand Up @@ -73,10 +75,10 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
} else if ("fields".equals(currentFieldName)) {
if (token == XContentParser.Token.START_ARRAY) {
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
extractFieldAndBoost(parseContext, parser, fieldNameWithBoosts);
parseFieldAndBoost(parser, fieldNameWithBoosts);
}
} else if (token.isValue()) {
extractFieldAndBoost(parseContext, parser, fieldNameWithBoosts);
parseFieldAndBoost(parser, fieldNameWithBoosts);
} else {
throw new QueryParsingException(parseContext, "[" + NAME + "] query does not support [" + currentFieldName + "]");
}
Expand Down Expand Up @@ -160,7 +162,10 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
}
}
}
Query query = multiMatchQuery.parse(type, fieldNameWithBoosts, value, minimumShouldMatch);

Map<String, Float> newFieldsBoosts = handleFieldsMatchPattern(parseContext.mapperService(), fieldNameWithBoosts);

Query query = multiMatchQuery.parse(type, newFieldsBoosts, value, minimumShouldMatch);
if (query == null) {
return null;
}
Expand All @@ -172,7 +177,23 @@ public Query parse(QueryParseContext parseContext) throws IOException, QueryPars
return query;
}

private void extractFieldAndBoost(QueryParseContext parseContext, XContentParser parser, Map<String, Float> fieldNameWithBoosts) throws IOException {
private static Map<String, Float> handleFieldsMatchPattern(MapperService mapperService, Map<String, Float> fieldsBoosts) {
Map<String, Float> newFieldsBoosts = new TreeMap<>();
for (Map.Entry<String, Float> fieldBoost : fieldsBoosts.entrySet()) {
String fField = fieldBoost.getKey();
Float fBoost = fieldBoost.getValue();
if (Regex.isSimpleMatchPattern(fField)) {
for (String field : mapperService.simpleMatchToIndexNames(fField)) {
newFieldsBoosts.put(field, fBoost);
}
} else {
newFieldsBoosts.put(fField, fBoost);
}
}
return newFieldsBoosts;
}

private static void parseFieldAndBoost(XContentParser parser, Map<String, Float> fieldsBoosts) throws IOException {
String fField = null;
Float fBoost = null;
char[] fieldText = parser.textCharacters();
Expand All @@ -188,13 +209,6 @@ private void extractFieldAndBoost(QueryParseContext parseContext, XContentParser
if (fField == null) {
fField = parser.text();
}

if (Regex.isSimpleMatchPattern(fField)) {
for (String field : parseContext.mapperService().simpleMatchToIndexNames(fField)) {
fieldNameWithBoosts.put(field, fBoost);
}
} else {
fieldNameWithBoosts.put(fField, fBoost);
}
fieldsBoosts.put(fField, fBoost);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public void testSingleField() throws NoSuchFieldException, IllegalAccessExceptio
assertNoFailures(searchResponse);
assertFirstHit(searchResponse, hasId("theone"));

String[] fields = {"full_name", "first_name", "last_name", "last_name_phrase", "first_name_phrase", "category_phrase", "category"};
String[] fields = {"full_name", "first_name", "last_name", "last_name_phrase", "first_name_phrase", "category_phrase", "category", "missing_field", "missing_fields*"};

String[] query = {"marvel","hero", "captain", "america", "15", "17", "1", "5", "ultimate", "Man",
"marvel", "wolferine", "ninja"};
Expand Down

0 comments on commit 06e2412

Please sign in to comment.