Add proper longitude validation in geo_polygon_query (#30497)

Fixes longitude validation in geo_polygon_query builder. The queries
with wrong longitude currently fail but only later during polygon
with quite complicated error message.

Fixes #30488
This commit is contained in:
Igor Motov 2018-05-10 11:14:08 -04:00 committed by GitHub
parent df17f85e14
commit 2a79d9234b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 69 additions and 71 deletions

View File

@ -177,7 +177,7 @@ public class GeoPolygonQueryBuilder extends AbstractQueryBuilder<GeoPolygonQuery
throw new QueryShardException(context, "illegal latitude value [{}] for [{}]", point.lat(), throw new QueryShardException(context, "illegal latitude value [{}] for [{}]", point.lat(),
GeoPolygonQueryBuilder.NAME); GeoPolygonQueryBuilder.NAME);
} }
if (!GeoUtils.isValidLongitude(point.lat())) { if (!GeoUtils.isValidLongitude(point.lon())) {
throw new QueryShardException(context, "illegal longitude value [{}] for [{}]", point.lon(), throw new QueryShardException(context, "illegal longitude value [{}] for [{}]", point.lon(),
GeoPolygonQueryBuilder.NAME); GeoPolygonQueryBuilder.NAME);
} }

View File

@ -254,4 +254,38 @@ public class GeoPolygonQueryBuilderTests extends AbstractQueryTestCase<GeoPolygo
QueryShardException e = expectThrows(QueryShardException.class, () -> failingQueryBuilder.toQuery(createShardContext())); QueryShardException e = expectThrows(QueryShardException.class, () -> failingQueryBuilder.toQuery(createShardContext()));
assertThat(e.getMessage(), containsString("failed to find geo_point field [unmapped]")); assertThat(e.getMessage(), containsString("failed to find geo_point field [unmapped]"));
} }
public void testPointValidation() throws IOException {
assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0);
QueryShardContext context = createShardContext();
String queryInvalidLat = "{\n" +
" \"geo_polygon\":{\n" +
" \"" + GEO_POINT_FIELD_NAME + "\":{\n" +
" \"points\":[\n" +
" [-70, 140],\n" +
" [-80, 30],\n" +
" [-90, 20]\n" +
" ]\n" +
" }\n" +
" }\n" +
"}\n";
QueryShardException e1 = expectThrows(QueryShardException.class, () -> parseQuery(queryInvalidLat).toQuery(context));
assertThat(e1.getMessage(), containsString("illegal latitude value [140.0] for [geo_polygon]"));
String queryInvalidLon = "{\n" +
" \"geo_polygon\":{\n" +
" \"" + GEO_POINT_FIELD_NAME + "\":{\n" +
" \"points\":[\n" +
" [-70, 40],\n" +
" [-80, 30],\n" +
" [-190, 20]\n" +
" ]\n" +
" }\n" +
" }\n" +
"}\n";
QueryShardException e2 = expectThrows(QueryShardException.class, () -> parseQuery(queryInvalidLon).toQuery(context));
assertThat(e2.getMessage(), containsString("illegal longitude value [-190.0] for [geo_polygon]"));
}
} }

View File

@ -1,19 +1,12 @@
{ {
"filtered": { "geo_polygon": {
"query": { "location": {
"match_all": {} "points": {
}, "points": [
"filter": { [-70, 40],
"geo_polygon": { [-80, 30],
"location": { [-90, 20]
"points": { ]
"points": [
[-70, 40],
[-80, 30],
[-90, 20]
]
}
}
} }
} }
} }

View File

@ -1,21 +1,13 @@
{ {
"filtered": { "geo_polygon": {
"query": { "location": {
"match_all": {} "points": [
}, [-70, 40],
"filter": { [-80, 30],
"geo_polygon": { [-90, 20]
"location": { ],
"points": [ "something_else": {
[-70, 40],
[-80, 30],
[-90, 20]
],
"something_else": {
}
}
} }
} }
} }

View File

@ -1,12 +1,5 @@
{ {
"filtered": { "geo_polygon": {
"query": { "location": ["WRONG"]
"match_all": {}
},
"filter": {
"geo_polygon": {
"location": ["WRONG"]
}
}
} }
} }

View File

@ -1,19 +1,12 @@
{ {
"filtered": { "geo_polygon": {
"query": { "location": {
"match_all": {} "points": [
[-70, 40],
[-80, 30],
[-90, 20]
]
}, },
"filter": { "bla": true
"geo_polygon": {
"location": {
"points": [
[-70, 40],
[-80, 30],
[-90, 20]
]
},
"bla": true
}
}
} }
} }

View File

@ -1,19 +1,12 @@
{ {
"filtered": { "geo_polygon": {
"query": { "location": {
"match_all": {} "points": [
[-70, 40],
[-80, 30],
[-90, 20]
]
}, },
"filter": { "bla": ["array"]
"geo_polygon": {
"location": {
"points": [
[-70, 40],
[-80, 30],
[-90, 20]
]
},
"bla": ["array"]
}
}
} }
} }