Source:
private org.elasticsearch.search.aggregations.AggregationBuilder toEsAggregation(DocumentMapping mapping, AggregationBuilder<?> aggregation, boolean fetchSource) { final TermsAggregationBuilder termsAgg = AggregationBuilders .terms(aggregation.getName()) .minDocCount(aggregation.getMinBucketSize()) .size(Integer.MAX_VALUE); boolean isFieldAgg = !Strings.isNullOrEmpty(aggregation.getGroupByField()); boolean isScriptAgg = !Strings.isNullOrEmpty(aggregation.getGroupByScript()); if (isFieldAgg) { checkArgument(!isScriptAgg, "Specify either field or script parameter, not both"); termsAgg.field(aggregation.getGroupByField()); } else if (isScriptAgg) { termsAgg.script(aggregation.toEsScript(mapping)); } else { throw new IllegalArgumentException("Specify either field or script parameter"); } boolean isNested = !Strings.isNullOrEmpty(aggregation.getPath()); if (aggregation.getBucketHitsLimit() > 0) { TopHitsAggregationBuilder topHitsAgg = AggregationBuilders.topHits(topHitsAggName(aggregation)) .size(aggregation.getBucketHitsLimit()); if (fetchSource) { topHitsAgg .storedFields(STORED_FIELDS_ID_ONLY) .fetchSource(); } else { topHitsAgg .storedFields(STORED_FIELDS_NONE) .fetchSource(false); aggregation.getFields().forEach(field -> topHitsAgg.docValueField(field)); } if (isNested) { termsAgg.subAggregation(AggregationBuilders.reverseNested(reverseNestedAggName(aggregation)).subAggregation(topHitsAgg)); } else { termsAgg.subAggregation(topHitsAgg); } } if (isNested) { return AggregationBuilders .nested(nestedAggName(aggregation), aggregation.getPath()) .subAggregation(termsAgg); } return termsAgg; }

Target:
private org.elasticsearch.search.aggregations.AggregationBuilder toEsAggregation(DocumentMapping mapping, AggregationBuilder<?> aggregation, boolean fetchSource) { final TermsAggregationBuilder termsAgg = AggregationBuilders .terms(aggregation.getName()) .minDocCount(aggregation.getMinBucketSize()) .size(Integer.MAX_VALUE); boolean isFieldAgg = !Strings.isNullOrEmpty(aggregation.getGroupByField()); boolean isScriptAgg = !Strings.isNullOrEmpty(aggregation.getGroupByScript()); if (isFieldAgg) { checkArgument(!isScriptAgg, "Specify either field or script parameter, not both"); termsAgg.field(aggregation.getGroupByField()); } else if (isScriptAgg) { termsAgg.script(aggregation.toEsScript(mapping)); } else { throw new IllegalArgumentException("Specify either field or script parameter"); } boolean isNested = !Strings.isNullOrEmpty(aggregation.getPath()); if (aggregation.getBucketHitsLimit() > 0) { TopHitsAggregationBuilder topHitsAgg = AggregationBuilders.topHits(topHitsAggName(aggregation)) .size(aggregation.getBucketHitsLimit()); if (fetchSource) { topHitsAgg .storedFields(STORED_FIELDS_ID_ONLY) .fetchSource(true); } else { topHitsAgg .storedFields(STORED_FIELDS_NONE) .fetchSource(false); aggregation.getFields().forEach(field -> topHitsAgg.docValueField(field)); } if (isNested) { termsAgg.subAggregation(AggregationBuilders.reverseNested(reverseNestedAggName(aggregation)).subAggregation(topHitsAgg)); } else { termsAgg.subAggregation(topHitsAgg); } } if (isNested) { return AggregationBuilders .nested(nestedAggName(aggregation), aggregation.getPath()) .subAggregation(termsAgg); } return termsAgg; }