Hopefully I've overlooking something easy. I recently replaced the TermsAggregation with a Composite agg and I can't get the bucket sorting to work.
With the terms agg there was an order directly in the agg builder that let me order via a max score script:
TermsAggregationBuilder groupByAggBuilder = AggregationBuilders
.terms(agg.getName())
.field(agg.getFieldName() )
.size( aggSize )
.order( BucketOrder.aggregation(SORT_TOP_HIT, false) ) //SORT by calculated score below
.shardSize(SHARD_SIZE);
...
aggregationBuilder.subAggregation(AggregationBuilders.max(SORT_TOP_HIT).script(new Script(SCORE))); ////Sorting buckets by their max score
However since Composite aggregations are built differently I can't get it to sort the buckets by the max score. I want to sort the buckets by the max score of the results inside each bucket. Below will return the buckets and their max score unsorted. What am I missing?
List<CompositeValuesSourceBuilder<?>> sources = new ArrayList<>();
TermsValuesSourceBuilder groupByTerm = new TermsValuesSourceBuilder(BucketName)
.field(groupByFieldName);
sources.add(groupByTerm);
CompositeAggregationBuilder compositeAggregationBuilder =
new CompositeAggregationBuilder(agg.getName(), sources); //No sort order here
...
aggregationBuilder.subAggregation( AggregationBuilders.max(SORT_TOP_HIT).script(new Script(SCORE)) ); //max Score per bucket I want to sort buckets by