Throw parsing error if range query contains multiple fields

Range Query, like many other queries, used to parse when the query refers to multiple fields and the last one would win. We rather throw an exception now instead.

Closes #19547
This commit is contained in:
javanna 2016-08-03 10:30:20 +02:00 committed by Luca Cavanna
parent a01475a20b
commit 11e4b0168b
2 changed files with 28 additions and 0 deletions

View File

@ -318,6 +318,10 @@ public class RangeQueryBuilder extends AbstractQueryBuilder<RangeQueryBuilder> i
} else if (parseContext.isDeprecatedSetting(currentFieldName)) {
// skip
} else if (token == XContentParser.Token.START_OBJECT) {
if (fieldName != null) {
throw new ParsingException(parser.getTokenLocation(), "[range] query doesn't support multiple fields, found ["
+ fieldName + "] and [" + currentFieldName + "]");
}
fieldName = currentFieldName;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {

View File

@ -27,6 +27,7 @@ import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermRangeQuery;
import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.common.ParseFieldMatcher;
import org.elasticsearch.common.ParsingException;
import org.elasticsearch.common.lucene.BytesRefs;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.mapper.MappedFieldType.Relation;
@ -519,4 +520,27 @@ public class RangeQueryBuilderTests extends AbstractQueryTestCase<RangeQueryBuil
QueryBuilder rewritten = query.rewrite(queryShardContext);
assertThat(rewritten, sameInstance(query));
}
public void testParseFailsWithMultipleFields() throws IOException {
String json =
"{\n" +
" \"range\": {\n" +
" \"age\": {\n" +
" \"gte\": 30,\n" +
" \"lte\": 40\n" +
" },\n" +
" \"price\": {\n" +
" \"gte\": 10,\n" +
" \"lte\": 30\n" +
" }\n" +
" }\n" +
" }";
try {
parseQuery(json);
fail("parseQuery should have failed");
} catch(ParsingException e) {
assertEquals("[range] query doesn't support multiple fields, found [age] and [price]", e.getMessage());
}
}
}