Skip to content

Commit 5b0a3d2

Browse files
markharwoodastefan
authored andcommitted
Search - reject arrays of values when passed to the Term query. (elastic#82194)
Bug fix to reject arrays of values when passed to the Term query. Use error message as an opportunity to advertise the `terms` query or use of a Bool query for scoring purposes. Closes elastic#82183
1 parent 0d6bb46 commit 5b0a3d2

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

server/src/main/java/org/elasticsearch/index/query/TermQueryBuilder.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,13 @@ public static TermQueryBuilder fromXContent(XContentParser parser) throws IOExce
120120
if (TERM_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
121121
value = maybeConvertToBytesRef(parser.objectBytes());
122122
} else if (VALUE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
123+
if (parser.currentToken() == XContentParser.Token.START_ARRAY) {
124+
throw new ParsingException(
125+
parser.getTokenLocation(),
126+
"[term] query does not support arrays for value - use a bool query with multiple term "
127+
+ "clauses in the should section or use a Terms query if scoring is not required"
128+
);
129+
}
123130
value = maybeConvertToBytesRef(parser.objectBytes());
124131
} else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
125132
queryName = parser.text();

server/src/test/java/org/elasticsearch/index/query/TermQueryBuilderTests.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,23 @@ public void testParseFailsWithMultipleFields() {
175175
assertEquals("[term] query doesn't support multiple fields, found [message1] and [message2]", e.getMessage());
176176
}
177177

178+
public void testParseFailsWithMultipleValues() {
179+
String json = """
180+
{
181+
"term" : {
182+
"message1" : {
183+
"value" : ["this", "that"]
184+
}
185+
}
186+
}""";
187+
ParsingException e = expectThrows(ParsingException.class, () -> parseQuery(json));
188+
assertEquals(
189+
"[term] query does not support arrays for value - use a bool query with multiple term clauses "
190+
+ "in the should section or use a Terms query if scoring is not required",
191+
e.getMessage()
192+
);
193+
}
194+
178195
public void testParseAndSerializeBigInteger() throws IOException {
179196
String json = """
180197
{

0 commit comments

Comments
 (0)