Throw parsing error if span term query contains multiple fields in its short version

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

View File

@ -104,10 +104,7 @@ public class SpanTermQueryBuilder extends BaseTermQueryBuilder<SpanTermQueryBuil
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (token == XContentParser.Token.START_OBJECT) {
if (fieldName != null) {
throw new ParsingException(parser.getTokenLocation(), "[span_term] query doesn't support multiple fields, found ["
+ fieldName + "] and [" + currentFieldName + "]");
}
throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, currentFieldName);
fieldName = currentFieldName;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
@ -128,6 +125,7 @@ public class SpanTermQueryBuilder extends BaseTermQueryBuilder<SpanTermQueryBuil
}
}
} else {
throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, parser.currentName());
fieldName = parser.currentName();
value = parser.objectBytes();
}

View File

@ -120,6 +120,15 @@ public class SpanTermQueryBuilderTests extends AbstractTermQueryTestCase<SpanTer
"}";
ParsingException e = expectThrows(ParsingException.class, () -> parseQuery(json));
assertEquals("[span_term] query doesn't support multiple fields, found [message1] and [message2]", e.getMessage());
String shortJson = "{\n" +
" \"span_term\" : {\n" +
" \"message1\" : \"this\",\n" +
" \"message2\" : \"this\"\n" +
" }\n" +
"}";
e = expectThrows(ParsingException.class, () -> parseQuery(shortJson));
assertEquals("[span_term] query doesn't support multiple fields, found [message1] and [message2]", e.getMessage());
}
}