title: ElasticSearch Multiple Sub Aggregation Queries Use 1.5G Memory With Only 25 MB Documents
body: <p>Can anyone help me take a look to this query builder and help me optimize it? Can someone also tell me why the collection model "breath_first" doesn't make difference to "depth_first"? This Query is keep hitting my circuit breaker request limit and cause OOM. The index metadata mapping fields are just flat listed, no nested fields. </p>
  
  <pre><code>    final FilterBuilder filterBuilder = termFilter("month", getMostRecentMonthToQuery(indexId));
  
      final String revenueScript = getRevenueScript(revenueModifier);
  
      final String sortString = convertTermSortToAggregationSort(sortColumn);
      final boolean isAscending = sortOrder.equalsIgnoreCase("asc");
      final SubAggCollectionMode aggCollectionMode = SubAggCollectionMode.parse("breadth_first");
  
      final SearchRequestBuilder builder = elasticSearchClient.prepareSearch(indexId)
              .setTypes(TYPE_NAME)
              .setQuery(QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(), filterBuilder))
              .addAggregation(AggregationBuilders.terms("carCount")
                  .field(CAR_RAW)
                  .size(pageSize)
                  .order(Terms.Order.aggregation(sortString, isAscending))
                  .collectMode(aggCollectionMode)
                  .subAggregation(AggregationBuilders.terms("partProviderCount")
                      .field(PART_PROVIDER_RAW)
                      .size(0)
                  )
                  .subAggregation(AggregationBuilders.cardinality("partCount")
                      .field(PART_NUMBER_RAW)
                      .precisionThreshold(40000)
                  )
                  .subAggregation(AggregationBuilders.avg(IGPART_AGG_NAME)
                      .field(IGPART_TERM_NAME)
                  )
                  .subAggregation(AggregationBuilders.avg(CGPART_AGG_NAME)
                      .field(CGPART_TERM_NAME)
                  )
                  .subAggregation(AggregationBuilders.avg(OGPART_AGG_NAME)
                      .field(OGPART_TERM_NAME)
                  )
                  .subAggregation(AggregationBuilders.avg(CPPART_AGG_NAME)
                      .field(CPPART_TERM_NAME)
                  )
                  .subAggregation(AggregationBuilders.sum(IGPART_PRICE_AGG_NAME)
                      .field(IGPART_TERM_NAME)
                      .script(revenueScript)
                  )
                  .subAggregation(AggregationBuilders.sum(CGPART_PRICE_AGG_NAME)
                      .field(CGPART_TERM_NAME)
                      .script(revenueScript)
                  )
                  .subAggregation(AggregationBuilders.sum(OGPART_PRICE_AGG_NAME)
                      .field(OGPART_TERM_NAME)
                      .script(revenueScript)
                  )
                  .subAggregation(AggregationBuilders.sum(CPPART_PRICE_AGG_NAME)
                      .field(CPPART_TERM_NAME)
                      .script(revenueScript)
                  )
              );
      return builder;
  </code></pre>