diff --git a/core/src/main/java/org/elasticsearch/index/query/TermQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/TermQueryBuilder.java index b3ce8bab9bd..92ec60f402f 100644 --- a/core/src/main/java/org/elasticsearch/index/query/TermQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/TermQueryBuilder.java @@ -98,11 +98,7 @@ public class TermQueryBuilder extends BaseTermQueryBuilder { } else if (parseContext.isDeprecatedSetting(currentFieldName)) { // skip } else if (token == XContentParser.Token.START_OBJECT) { - // also support a format of "term" : {"field_name" : { ... }} - if (fieldName != null) { - throw new ParsingException(parser.getTokenLocation(), - "[term] query does not support different field names, use [bool] query instead"); - } + throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, currentFieldName); fieldName = currentFieldName; while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { @@ -123,10 +119,7 @@ public class TermQueryBuilder extends BaseTermQueryBuilder { } } } else if (token.isValue()) { - if (fieldName != null) { - throw new ParsingException(parser.getTokenLocation(), - "[term] query does not support different field names, use [bool] query instead"); - } + throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, parser.currentName()); fieldName = currentFieldName; value = parser.objectBytes(); } else if (token == XContentParser.Token.START_ARRAY) { diff --git a/core/src/test/java/org/elasticsearch/index/query/TermQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/TermQueryBuilderTests.java index 14e5054c5e1..0e6542e4e3d 100644 --- a/core/src/test/java/org/elasticsearch/index/query/TermQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/TermQueryBuilderTests.java @@ -154,7 +154,7 @@ public class TermQueryBuilderTests extends AbstractTermQueryTestCase 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" + " \"term\" : {\n" + @@ -163,6 +163,6 @@ public class TermQueryBuilderTests extends AbstractTermQueryTestCase 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()); } }