elasticsearch

how to do custom grouping of aggregations?


lets say I have a bunch of aggregation like:

  "aggs": {
        "minValue": {
            "min": {
                "field": "field1"
            }
        },
        "maxValue": {
            "max": {
                "field": "field1"
            }
        },
        "sumValue": {
            "sum": {
                "field": "field1"
            }
        },
         "terms1": {
            "terms": {
                "field": "field3"
            }
        },
         "terms2": {
            "terms": {
                "field": "field4"
            }
        }
}

The result is something like that:

"aggregations": {
        "minValue": {
            "value": 4
        },
        "maxValue": {
            "value": 100
        },
        "sumValue": {
            "value": 43342
        },
       "terms1": {
          "doc_count_error_upper_bound": 0,   
          "sum_other_doc_count": 0,           
          "buckets": [                        
            {
             "key": "electronic",
             "doc_count": 6
            } ..etc
          ]
        },
       "terms2": {
          "doc_count_error_upper_bound": 0,   
          "sum_other_doc_count": 0,           
          "buckets": [                        
           {
            "key": "bmw",
            "doc_count": 4
           } ..etc
          ]
        }
}

I would like to get the result with some user specified custom logical groupping. like value_aggr term_aggr result:

"aggregations": {
  "value_aggr": {
        "minValue": {
            "value": 4
        },
        "maxValue": {
            "value": 100
        },
        "sumValue": {
            "value": 43342
        },
},
 "term_aggr": {
       "terms1": {
          "doc_count_error_upper_bound": 0,   
          "sum_other_doc_count": 0,           
          "buckets": [                        
            {
             "key": "electronic",
             "doc_count": 6
            } ..etc
          ]
        },
       "terms2": {
          "doc_count_error_upper_bound": 0,   
          "sum_other_doc_count": 0,           
          "buckets": [                        
           {
            "key": "bmw",
            "doc_count": 4
           } ..etc
          ]
        }
}
}

note: the reason for this is that it is much easier to look up the results when we have a bunch of aggregation.Much easier to distinguish the type of aggregations on the client side due to the extra levels.


Solution

  • So the closest I could get is to create subaggregations for an aggregation whichs has an empty bool filter:

    "aggregations": {
         "value_aggr": {
                    "filter": {
                        "bool": {
                       
                        }
                    },
                    "aggregations": {
                        "agg1": {
                            "min": {
                                "field": "f1"
                            }
                        },
                        "agg2": {
                            "max": {
                                "field": "f2"
                            }
                        }
                       ...aggN
                    }
                },
         "terms_aggr": {
            ....
          }
    
    }