# Distance calculation and Distance sorting not correct

**URL:** <https://discuss.elastic.co/t/distance-calculation-and-distance-sorting-not-correct/37623>\
**Category:** Elasticsearch\
**Created:** [December 19, 2015, 3:46pm UTC](https://discuss.elastic.co/t/distance-calculation-and-distance-sorting-not-correct/37623 "2015-12-19T15:46:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![antondachauer](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@antondachauer](https://discuss.elastic.co/u/antondachauer)\
**Post date:** [December 19, 2015, 3:46pm UTC](https://discuss.elastic.co/t/distance-calculation-and-distance-sorting-not-correct/37623/1 "2015-12-19T15:46:57Z")

</div>

Hi everybody,

I'm using elasticsearch for a search functionality on a website. The search must provide a search by distance from a location in an - for example - 25km radius.

That's no problem, I'm using the following elaticsearch query array:  
`{ "index": "kasd9i9021profiles", "type": "profile", "size": 30, "from": 0, "body": { "query": { "bool": { "filter": { "geo_distance": { "distance": "25km", "distance_type": "sloppy_arc", "_cache": true, "location": { "lat": 48.136833417046, "lon": 11.570900696682 }, "unit": "km" } }, "must": [{ "term": { "published": "1" } }, { "match": { "country": "DE" } }, { "match": { "category_id": "1" } }] } }, "script_fields": { "distance": { "lang": "groovy", "params": { "lat": 48.136833417046, "lon": 11.570900696682 }, "script": "doc['location'].distanceInKm(lat,lon)" } }, "sort": [{ "upgrade_sort": { "order": "desc" } }, { "has_siegel": { "order": "desc" } }, { "_geo_distance": { "location": { "lat": 48.136833417046, "lon": 11.570900696682 }, "order": "asc", "unit": "km" } }] }, "fields": ["_source", "distance"] }`

The value of "upgrade\_sort" can be between 0 and 3.  
The value of "has\_siegel" can be true or false.

**The problems are:**

1. The results are not sorted by distance
2. The distance in the result is between 0 and ~30km, not between 0 and 25km.

Is that a bug or a wrong query?

---

<div class="post-metadata">

**Author:** ![antondachauer](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@antondachauer](https://discuss.elastic.co/u/antondachauer)\
**Post date:** [December 22, 2015, 5:31pm UTC](https://discuss.elastic.co/t/distance-calculation-and-distance-sorting-not-correct/37623/2 "2015-12-22T17:31:57Z")

</div>

I also postet my questen on stackoverflow. Is there anyone who can help? [http://stackoverflow.com/questions/34372194/elasticsearch-distance-sorting-and-calculation-incorrect-and-inaccurate](http://stackoverflow.com/questions/34372194/elasticsearch-distance-sorting-and-calculation-incorrect-and-inaccurate)

---

<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:29pm UTC](https://discuss.elastic.co/t/distance-calculation-and-distance-sorting-not-correct/37623/3 "2017-07-05T23:29:15Z")

</div>


