# Sort result by distance ranges

**URL:** <https://discuss.elastic.co/t/sort-result-by-distance-ranges/247286>\
**Category:** Elasticsearch\
**Created:** [September 2, 2020, 8:22pm UTC](https://discuss.elastic.co/t/sort-result-by-distance-ranges/247286 "2020-09-02T20:22:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![r3vile](https://avatars.discourse-cdn.com/v4/letter/r/ecb155/32.png) [@r3vile](https://discuss.elastic.co/u/r3vile)\
**Post date:** [September 2, 2020, 8:22pm UTC](https://discuss.elastic.co/t/sort-result-by-distance-ranges/247286/1 "2020-09-02T20:22:29Z")

</div>

I have a document with many restaurants including their location.  
Is it possible to get all of them in a 100km range and group them into 3 ranges. So I will first see all which are at max 10km away from my location, sorted randomly. Followed by those which are at max 50km away and so on:

- 1km - 10km
- 10 - 50km
- 50 - 100km

---

<div class="post-metadata">

**Author:** ![lzukel](https://avatars.discourse-cdn.com/v4/letter/l/4491bb/32.png) [@lzukel](https://discuss.elastic.co/u/lzukel)\
**Post date:** [September 2, 2020, 11:15pm UTC](https://discuss.elastic.co/t/sort-result-by-distance-ranges/247286/2 "2020-09-02T23:15:22Z")

</div>

A lot of what you are asking is going to depend on how your data is indexed.

I would think it possible to use a range aggregation:  
[https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-range-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-range-aggregation.html)

---

<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 2, 2020, 11:38pm UTC](https://discuss.elastic.co/t/sort-result-by-distance-ranges/247286/3 "2020-09-02T23:38:19Z")

</div>

[https://www.elastic.co/guide/en/elasticsearch/reference/7.9/query-dsl-geo-distance-query.html](https://www.elastic.co/guide/en/elasticsearch/reference/7.9/query-dsl-geo-distance-query.html) might be better suited here.

---

<div class="post-metadata">

**Author:** ![r3vile](https://avatars.discourse-cdn.com/v4/letter/r/ecb155/32.png) [@r3vile](https://discuss.elastic.co/u/r3vile)\
**Post date:** [September 3, 2020, 2:48pm UTC](https://discuss.elastic.co/t/sort-result-by-distance-ranges/247286/4 "2020-09-03T14:48:37Z")

</div>

I have tried to use geo-distance-query but then i get all results ordered by distance.  
But i only want to order them in steps of xkm.  
So a restaurant with a distance of 1km should have the exact same score than one with a distance of 2km. How can i achieve this?

---

<div class="post-metadata">

**Author:** ![r3vile](https://avatars.discourse-cdn.com/v4/letter/r/ecb155/32.png) [@r3vile](https://discuss.elastic.co/u/r3vile)\
**Post date:** [September 3, 2020, 3:05pm UTC](https://discuss.elastic.co/t/sort-result-by-distance-ranges/247286/5 "2020-09-03T15:05:32Z")

</div>

I played around with aggregation and get the buckets as result, but how can i order my hits with that?

```auto
"aggregations": {
		"distances": {
			"buckets": [{
				"key": "*-10.0",
				"from": 0,
				"to": 10,
				"doc_count": 60
			}, {
				"key": "10.0-20.0",
				"from": 10,
				"to": 20,
				"doc_count": 45
			}, {
				"key": "20.0-30.0",
				"from": 20,
				"to": 30,
				"doc_count": 30
			}]
		}
	}

```

---

<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:** [October 1, 2020, 3:05pm UTC](https://discuss.elastic.co/t/sort-result-by-distance-ranges/247286/6 "2020-10-01T15:05:39Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
