Combine Terms and GeohashGrid (?) aggregations for merged results

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.

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

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.

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