private Map<Integer, Grouping> mergeGroupingResults(Result result) {
        Map<Integer, Grouping> ret = new HashMap<>();
        for (Iterator<Hit> i = result.hits().unorderedIterator(); i.hasNext(); ) {
            Hit hit = i.next();
            if (hit instanceof GroupingListHit) {
                for (Grouping grp : ((GroupingListHit)hit).getGroupingList()) {
                    grp.select(
                            o -> o instanceof com.yahoo.searchlib.aggregation.Hit
                                    && ((com.yahoo.searchlib.aggregation.Hit)o).getContext() == null,
                            o -> ((com.yahoo.searchlib.aggregation.Hit)o).setContext(hit));
                    Grouping old = ret.get(grp.getId());
                    if (old != null) {
                        old.merge(grp);
                    } else {
                        ret.put(grp.getId(), grp);
                    }
                }
                i.remove();
            }
        }
        for (Grouping grouping : ret.values()) {
            grouping.postMerge();
        }
        return ret;
    }
Merges the grouping content of the given result object. The first grouping hit found by iterating over the result
content is kept, and all consecutive matching hits are merged into this.

@param result the result to traverse
@return a map of merged grouping objects
container-search/src/main/java/com/yahoo/search/grouping/vespa/GroupingExecutor.java