Buggy code:
public class TestGrouping extends LuceneTestCase { public void testBasic ( ) throws Exception { } private static class GroupDoc { final int id ; final String group ; final String sort1 ; final String sort2 ; final String content ; float score ; float score2 ; public GroupDoc ( int id , String group , String sort1 , String sort2 , String content ) { } } private Sort getRandomSort ( ) { } private Comparator < TestGrouping . GroupDoc > getComparator ( Sort sort ) { } private Comparable < ? > [ ] fillFields ( TestGrouping . GroupDoc d , Sort sort ) { } private String groupToString ( String b ) { } private TopGroups < String > slowGrouping ( TestGrouping . GroupDoc [ ] groupDocs , String searchTerm , boolean fillFields , boolean getScores , boolean getMaxScores , boolean doAllGroups , Sort groupSort , Sort docSort , int topNGroups , int docsPerGroup , int groupOffset , int docOffset ) { } private IndexReader getDocBlockReader ( Directory dir , TestGrouping . GroupDoc [ ] groupDocs ) throws IOException { } private static class ShardState { public final TestGrouping . ShardSearcher [ ] subSearchers ; public final int [ ] docStarts ; public ShardState ( IndexSearcher s ) { } } public void testRandom ( ) throws Exception { } private void verifyShards ( int [ ] docStarts , TopGroups < String > topGroups ) { } private void assertEquals ( Collection < SearchGroup < String > > groups1 , Collection < SearchGroup < String > > groups2 , boolean doSortValues ) { } private TopGroups < String > searchShards ( IndexSearcher topSearcher , TestGrouping . ShardState shardState , Query query , Sort groupSort , Sort docSort , int groupOffset , int topNGroups , int docOffset , int topNDocs , boolean getScores , boolean getMaxScores ) throws Exception { if ( VERBOSE ) { System . out . println ( ( ( ( "TEST:<seq2seq4repair_space>" + ( shardState . subSearchers . length ) ) + "<seq2seq4repair_space>shards:<seq2seq4repair_space>" ) + ( Arrays . toString ( shardState . subSearchers ) ) ) ) ; } <START_BUG> final Weight w = query . weight ( topSearcher ) ; <END_BUG> final List < Collection < SearchGroup < String > > > shardGroups = new ArrayList < Collection < SearchGroup < String > > > ( ) ; for ( int shardIDX = 0 ; shardIDX < ( shardState . subSearchers . length ) ; shardIDX ++ ) { final TermFirstPassGroupingCollector c = new TermFirstPassGroupingCollector ( "group" , groupSort , ( groupOffset + topNGroups ) ) ; shardState . subSearchers [ shardIDX ] . search ( w , c ) ; final Collection < SearchGroup < String > > topGroups = c . getTopGroups ( 0 , true ) ; if ( topGroups != null ) { if ( VERBOSE ) { System . out . println ( ( ( ( ( ( ( "<seq2seq4repair_space>shard<seq2seq4repair_space>" + shardIDX ) + "<seq2seq4repair_space>s=" ) + ( shardState . subSearchers [ shardIDX ] ) ) + "<seq2seq4repair_space>" ) + ( topGroups . size ( ) ) ) + "<seq2seq4repair_space>groups:" ) ) ; for ( SearchGroup < String > group : topGroups ) { System . out . println ( ( ( ( "<seq2seq4repair_space>" + ( groupToString ( group . groupValue ) ) ) + "<seq2seq4repair_space>sort=" ) + ( Arrays . toString ( group . sortValues ) ) ) ) ; } } shardGroups . add ( topGroups ) ; } } final Collection < SearchGroup < String > > mergedTopGroups = SearchGroup . merge ( shardGroups , groupOffset , topNGroups , groupSort ) ; if ( VERBOSE ) { System . out . println ( "<seq2seq4repair_space>merged:" ) ; if ( mergedTopGroups == null ) { System . out . println ( "<seq2seq4repair_space>null" ) ; } else { for ( SearchGroup < String > group : mergedTopGroups ) { System . out . println ( ( ( ( "<seq2seq4repair_space>" + ( groupToString ( group . groupValue ) ) ) + "<seq2seq4repair_space>sort=" ) + ( Arrays . toString ( group . sortValues ) ) ) ) ; } } } if ( mergedTopGroups != null ) { @ SuppressWarnings ( "unchecked" ) final TopGroups < String > [ ] shardTopGroups = new TopGroups [ shardState . subSearchers . length ] ; for ( int shardIDX = 0 ; shardIDX < ( shardState . subSearchers . length ) ; shardIDX ++ ) { final TermSecondPassGroupingCollector c = new TermSecondPassGroupingCollector ( "group" , mergedTopGroups , groupSort , docSort , ( docOffset + topNDocs ) , getScores , getMaxScores , true ) ; shardState . subSearchers [ shardIDX ] . search ( w , c ) ; shardTopGroups [ shardIDX ] = c . getTopGroups ( 0 ) ; rebaseDocIDs ( groupSort , docSort , shardState . docStarts [ shardIDX ] , shardTopGroups [ shardIDX ] ) ; } return TopGroups . merge ( shardTopGroups , groupSort , docSort , docOffset , topNDocs ) ; } else { return null ; } } private List < Integer > getDocIDSortLocs ( Sort sort ) { } private void rebaseDocIDs ( Sort groupSort , Sort docSort , int docBase , TopGroups < String > groups ) { } private void assertEquals ( int [ ] docIDtoID , TopGroups expected , TopGroups actual , boolean verifyGroupValues , boolean verifyTotalGroupCount , boolean verifySortValues , boolean testScores ) { } private static class ShardSearcher { private final IndexSearcher subSearcher ; public ShardSearcher ( IndexReader subReader ) { } public void search ( Weight weight , Collector collector ) throws IOException { } public TopDocs search ( Weight weight , int topN ) throws IOException { } @ Override public String toString ( ) { } } }

Fix:
final Weight w = topSearcher . createNormalizedWeight ( query ) ;