# Combine Terms and GeohashGrid (?) aggregations for merged results

**URL:** <https://discuss.elastic.co/t/combine-terms-and-geohashgrid-aggregations-for-merged-results/142894>\
**Category:** Elasticsearch\
**Created:** [August 3, 2018, 11:06am UTC](https://discuss.elastic.co/t/combine-terms-and-geohashgrid-aggregations-for-merged-results/142894 "2018-08-03T11:06:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Destroy666](https://avatars.discourse-cdn.com/v4/letter/d/e9a140/32.png) [@Destroy666](https://discuss.elastic.co/u/Destroy666)\
**Post date:** [August 3, 2018, 11:06am UTC](https://discuss.elastic.co/t/combine-terms-and-geohashgrid-aggregations-for-merged-results/142894/1 "2018-08-03T11:06:47Z")

</div>

I have following data - areas with `boundingBoxCenter` `geo_point` field and businesses with `areaIds` array and `location` `geo_point` field.

For now I am aggregating them just by `Terms`, by setting `include` of `areaIds` to the values I currently need. But there are too many areas next to each other on some map levels, so I would like to combine them based on density, e.g. 3 closest areas get merged based on dynamic precision.

I thought using `GeohashGrid` as a subaggregation would do it, but I don't see a way to get the center points of `TermsAggregation` without processing it by API first.

Is the problem solvable by Elastic in a single request with a combination of subaggregations by default? Or would I need to write some sort of a script or do the second part in API?

EDIT: also, ideally, I'd like to have results like this:

> ```
> buckets {
> {
> doc_count: 47,
> subAggregation: {
> buckets: {
> {
> key: area_id,
> doc_count: 23,
> },
> {
> key: another_area_id,
> doc_count: 24,
> }
> }
> }
> },
> {
> doc_count: 90,
> subAggregation: {
> buckets: {
> ...
> }
> }
> }
> }
> 
> ```

Or the other way round when it comes to main and sub aggregation. But any hints are very welcome.

---

<div class="post-metadata">

**Author:** ![Destroy666](https://avatars.discourse-cdn.com/v4/letter/d/e9a140/32.png) [@Destroy666](https://discuss.elastic.co/u/Destroy666)\
**Post date:** [August 11, 2018, 11:57pm UTC](https://discuss.elastic.co/t/combine-terms-and-geohashgrid-aggregations-for-merged-results/142894/2 "2018-08-11T23:57:08Z")

</div>

So, I assume this is impossible with default Elastisearch aggregations?

---

<div class="post-metadata">

**Author:** ![Destroy666](https://avatars.discourse-cdn.com/v4/letter/d/e9a140/32.png) [@Destroy666](https://discuss.elastic.co/u/Destroy666)\
**Post date:** [August 16, 2018, 10:11am UTC](https://discuss.elastic.co/t/combine-terms-and-geohashgrid-aggregations-for-merged-results/142894/3 "2018-08-16T10:11:45Z")

</div>

Sorry for bumping again. Let me know if you need more info. If not I'll report it as feature request on GH next week since it looks impossible to me.

---

<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:** [September 13, 2018, 10:22am UTC](https://discuss.elastic.co/t/combine-terms-and-geohashgrid-aggregations-for-merged-results/142894/4 "2018-09-13T10:22:35Z")

</div>

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