This version of the OpenSearch documentation is no longer maintained. For the latest version, see the current documentation. For information about OpenSearch version maintenance, see Release Schedule and Maintenance Policy.

Geodistance aggregations

The geo_distance aggregation groups documents into concentric circles based on distances from an origin geo_point field. It’s the same as the range aggregation, except that it works on geo locations.

For example, you can use the geo_distance aggregation to find all pizza places within 1 km of you. The search results are limited to the 1 km radius specified by you, but you can add another result found within 2 km.

You can only use the geo_distance aggregation on fields mapped as geo_point.

A point is a single geographical coordinate, such as your current location shown by your smart-phone. A point in OpenSearch is represented as follows:

  1. {
  2. "location": {
  3. "type": "point",
  4. "coordinates": {
  5. "lat": 83.76,
  6. "lon": -81.2
  7. }
  8. }
  9. }

You can also specify the latitude and longitude as an array [-81.20, 83.76] or as a string "83.76, -81.20"

This table lists the relevant fields of a geo_distance aggregation:

FieldDescriptionRequired
fieldSpecify the geopoint field that you want to work on.Yes
originSpecify the geopoint that’s used to compute the distances from.Yes
rangesSpecify a list of ranges to collect documents based on their distance from the target point.Yes
unitDefine the units used in the ranges array. The unit defaults to m (meters), but you can switch to other units like km (kilometers), mi (miles), in (inches), yd (yards), cm (centimeters), and mm (millimeters).No
distance_typeSpecify how OpenSearch calculates the distance. The default is sloppy_arc (faster but less accurate), but can also be set to arc (slower but most accurate) or plane (fastest but least accurate). Because of high error margins, use plane only for small geographic areas.No

The syntax is as follows:

  1. {
  2. "aggs": {
  3. "aggregation_name": {
  4. "geo_distance": {
  5. "field": "field_1",
  6. "origin": "x, y",
  7. "ranges": [
  8. {
  9. "to": "value_1"
  10. },
  11. {
  12. "from": "value_2",
  13. "to": "value_3"
  14. },
  15. {
  16. "from": "value_4"
  17. }
  18. ]
  19. }
  20. }
  21. }
  22. }

This example forms buckets from the following distances from a geo-point field:

  • Fewer than 10 km
  • From 10 to 20 km
  • From 20 to 50 km
  • From 50 to 100 km
  • Above 100 km
  1. GET opensearch_dashboards_sample_data_logs/_search
  2. {
  3. "size": 0,
  4. "aggs": {
  5. "position": {
  6. "geo_distance": {
  7. "field": "geo.coordinates",
  8. "origin": {
  9. "lat": 83.76,
  10. "lon": -81.2
  11. },
  12. "ranges": [
  13. {
  14. "to": 10
  15. },
  16. {
  17. "from": 10,
  18. "to": 20
  19. },
  20. {
  21. "from": 20,
  22. "to": 50
  23. },
  24. {
  25. "from": 50,
  26. "to": 100
  27. },
  28. {
  29. "from": 100
  30. }
  31. ]
  32. }
  33. }
  34. }
  35. }

copy

Example response

  1. ...
  2. "aggregations" : {
  3. "position" : {
  4. "buckets" : [
  5. {
  6. "key" : "*-10.0",
  7. "from" : 0.0,
  8. "to" : 10.0,
  9. "doc_count" : 0
  10. },
  11. {
  12. "key" : "10.0-20.0",
  13. "from" : 10.0,
  14. "to" : 20.0,
  15. "doc_count" : 0
  16. },
  17. {
  18. "key" : "20.0-50.0",
  19. "from" : 20.0,
  20. "to" : 50.0,
  21. "doc_count" : 0
  22. },
  23. {
  24. "key" : "50.0-100.0",
  25. "from" : 50.0,
  26. "to" : 100.0,
  27. "doc_count" : 0
  28. },
  29. {
  30. "key" : "100.0-*",
  31. "from" : 100.0,
  32. "doc_count" : 14074
  33. }
  34. ]
  35. }
  36. }
  37. }