How to achieve Elasticsearch aggregation by grids of specific size?

Basically, what I have is a polygon that I use to filter my data through a Geoshape query. What I would like to achieve is to apply an Elasticsearch aggregation to that query that gives me a bucket for every grid of an specific size, for example, 1km, inside that polygon.

Of course, the aggregation is done over a Geopoint type field.

I've tried the Geohash aggregation grid, because it allows me to specify a precision of "1km". But results don't always have the exact form of a grid, it is just an aproximation.

Here's my current query:

  {
      "query": {
        "bool": {
          "should": [
            {
              "bool": {
                "must": [
                  {
                    "range": {
                      "timestamp": {
                        "gte": 1662012000000,
                        "lte": 1662098399999
                      }
                    }
                  }
                ],
                "filter": {
                  "geo_shape": {
                    "location": {
                      "shape": {
                        "type": "polygon",
                        "coordinates": [
                          [
                            [
                              -84.5214136675492,
                              9.911814727700381
                            ],
                            [
                              -85.02380959494664,
                              10.897883119232858
                            ],
                            [
                              -85.02380959494664,
                              10.90692961823774
                            ],
                            ....
                          ]
                        ]
                      },
                      "relation": "WITHIN"
                    }
                  }
                }
              }
            }
          ]
        }
      },
      "size": 0,
      "_source": true,
      "sort": [
        {
          "timestamp": {
            "order": "asc"
          }
        }
      ],"aggs": {
        "1km-grids": {
          "geohash_grid": {
            "field": "location",
            "precision": "1km"
          },
          "aggs": {
            "sales": {
              "avg": {
                "field": "finales.tiempo_mdev"
              }
            }
          }
        }
      }
    }

Is there any way to achive what I'm looking for?

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