Skip to content

Commit c08ad0c

Browse files
Adriano Ferreirakimchy
authored andcommitted
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.
1 parent 9b20614 commit c08ad0c

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

modules/elasticsearch/src/main/java/org/elasticsearch/index/query/xcontent/GeoDistanceFilterParser.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ public class GeoDistanceFilterParser extends AbstractIndexComponent implements X
7474
double lon = 0;
7575
String fieldName = null;
7676
double distance = 0;
77-
DistanceUnit unit = null;
77+
Object vDistance = null;
78+
DistanceUnit unit = DistanceUnit.KILOMETERS; // default unit
7879
GeoDistance geoDistance = GeoDistance.ARC;
7980
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
8081
if (token == XContentParser.Token.FIELD_NAME) {
@@ -110,9 +111,9 @@ public class GeoDistanceFilterParser extends AbstractIndexComponent implements X
110111
} else if (token.isValue()) {
111112
if (currentFieldName.equals("distance")) {
112113
if (token == XContentParser.Token.VALUE_STRING) {
113-
distance = DistanceUnit.parse(parser.text(), DistanceUnit.KILOMETERS, DistanceUnit.MILES);
114+
vDistance = parser.text(); // a String
114115
} else {
115-
distance = parser.doubleValue();
116+
vDistance = parser.numberValue(); // a Number
116117
}
117118
} else if (currentFieldName.equals("unit")) {
118119
unit = DistanceUnit.fromString(parser.text());
@@ -150,8 +151,10 @@ public class GeoDistanceFilterParser extends AbstractIndexComponent implements X
150151
}
151152
}
152153

153-
if (unit != null) {
154-
distance = unit.toMiles(distance);
154+
if (vDistance instanceof Number) {
155+
distance = unit.toMiles(((Number)vDistance).doubleValue());
156+
} else {
157+
distance = DistanceUnit.parse((String)vDistance, unit, DistanceUnit.MILES);
155158
}
156159

157160
MapperService mapperService = parseContext.mapperService();

0 commit comments

Comments
 (0)