Optional<GroupingListHit> toAggregatedHit() {
        if (groupingHitsMerged == 0) return Optional.empty();
        log.log(Level.FINE, () ->
                String.format("Creating aggregated hit containing %d groupings from %d hits with docsums '%s' and %s",
                        groupings.size(), groupingHitsMerged, documentDatabase.getDocsumDefinitionSet(), query));
        GroupingListHit groupingHit = new GroupingListHit(List.copyOf(groupings.values()), documentDatabase, query);
        groupingHit.setQuery(query);
        groupingHit.getGroupingList().forEach(g -> {
            g.select(o -> o instanceof Hit, o -> ((Hit)o).setContext(groupingHit));
            g.postMerge();
        });
        return Optional.of(groupingHit);
    }
Incrementally merges underlying {@link Grouping} instances from {@link GroupingListHit} hits.

@author bjorncs
container-search/src/main/java/com/yahoo/search/dispatch/GroupingResultAggregator.java