Skip to content

Commit 3f446c3

Browse files
committed
Improve error detection in geo_filter parsing
Relates to #5370
1 parent bc29ff0 commit 3f446c3

File tree

7 files changed

+127
-8
lines changed

7 files changed

+127
-8
lines changed

src/main/java/org/elasticsearch/index/query/GeoPolygonFilterParser.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
public class GeoPolygonFilterParser implements FilterParser {
5454

5555
public static final String NAME = "geo_polygon";
56-
public static final String POINTS = "points";
56+
public static final String POINTS = "points";
5757

5858
@Inject
5959
public GeoPolygonFilterParser() {
@@ -91,13 +91,15 @@ public Filter parse(QueryParseContext parseContext) throws IOException, QueryPar
9191
if (token == XContentParser.Token.FIELD_NAME) {
9292
currentFieldName = parser.currentName();
9393
} else if (token == XContentParser.Token.START_ARRAY) {
94-
if(POINTS.equals(currentFieldName)) {
95-
while((token = parser.nextToken()) != Token.END_ARRAY) {
94+
if (POINTS.equals(currentFieldName)) {
95+
while ((token = parser.nextToken()) != Token.END_ARRAY) {
9696
shell.add(GeoPoint.parse(parser));
9797
}
9898
} else {
9999
throw new QueryParsingException(parseContext.index(), "[geo_polygon] filter does not support [" + currentFieldName + "]");
100100
}
101+
} else {
102+
throw new QueryParsingException(parseContext.index(), "[geo_polygon] filter does not support token type [" + token.name() + "] under [" + currentFieldName + "]");
101103
}
102104
}
103105
} else if (token.isValue()) {
@@ -113,20 +115,22 @@ public Filter parse(QueryParseContext parseContext) throws IOException, QueryPar
113115
} else {
114116
throw new QueryParsingException(parseContext.index(), "[geo_polygon] filter does not support [" + currentFieldName + "]");
115117
}
118+
} else {
119+
throw new QueryParsingException(parseContext.index(), "[geo_polygon] unexpected token type [" + token.name() + "]");
116120
}
117121
}
118122

119123
if (shell.isEmpty()) {
120124
throw new QueryParsingException(parseContext.index(), "no points defined for geo_polygon filter");
121125
} else {
122-
if(shell.size() < 3) {
126+
if (shell.size() < 3) {
123127
throw new QueryParsingException(parseContext.index(), "to few points defined for geo_polygon filter");
124128
}
125129
GeoPoint start = shell.get(0);
126-
if(!start.equals(shell.get(shell.size()-1))) {
130+
if (!start.equals(shell.get(shell.size() - 1))) {
127131
shell.add(start);
128132
}
129-
if(shell.size() < 4) {
133+
if (shell.size() < 4) {
130134
throw new QueryParsingException(parseContext.index(), "to few points defined for geo_polygon filter");
131135
}
132136
}

src/test/java/org/elasticsearch/index/query/SimpleIndexQueryParserTests.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2038,6 +2038,29 @@ public void testGeoPolygonNamedFilter() throws IOException {
20382038
assertThat(filter.points()[2].lon(), closeTo(-90, 0.00001));
20392039
}
20402040

2041+
2042+
@Test
2043+
public void testGeoPolygonFilterParsingExceptions() throws IOException {
2044+
String[] brokenFiles = new String[]{
2045+
"/org/elasticsearch/index/query/geo_polygon_exception_1.json",
2046+
"/org/elasticsearch/index/query/geo_polygon_exception_2.json",
2047+
"/org/elasticsearch/index/query/geo_polygon_exception_3.json",
2048+
"/org/elasticsearch/index/query/geo_polygon_exception_4.json",
2049+
"/org/elasticsearch/index/query/geo_polygon_exception_5.json"
2050+
};
2051+
for (String brokenFile : brokenFiles) {
2052+
IndexQueryParserService queryParser = queryParser();
2053+
String query = copyToStringFromClasspath(brokenFile);
2054+
try {
2055+
queryParser.parse(query).query();
2056+
fail("parsing a broken geo_polygon filter didn't fail as expected while parsing: " + brokenFile);
2057+
} catch (QueryParsingException e) {
2058+
// success!
2059+
}
2060+
}
2061+
}
2062+
2063+
20412064
@Test
20422065
public void testGeoPolygonFilter1() throws IOException {
20432066
IndexQueryParserService queryParser = queryParser();
@@ -2181,7 +2204,7 @@ public void testFilterParsing() throws IOException {
21812204
Query parsedQuery = queryParser.parse(query).query();
21822205
assertThat((double) (parsedQuery.getBoost()), Matchers.closeTo(3.0, 1.e-7));
21832206
}
2184-
2207+
21852208
@Test
21862209
public void testBadTypeMatchQuery() throws Exception {
21872210
IndexQueryParserService queryParser = queryParser();
@@ -2193,7 +2216,7 @@ public void testBadTypeMatchQuery() throws Exception {
21932216
expectedException = qpe;
21942217
}
21952218
assertThat(expectedException, notNullValue());
2196-
}
2219+
}
21972220

21982221
@Test
21992222
public void testMultiMatchQuery() throws Exception {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"filtered": {
3+
"query": {
4+
"match_all": {}
5+
},
6+
"filter": {
7+
"geo_polygon": {
8+
"location": {
9+
"points": {
10+
"points": [
11+
[-70, 40],
12+
[-80, 30],
13+
[-90, 20]
14+
]
15+
}
16+
}
17+
}
18+
}
19+
}
20+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"filtered": {
3+
"query": {
4+
"match_all": {}
5+
},
6+
"filter": {
7+
"geo_polygon": {
8+
"location": {
9+
"points": [
10+
[-70, 40],
11+
[-80, 30],
12+
[-90, 20]
13+
],
14+
"something_else": {
15+
16+
}
17+
18+
}
19+
}
20+
}
21+
}
22+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"filtered": {
3+
"query": {
4+
"match_all": {}
5+
},
6+
"filter": {
7+
"geo_polygon": {
8+
"location": ["WRONG"]
9+
}
10+
}
11+
}
12+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"filtered": {
3+
"query": {
4+
"match_all": {}
5+
},
6+
"filter": {
7+
"geo_polygon": {
8+
"location": {
9+
"points": [
10+
[-70, 40],
11+
[-80, 30],
12+
[-90, 20]
13+
]
14+
},
15+
"bla": true
16+
}
17+
}
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"filtered": {
3+
"query": {
4+
"match_all": {}
5+
},
6+
"filter": {
7+
"geo_polygon": {
8+
"location": {
9+
"points": [
10+
[-70, 40],
11+
[-80, 30],
12+
[-90, 20]
13+
]
14+
},
15+
"bla": ["array"]
16+
}
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)