Sort result by distance ranges

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

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/7.9/query-dsl-geo-distance-query.html might be better suited here.

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?

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

"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
			}]
		}
	}

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