From ad8f5e7e4b8900117d8d6f0a4fe6994a30fc3b44 Mon Sep 17 00:00:00 2001 From: javanna Date: Wed, 3 Aug 2016 19:11:54 +0200 Subject: [PATCH] Throw parsing error if geo_distance query contains multiple fields Geo distance Query, like many other queries, used to parse even when the query referred to multiple fields and the last one would win. We rather throw an exception now instead. --- .../index/query/GeoDistanceQueryBuilder.java | 5 ++++- .../query/GeoDistanceQueryBuilderTests.java | 21 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/elasticsearch/index/query/GeoDistanceQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/GeoDistanceQueryBuilder.java index 92b30747a14..1233df11714 100644 --- a/core/src/main/java/org/elasticsearch/index/query/GeoDistanceQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/GeoDistanceQueryBuilder.java @@ -359,9 +359,12 @@ public class GeoDistanceQueryBuilder extends AbstractQueryBuilder field : { lat : 30, lon : 12 } String currentName = parser.currentName(); - assert currentFieldName != null; fieldName = currentFieldName; while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { diff --git a/core/src/test/java/org/elasticsearch/index/query/GeoDistanceQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/GeoDistanceQueryBuilderTests.java index 387df7ac3ca..decdf50b168 100644 --- a/core/src/test/java/org/elasticsearch/index/query/GeoDistanceQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/GeoDistanceQueryBuilderTests.java @@ -23,6 +23,7 @@ import org.apache.lucene.search.MatchNoDocsQuery; import org.apache.lucene.search.Query; import org.apache.lucene.spatial.geopoint.search.GeoPointDistanceQuery; import org.elasticsearch.Version; +import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.geo.GeoDistance; import org.elasticsearch.common.geo.GeoPoint; import org.elasticsearch.common.geo.GeoUtils; @@ -474,4 +475,24 @@ public class GeoDistanceQueryBuilderTests extends AbstractQueryTestCase failingQueryBuilder.toQuery(shardContext)); assertThat(e.getMessage(), containsString("failed to find geo_point field [unmapped]")); } + + public void testParseFailsWithMultipleFields() throws IOException { + String json = "{\n" + + " \"geo_distance\" : {\n" + + " \"point1\" : {\n" + + " \"lat\" : 30, \"lon\" : 12\n" + + " },\n" + + " \"point2\" : {\n" + + " \"lat\" : 30, \"lon\" : 12\n" + + " }\n" + + " }\n" + + "}"; + + try { + parseQuery(json); + fail("parseQuery should have failed"); + } catch(ParsingException e) { + assertEquals("[geo_distance] query doesn't support multiple fields, found [point1] and [point2]", e.getMessage()); + } + } }