public FilterAggregationBuilder buildTermTopAggregation(
    String topAggregationName,
    TopAggregationDefinition<?> topAggregation,
    @Nullable Integer numberOfTerms,
    Consumer<BoolQueryBuilder> extraFilters,
    Consumer<FilterAggregationBuilder> otherSubAggregations
  ) {
    Consumer<FilterAggregationBuilder> subAggregations = t -> {
      t.subAggregation(subAggregationHelper.buildTermsAggregation(topAggregationName, topAggregation, numberOfTerms));
      otherSubAggregations.accept(t);
    };
    return buildTopAggregation(topAggregationName, topAggregation, extraFilters, subAggregations);
  }
@Test
  public void buildTermTopAggregation_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));
    String topAggregationName = randomAlphabetic(10);
    TermsAggregationBuilder termSubAgg = AggregationBuilders.terms("foo");
    when(subAggregationHelper.buildTermsAggregation(topAggregationName, topAggregation, null)).thenReturn(termSubAgg);

    FilterAggregationBuilder aggregationBuilder = underTest.buildTermTopAggregation(
      topAggregationName, topAggregation, null,
      NO_EXTRA_FILTER, NO_OTHER_SUBAGGREGATION);

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