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.
This commit is contained in:
javanna 2016-08-03 19:11:54 +02:00 committed by Luca Cavanna
parent 195320f2d6
commit ad8f5e7e4b
2 changed files with 25 additions and 1 deletions

View File

@ -359,9 +359,12 @@ public class GeoDistanceQueryBuilder extends AbstractQueryBuilder<GeoDistanceQue
fieldName = currentFieldName;
GeoUtils.parseGeoPoint(parser, point);
} else if (token == XContentParser.Token.START_OBJECT) {
if (fieldName != null) {
throw new ParsingException(parser.getTokenLocation(), "[geo_distance] query doesn't support multiple fields, found ["
+ fieldName + "] and [" + currentFieldName + "]");
}
// the json in the format of -> 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) {

View File

@ -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<GeoDista
QueryShardException e = expectThrows(QueryShardException.class, () -> 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());
}
}
}