Modify term query error when multiple fields are provided to comply with all other queries

This commit is contained in:
javanna 2016-08-09 10:01:56 +02:00 committed by Luca Cavanna
parent 0f54cb69ab
commit f9a40344b2
2 changed files with 4 additions and 11 deletions

View File

@ -98,11 +98,7 @@ public class TermQueryBuilder extends BaseTermQueryBuilder<TermQueryBuilder> {
} else if (parseContext.isDeprecatedSetting(currentFieldName)) { } else if (parseContext.isDeprecatedSetting(currentFieldName)) {
// skip // skip
} else if (token == XContentParser.Token.START_OBJECT) { } else if (token == XContentParser.Token.START_OBJECT) {
// also support a format of "term" : {"field_name" : { ... }} throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, currentFieldName);
if (fieldName != null) {
throw new ParsingException(parser.getTokenLocation(),
"[term] query does not support different field names, use [bool] query instead");
}
fieldName = currentFieldName; fieldName = currentFieldName;
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) {
@ -123,10 +119,7 @@ public class TermQueryBuilder extends BaseTermQueryBuilder<TermQueryBuilder> {
} }
} }
} else if (token.isValue()) { } else if (token.isValue()) {
if (fieldName != null) { throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, parser.currentName());
throw new ParsingException(parser.getTokenLocation(),
"[term] query does not support different field names, use [bool] query instead");
}
fieldName = currentFieldName; fieldName = currentFieldName;
value = parser.objectBytes(); value = parser.objectBytes();
} else if (token == XContentParser.Token.START_ARRAY) { } else if (token == XContentParser.Token.START_ARRAY) {

View File

@ -154,7 +154,7 @@ public class TermQueryBuilderTests extends AbstractTermQueryTestCase<TermQueryBu
" }\n" + " }\n" +
"}"; "}";
ParsingException e = expectThrows(ParsingException.class, () -> parseQuery(json)); ParsingException e = expectThrows(ParsingException.class, () -> parseQuery(json));
assertEquals("[term] query does not support different field names, use [bool] query instead", e.getMessage()); assertEquals("[term] query doesn't support multiple fields, found [message1] and [message2]", e.getMessage());
String shortJson = "{\n" + String shortJson = "{\n" +
" \"term\" : {\n" + " \"term\" : {\n" +
@ -163,6 +163,6 @@ public class TermQueryBuilderTests extends AbstractTermQueryTestCase<TermQueryBu
" }\n" + " }\n" +
"}"; "}";
e = expectThrows(ParsingException.class, () -> parseQuery(shortJson)); e = expectThrows(ParsingException.class, () -> parseQuery(shortJson));
assertEquals("[term] query does not support different field names, use [bool] query instead", e.getMessage()); assertEquals("[term] query doesn't support multiple fields, found [message1] and [message2]", e.getMessage());
} }
} }