Geodistance aggregation

Geodistance aggregation helps in creating buckets of distances from a given geo-point. This can be better illustrated using a diagram:

Fig 4.2 Geodistance aggregation with only to specified (left), and both to and from specified (right)

The shaded area in blue represents the area included in the geodistance aggregation.

The following aggregation will form a bucket with all the documents within the given distance from the given geo-point. This corresponds to the first (left) circle in the preceding diagram. The shaded area is from the center up to the given radius, forming a circle:

GET bigginsight/_search?size=0
{
"aggs": {
"within_radius": {
"geo_distance": {
"field": "location",
"origin": {"lat": 23.102869,"lon": 72.595692},
"ranges": [{"to": 5}]
}
}
}
}

As you can see, the ranges parameter is similar to the range aggregation that we saw earlier. It includes all the points up to 5 meters away from the given origin specified. This is helpful in aggregations like getting the counts of things that are within 2 kilometers from a given location, and is often used on many websites. This is a good way to find all businesses within a given distance of your location (such as all coffee shops or hospitals within 2 km).

The default unit of distance is meters, but you can specify the unit parameter as km, mi, and so on, to switch to different units.

Now, let's look at what happens if you specify both from and to in the geodistance aggregation. This will correspond to the right circle in the preceding diagram:

GET bigginsight/_search?size=0
{
"aggs": {
"within_radius": {
"geo_distance": {
"field": "location",
"origin": {"lat": 23.102869,"lon": 72.595692},
"ranges": [{"from": 5, "to": 10}]
}
}
}
}

Here, we are bucketing the points that are at least 5 meters away, but less than 10 meters away, from the given point. Similarly, it is possible to form a bucket of a point which is at least x units away from the given origin, by only specifying the from parameter.

Now, let's look at GeoHash grid aggregation.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset