From 0f2a11695e9c8c9ebf156f421ccd078d0bbe609b Mon Sep 17 00:00:00 2001 From: kel Date: Fri, 15 Sep 2017 20:01:35 +0800 Subject: [PATCH] Filter unsupported relation for range query builder (#26620) --- .../index/query/RangeQueryBuilder.java | 13 ++++++++++ .../index/query/RangeQueryBuilderTests.java | 26 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/core/src/main/java/org/elasticsearch/index/query/RangeQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/RangeQueryBuilder.java index 0d4ded10d2c..35350a8480e 100644 --- a/core/src/main/java/org/elasticsearch/index/query/RangeQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/RangeQueryBuilder.java @@ -115,10 +115,20 @@ public class RangeQueryBuilder extends AbstractQueryBuilder 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 { out.writeString(this.fieldName); @@ -317,6 +327,9 @@ public class RangeQueryBuilder extends AbstractQueryBuilder 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; } diff --git a/core/src/test/java/org/elasticsearch/index/query/RangeQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/RangeQueryBuilderTests.java index a3f90498b52..67c3e67d39e 100644 --- a/core/src/test/java/org/elasticsearch/index/query/RangeQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/RangeQueryBuilderTests.java @@ -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 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()); + } }