From c08ad0cad674773ecdbc8300ddf252fb1ff8a6f9 Mon Sep 17 00:00:00 2001 From: Adriano Ferreira Date: Tue, 26 Oct 2010 15:02:58 -0200 Subject: [PATCH] Fix parsing of "geo_distance" filter wrt "distance" / "unit" parameters The problem was that when "unit" was given, the conversion to miles was happening too early, which caused wrong computations. This change postpones this computation when one really knows which unit should be used. --- .../query/xcontent/GeoDistanceFilterParser.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/xcontent/GeoDistanceFilterParser.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/xcontent/GeoDistanceFilterParser.java index a43a55bd274..fc9922cfcd2 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/xcontent/GeoDistanceFilterParser.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/query/xcontent/GeoDistanceFilterParser.java @@ -74,7 +74,8 @@ public class GeoDistanceFilterParser extends AbstractIndexComponent implements X double lon = 0; String fieldName = null; double distance = 0; - DistanceUnit unit = null; + Object vDistance = null; + DistanceUnit unit = DistanceUnit.KILOMETERS; // default unit GeoDistance geoDistance = GeoDistance.ARC; while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { @@ -110,9 +111,9 @@ public class GeoDistanceFilterParser extends AbstractIndexComponent implements X } else if (token.isValue()) { if (currentFieldName.equals("distance")) { if (token == XContentParser.Token.VALUE_STRING) { - distance = DistanceUnit.parse(parser.text(), DistanceUnit.KILOMETERS, DistanceUnit.MILES); + vDistance = parser.text(); // a String } else { - distance = parser.doubleValue(); + vDistance = parser.numberValue(); // a Number } } else if (currentFieldName.equals("unit")) { unit = DistanceUnit.fromString(parser.text()); @@ -150,8 +151,10 @@ public class GeoDistanceFilterParser extends AbstractIndexComponent implements X } } - if (unit != null) { - distance = unit.toMiles(distance); + if (vDistance instanceof Number) { + distance = unit.toMiles(((Number)vDistance).doubleValue()); + } else { + distance = DistanceUnit.parse((String)vDistance, unit, DistanceUnit.MILES); } MapperService mapperService = parseContext.mapperService();