Buggy code:
public class TopGroupsShardResponseProcessor implements ShardResponseProcessor { @ SuppressWarnings ( "unchecked" ) public void process ( ResponseBuilder rb , ShardRequest shardRequest ) { Sort groupSort = rb . getGroupingSpec ( ) . getGroupSort ( ) ; String [ ] fields = rb . getGroupingSpec ( ) . getFields ( ) ; String [ ] queries = rb . getGroupingSpec ( ) . getQueries ( ) ; Sort sortWithinGroup = rb . getGroupingSpec ( ) . getSortWithinGroup ( ) ; int groupOffsetDefault ; if ( ( ( rb . getGroupingSpec ( ) . getResponseFormat ( ) ) == ( Format . simple ) ) || ( rb . getGroupingSpec ( ) . isMain ( ) ) ) { groupOffsetDefault = 0 ; } else { groupOffsetDefault = rb . getGroupingSpec ( ) . getGroupOffset ( ) ; } int docsPerGroupDefault = rb . getGroupingSpec ( ) . getGroupLimit ( ) ; Map < String , List < TopGroups < BytesRef > > > commandTopGroups = new HashMap < String , List < TopGroups < BytesRef > > > ( ) ; for ( String field : fields ) { commandTopGroups . put ( field , new ArrayList < TopGroups < BytesRef > > ( ) ) ; } Map < String , List < QueryCommandResult > > commandTopDocs = new HashMap < String , List < QueryCommandResult > > ( ) ; for ( String query : queries ) { commandTopDocs . put ( query , new ArrayList < QueryCommandResult > ( ) ) ; } TopGroupsResultTransformer serializer = new TopGroupsResultTransformer ( rb ) ; for ( ShardResponse srsp : shardRequest . responses ) { NamedList < NamedList > secondPhaseResult = ( ( NamedList < NamedList > ) ( srsp . getSolrResponse ( ) . getResponse ( ) . get ( "secondPhase" ) ) ) ; Map < String , ? > result = serializer . transformToNative ( secondPhaseResult , groupSort , sortWithinGroup , srsp . getShard ( ) ) ; for ( String field : commandTopGroups . keySet ( ) ) { TopGroups < BytesRef > topGroups = ( ( TopGroups < BytesRef > ) ( result . get ( field ) ) ) ; if ( topGroups == null ) { continue ; } commandTopGroups . get ( field ) . add ( topGroups ) ; } for ( String query : queries ) { commandTopDocs . get ( query ) . add ( ( ( QueryCommandResult ) ( result . get ( query ) ) ) ) ; } } try { for ( String groupField : commandTopGroups . keySet ( ) ) { List < TopGroups < BytesRef > > topGroups = commandTopGroups . get ( groupField ) ; if ( topGroups . isEmpty ( ) ) { continue ; } TopGroups < BytesRef > [ ] topGroupsArr = new TopGroups [ topGroups . size ( ) ] ; <START_BUG> rb . mergedTopGroups . put ( groupField , TopGroups . merge ( topGroups . toArray ( topGroupsArr ) , groupSort , sortWithinGroup , groupOffsetDefault , docsPerGroupDefault ) ) ; <END_BUG> } for ( String query : commandTopDocs . keySet ( ) ) { List < QueryCommandResult > queryCommandResults = commandTopDocs . get ( query ) ; List < TopDocs > topDocs = new ArrayList < TopDocs > ( queryCommandResults . size ( ) ) ; int mergedMatches = 0 ; for ( QueryCommandResult queryCommandResult : queryCommandResults ) { topDocs . add ( queryCommandResult . getTopDocs ( ) ) ; mergedMatches += queryCommandResult . getMatches ( ) ; } int topN = ( rb . getGroupingSpec ( ) . getOffset ( ) ) + ( rb . getGroupingSpec ( ) . getLimit ( ) ) ; TopDocs mergedTopDocs = TopDocs . merge ( sortWithinGroup , topN , topDocs . toArray ( new TopDocs [ topDocs . size ( ) ] ) ) ; rb . mergedQueryCommandResults . put ( query , new QueryCommandResult ( mergedTopDocs , mergedMatches ) ) ; } Map < Object , ShardDoc > resultIds = new HashMap < Object , ShardDoc > ( ) ; int i = 0 ; for ( TopGroups < BytesRef > topGroups : rb . mergedTopGroups . values ( ) ) { for ( GroupDocs < BytesRef > group : topGroups . groups ) { for ( ScoreDoc scoreDoc : group . scoreDocs ) { ShardDoc solrDoc = ( ( ShardDoc ) ( scoreDoc ) ) ; solrDoc . positionInResponse = i ++ ; resultIds . put ( solrDoc . id , solrDoc ) ; } } } for ( QueryCommandResult queryCommandResult : rb . mergedQueryCommandResults . values ( ) ) { for ( ScoreDoc scoreDoc : queryCommandResult . getTopDocs ( ) . scoreDocs ) { ShardDoc solrDoc = ( ( ShardDoc ) ( scoreDoc ) ) ; solrDoc . positionInResponse = i ++ ; resultIds . put ( solrDoc . id , solrDoc ) ; } } rb . resultIds = resultIds ; } catch ( IOException e ) { throw new org . apache . solr . common . SolrException ( ErrorCode . SERVER_ERROR , e ) ; } } }

Fix:
rb . mergedTopGroups . put ( groupField , TopGroups . merge ( topGroups . toArray ( topGroupsArr ) , groupSort , sortWithinGroup , groupOffsetDefault , docsPerGroupDefault , None ) ) ;