public FilterAggregationBuilder buildTopAggregation(String topAggregationName, TopAggregationDefinition<?> topAggregation,
    Consumer<BoolQueryBuilder> extraFilters, Consumer<FilterAggregationBuilder> subAggregations) {
    BoolQueryBuilder filter = filterComputer.getTopAggregationFilter(topAggregation)
      .orElseGet(QueryBuilders::boolQuery);
    // optionally add extra filter(s)
    extraFilters.accept(filter);

    FilterAggregationBuilder res = AggregationBuilders.filter(topAggregationName, filter);
    subAggregations.accept(res);
    checkState(
      !res.getSubAggregations().isEmpty(),
      "no sub-aggregation has been added to top-aggregation %s", topAggregationName);
    return res;
  }
@Test
  public void buildTopAggregation_adds_filter_from_FiltersComputer_for_TopAggregation() {
    SimpleFieldTopAggregationDefinition topAggregation = new SimpleFieldTopAggregationDefinition("bar", false);
    SimpleFieldTopAggregationDefinition otherTopAggregation = new SimpleFieldTopAggregationDefinition("acme", false);
    BoolQueryBuilder computerFilter = boolQuery();
    BoolQueryBuilder otherFilter = boolQuery();
    when(filtersComputer.getTopAggregationFilter(topAggregation)).thenReturn(Optional.of(computerFilter));
    when(filtersComputer.getTopAggregationFilter(otherTopAggregation)).thenReturn(Optional.of(otherFilter));
    MinAggregationBuilder subAggregation = AggregationBuilders.min("donut");
    String topAggregationName = randomAlphabetic(10);

    FilterAggregationBuilder aggregationBuilder = underTest.buildTopAggregation(topAggregationName, topAggregation,
      NO_EXTRA_FILTER, t -> t.subAggregation(subAggregation));

    assertThat(aggregationBuilder.getName()).isEqualTo(topAggregationName);
    assertThat(aggregationBuilder.getFilter()).isSameAs(computerFilter);
  }