Can I improve this aggregation query?

I figured it out:

  1. I added an additional max_bucket aggregation (agg_property_capacity):
"aggs": {
  "agg_by_property": {
    "terms": {
      "field": "property_id"
    },
    "aggs": {
      "agg_by_room_type": {
        "terms": {
          "field": "is_private"
        },
        "aggs": {
          "agg_capacity_by_room_type": {
            "sum_bucket": {
              "buckets_path": "agg_by_room>agg_min_space_over_period"
            }
          },
          "agg_by_room": {
            "terms": {
              "field": "room_id"
            },
            "aggs": {
              "agg_min_space_over_period": {
                "min": {
                  "field": "capacity"
                }
              }
            }
          }
        }
      },
      "agg_property_capacity": {
        "max_bucket": {
          "buckets_path": "agg_by_room_type>agg_capacity_by_room_type"
        }
      }
    }
  }
}
  1. I reduced the amount of data returned by specifying a filter_path value:

filter_path=aggregations.agg_by_property.buckets.key,aggregations.agg_by_property.buckets.agg_property_capacity.value

this gave me the much smaller response of:

{
  "aggregations" : {
    "agg_by_property" : {
      "buckets" : [
        {
          "key" : 1,
          "agg_property_capacity" : {
            "value" : 15.0
          }
        },
        {
          "key" : 2,
          "agg_property_capacity" : {
            "value" : 7.0
          }
        }
      ]
    }
  }
}

1 Like