percolator: Fix NPE in percolator's 'now' range check for percolator queries with range queries.

Closes #22355
This commit is contained in:
Martijn van Groningen 2016-12-27 21:10:01 +01:00
parent 02d4cbfeea
commit 9ccdd3303d
2 changed files with 55 additions and 2 deletions

View File

@ -377,12 +377,18 @@ public class PercolatorFieldMapper extends FieldMapper {
RangeQueryBuilder rangeQueryBuilder = (RangeQueryBuilder) queryBuilder;
if (rangeQueryBuilder.from() instanceof String) {
String from = (String) rangeQueryBuilder.from();
String to = (String) rangeQueryBuilder.to();
if (from.contains("now") || to.contains("now")) {
if (from.contains("now")) {
throw new IllegalArgumentException("percolator queries containing time range queries based on the " +
"current time is unsupported");
}
}
if (rangeQueryBuilder.to() instanceof String) {
String to = (String) rangeQueryBuilder.to();
if (to.contains("now")) {
throw new IllegalArgumentException("percolator queries containing time range queries based on the " +
"current time is unsupported");
}
}
} else if (queryBuilder instanceof HasChildQueryBuilder) {
throw new IllegalArgumentException("the [has_child] query is unsupported inside a percolator query");
} else if (queryBuilder instanceof HasParentQueryBuilder) {

View File

@ -442,6 +442,53 @@ public class PercolatorFieldMapperTests extends ESSingleNodeTestCase {
}
);
assertThat(e.getCause(), instanceOf(IllegalArgumentException.class));
e = expectThrows(MapperParsingException.class, () -> {
mapperService.documentMapper(typeName).parse("test", typeName, "1",
jsonBuilder().startObject()
.field(fieldName, rangeQuery("date_field").from("now"))
.endObject().bytes());
}
);
assertThat(e.getCause(), instanceOf(IllegalArgumentException.class));
e = expectThrows(MapperParsingException.class, () -> {
mapperService.documentMapper(typeName).parse("test", typeName, "1",
jsonBuilder().startObject()
.field(fieldName, rangeQuery("date_field").to("now"))
.endObject().bytes());
}
);
assertThat(e.getCause(), instanceOf(IllegalArgumentException.class));
}
// https://github.com/elastic/elasticsearch/issues/22355
public void testVerifyRangeQueryWithNullBounds() throws Exception {
addQueryMapping();
MapperParsingException e = expectThrows(MapperParsingException.class, () -> {
mapperService.documentMapper(typeName).parse("test", typeName, "1",
jsonBuilder().startObject()
.field(fieldName, rangeQuery("date_field").from("now").to(null))
.endObject().bytes());
}
);
assertThat(e.getCause(), instanceOf(IllegalArgumentException.class));
e = expectThrows(MapperParsingException.class, () -> {
mapperService.documentMapper(typeName).parse("test", typeName, "1",
jsonBuilder().startObject()
.field(fieldName, rangeQuery("date_field").from(null).to("now"))
.endObject().bytes());
}
);
assertThat(e.getCause(), instanceOf(IllegalArgumentException.class));
// No validation failures:
mapperService.documentMapper(typeName).parse("test", typeName, "1",
jsonBuilder().startObject()
.field(fieldName, rangeQuery("date_field").from("2016-01-01").to(null))
.endObject().bytes());
mapperService.documentMapper(typeName).parse("test", typeName, "1",
jsonBuilder().startObject()
.field(fieldName, rangeQuery("date_field").from(null).to("2016-01-01"))
.endObject().bytes());
}
public void testUnsupportedQueries() {