code: function make_bucket_agg(groupby, subagg) {
      var bucket_agg = {};
      groupby.forEach(function(field) {
          bucket_agg[field] = {
              terms: {
                  field: field,
                  size: _calculate_size_for_aggregation(subagg)
              }
          };
      });
  
      return _.reduceRight(bucket_agg, function nest(memo, value, key) {
          var obj = {};
  
          if (!_.isEmpty(memo)) {
              value.aggregations = memo;
          }
  
          obj[key] = value;
  
          return obj;
      }, subagg);
  }
docs: build a bucketed aggregation that groups by the given list of field names (and optionally includes given sub-aggregations). groupby is a list of field names