# Stats aggregation on latitude or longitude of a geo\_point

**URL:** <https://discuss.elastic.co/t/stats-aggregation-on-latitude-or-longitude-of-a-geo-point/294070>\
**Category:** Elasticsearch\
**Created:** [January 11, 2022, 6:27pm UTC](https://discuss.elastic.co/t/stats-aggregation-on-latitude-or-longitude-of-a-geo-point/294070 "2022-01-11T18:27:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![seb\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/seb_2/32/1419_2.png) [@seb\_2](https://discuss.elastic.co/u/seb_2)\
**Post date:** [January 11, 2022, 6:27pm UTC](https://discuss.elastic.co/t/stats-aggregation-on-latitude-or-longitude-of-a-geo-point/294070/1 "2022-01-11T18:27:30Z")

</div>

Hi  
I use geo\_point data and I'd like to have stats aggregation on latitude.  
like

```auto
      "aggs": {
        "lat": {
          "avg": {
            "field": "location.lat"
          }
        },

```

where location is a geo\_point type.

It doesn't work. I succeeded using a script but it's slow. Any idea how to do that?  
My current workaround is to also have a lat and lng field as double in my index. But it doesn't make sense to double the info.

Thanks  
Seb

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 11, 2022, 7:02pm UTC](https://discuss.elastic.co/t/stats-aggregation-on-latitude-or-longitude-of-a-geo-point/294070/2 "2022-01-11T19:02:40Z")

</div>

That's the best option IMO. You could also use a slower runtime field I believe.

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [January 11, 2022, 10:29pm UTC](https://discuss.elastic.co/t/stats-aggregation-on-latitude-or-longitude-of-a-geo-point/294070/3 "2022-01-11T22:29:40Z")

</div>

You either use a script, which is expensive, or you store the points as doubles. The latter sounds more expensive, but it's probably not.

---

<div class="post-metadata">

**Author:** ![Ignacio\_Vera](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ignacio_vera/32/36674_2.png) [@Ignacio\_Vera](https://discuss.elastic.co/u/Ignacio_Vera)\
**Post date:** [January 12, 2022, 7:38am UTC](https://discuss.elastic.co/t/stats-aggregation-on-latitude-or-longitude-of-a-geo-point/294070/4 "2022-01-12T07:38:44Z")

</div>

I guess you can use the geo\_centroid aggregation as the latitude of the centroid should be equivalent to the latitude average. Of course you will be doing a bit more work as you are computing the average of the longitude too.

---

<div class="post-metadata">

**Author:** ![seb\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/seb_2/32/1419_2.png) [@seb\_2](https://discuss.elastic.co/u/seb_2)\
**Post date:** [January 12, 2022, 9:30am UTC](https://discuss.elastic.co/t/stats-aggregation-on-latitude-or-longitude-of-a-geo-point/294070/5 "2022-01-12T09:30:01Z")

</div>

geo\_centroid looks a good approach to my use case! thanks

---

<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:** [February 9, 2022, 9:30am UTC](https://discuss.elastic.co/t/stats-aggregation-on-latitude-or-longitude-of-a-geo-point/294070/6 "2022-02-09T09:30:09Z")

</div>

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