# How to achieve Elasticsearch aggregation by grids of specific size?

**URL:** <https://discuss.elastic.co/t/how-to-achieve-elasticsearch-aggregation-by-grids-of-specific-size/314725>\
**Category:** Elasticsearch\
**Created:** [September 19, 2022, 10:02pm UTC](https://discuss.elastic.co/t/how-to-achieve-elasticsearch-aggregation-by-grids-of-specific-size/314725 "2022-09-19T22:02:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel\_Garcia](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/daniel_garcia/32/75104_2.png) [@Daniel\_Garcia](https://discuss.elastic.co/u/Daniel_Garcia)\
**Post date:** [September 19, 2022, 10:02pm UTC](https://discuss.elastic.co/t/how-to-achieve-elasticsearch-aggregation-by-grids-of-specific-size/314725/1 "2022-09-19T22:02:40Z")

</div>

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](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-shape-query.html#query-dsl-geo-shape-query), 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:

```auto
  {
      "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?

---

<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 17, 2022, 10:03pm UTC](https://discuss.elastic.co/t/how-to-achieve-elasticsearch-aggregation-by-grids-of-specific-size/314725/2 "2022-10-17T22:03:06Z")

</div>

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