Filter unsupported relation for range query builder (#26620)

This commit is contained in:
kel 2017-09-15 20:01:35 +08:00 committed by Adrien Grand
parent 7184cf8b5b
commit 0f2a11695e
2 changed files with 39 additions and 0 deletions

View File

@ -115,9 +115,19 @@ public class RangeQueryBuilder extends AbstractQueryBuilder<RangeQueryBuilder> i
String relationString = in.readOptionalString();
if (relationString != null) {
relation = ShapeRelation.getRelationByName(relationString);
if (relation != null && !isRelationAllowed(relation)) {
throw new IllegalArgumentException(
"[range] query does not support relation [" + relationString + "]");
}
}
}
}
private boolean isRelationAllowed(ShapeRelation relation) {
return relation == ShapeRelation.INTERSECTS
|| relation == ShapeRelation.CONTAINS
|| relation == ShapeRelation.WITHIN;
}
@Override
protected void doWriteTo(StreamOutput out) throws IOException {
@ -317,6 +327,9 @@ public class RangeQueryBuilder extends AbstractQueryBuilder<RangeQueryBuilder> i
if (this.relation == null) {
throw new IllegalArgumentException(relation + " is not a valid relation");
}
if (!isRelationAllowed(this.relation)) {
throw new IllegalArgumentException("[range] query does not support relation [" + relation + "]");
}
return this;
}

View File

@ -31,6 +31,7 @@ import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TermRangeQuery;
import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.common.ParsingException;
import org.elasticsearch.common.geo.ShapeRelation;
import org.elasticsearch.common.lucene.BytesRefs;
import org.elasticsearch.index.mapper.DateFieldMapper;
import org.elasticsearch.index.mapper.FieldNamesFieldMapper;
@ -535,4 +536,29 @@ public class RangeQueryBuilderTests extends AbstractQueryTestCase<RangeQueryBuil
ParsingException e = expectThrows(ParsingException.class, () -> parseQuery(json));
assertEquals("[range] query doesn't support multiple fields, found [age] and [" + DATE_FIELD_NAME + "]", e.getMessage());
}
public void testParseRelation() {
String json =
"{\n" +
" \"range\": {\n" +
" \"age\": {\n" +
" \"gte\": 30,\n" +
" \"lte\": 40,\n" +
" \"relation\": \"disjoint\"\n" +
" }" +
" }\n" +
" }";
String fieldName = randomAlphaOfLengthBetween(1, 20);
IllegalArgumentException e1 = expectThrows(IllegalArgumentException.class, () -> parseQuery(json));
assertEquals("[range] query does not support relation [disjoint]", e1.getMessage());
RangeQueryBuilder builder = new RangeQueryBuilder(fieldName);
IllegalArgumentException e2 = expectThrows(IllegalArgumentException.class, ()->builder.relation("disjoint"));
assertEquals("[range] query does not support relation [disjoint]", e2.getMessage());
builder.relation("contains");
assertEquals(ShapeRelation.CONTAINS, builder.relation());
builder.relation("within");
assertEquals(ShapeRelation.WITHIN, builder.relation());
builder.relation("intersects");
assertEquals(ShapeRelation.INTERSECTS, builder.relation());
}
}