public TermsAggregationBuilder buildTermsAggregation(String name,
    TopAggregationDefinition<?> topAggregation, @Nullable Integer numberOfTerms) {
    TermsAggregationBuilder termsAggregation = AggregationBuilders.terms(name)
      .field(topAggregation.getFilterScope().getFieldName())
      .order(order)
      .minDocCount(TERM_AGGREGATION_MIN_DOC_COUNT);
    if (numberOfTerms != null) {
      termsAggregation.size(numberOfTerms);
    }
    if (subAggregation != null) {
      termsAggregation = termsAggregation.subAggregation(subAggregation);
    }
    return termsAggregation;
  }
@Test
  public void buildTermsAggregation_adds_term_subaggregation_with_minDoc_1_and_default_sort() {
    String aggName = randomAlphabetic(10);
    SimpleFieldTopAggregationDefinition topAggregation = new SimpleFieldTopAggregationDefinition("bar", false);

    Stream.of(
      underTest,
      underTestWithCustomSubAgg)
      .forEach(t -> {
        TermsAggregationBuilder agg = t.buildTermsAggregation(aggName, topAggregation, null);

        assertThat(agg.getName()).isEqualTo(aggName);
        assertThat(agg.field()).isEqualTo(topAggregation.getFilterScope().getFieldName());
        assertThat(agg.size()).isEqualTo(DEFAULT_BUCKET_SIZE);
        assertThat(agg.minDocCount()).isOne();
        assertThat(agg.order()).isEqualTo(BucketOrder.compound(SQ_DEFAULT_BUCKET_ORDER, ES_BUILTIN_TIE_BREAKER));
      });
  }