# Bug in geo distance search in Java API v1.7.2

**URL:** <https://discuss.elastic.co/t/bug-in-geo-distance-search-in-java-api-v1-7-2/31244>\
**Category:** Elasticsearch\
**Created:** [September 28, 2015, 9:16am UTC](https://discuss.elastic.co/t/bug-in-geo-distance-search-in-java-api-v1-7-2/31244 "2015-09-28T09:16:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ivan\_Batura](https://avatars.discourse-cdn.com/v4/letter/i/b77776/32.png) [@Ivan\_Batura](https://discuss.elastic.co/u/Ivan_Batura)\
**Post date:** [September 28, 2015, 9:16am UTC](https://discuss.elastic.co/t/bug-in-geo-distance-search-in-java-api-v1-7-2/31244/1 "2015-09-28T09:16:41Z")

</div>

There are some similar topic, but I have decided to open new one, as I have found the exact reason of this bug.

_While use org.elasticsearch.index.query.FilterBuilders#geoDistanceFilter_

```
FilterBuilders.geoDistanceFilter("locations").distance("100mi")
      .lat(-70).lon(40);

```

It builds incorrect filter (query) according to the documentation, should be [**lon, lat**], however builder builds [**lat,lon**]:

```
...
"geo_distance" : {
                             "points" : [-70, 40],
                             "distance" : "100mi"
                           }                            
... 

```

Reason:

_org.elasticsearch.index.query.GeoDistanceFilterBuilder_ (lines: 124-126)

```
} else {
    builder.startArray(name).value(lon).value(lat).endArray();
}

```

Thus it builds array with " **name**" and values FIRST - **lon** , SECOND - **lat**.

_org.elasticsearch.common.geo.GeoPoint#resetFromString_ (lines: 65-74)

```
public GeoPoint resetFromString(String value) {
    int comma = value.indexOf(',');
    if (comma != -1) {
        lat = Double.parseDouble(value.substring(0, comma).trim());
        lon = Double.parseDouble(value.substring(comma + 1).trim());
    } else {
        resetFromGeoHash(value);
    }
    return this;
}

```

It parse string(array) and take values FIRST - **lat** , SECOND - **lon**.

Therefore it use incorrect order, as it writes there **lon, lat,** but reads **lat,lon**

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [September 28, 2015, 10:18am UTC](https://discuss.elastic.co/t/bug-in-geo-distance-search-in-java-api-v1-7-2/31244/2 "2015-09-28T10:18:51Z")

</div>

I've asked the dev team to check and comment on this 🙂

---

<div class="post-metadata">

**Author:** ![nknize](https://avatars.discourse-cdn.com/v4/letter/n/edb3f5/32.png) [@nknize](https://discuss.elastic.co/u/nknize)\
**Post date:** [September 28, 2015, 3:11pm UTC](https://discuss.elastic.co/t/bug-in-geo-distance-search-in-java-api-v1-7-2/31244/3 "2015-09-28T15:11:05Z")

</div>

This often causes confusion but the logic is correct. They're two different geo-point representations and parsed in different logic blocks. The first is a GeoJSON lon, lat array - an array of two double field values required in lon, lat order. The second is a human-friendly comma separated string in lat, lon order. The parsing logic for the array:

_org.elasticsearch.index.query.GeoDistanceFilterParser_ (lines: 84 - 87)

```
else if (token == XContentParser.Token.START_ARRAY) {
    fieldName = currentFieldName;
    GeoUtils.parseGeoPoint(parser, point);
}

```

and from _org.elasticsearch.common.geo.GeoUtils_ (lines: 398 - 402)

```
if(element == 1) {
    lon = parser.doubleValue();
} else if(element == 2) {
    lat = parser.doubleValue();
} 

```

The parsing logic for the comma separated string:

_org.elasticsearch.index.query.GeoDistanceFilterParser.java_ (lines: 137 - 140)

```
} else {
    point.resetFromString(parser.text());
    fieldName = currentFieldName;
}
```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 11:47pm UTC](https://discuss.elastic.co/t/bug-in-geo-distance-search-in-java-api-v1-7-2/31244/4 "2017-07-05T23:47:44Z")

</div>


