Buggy code:
docs . add ( document ) ; w . addDocuments ( docs ) ; document = new Document ( ) ; document . add ( new org . apache . lucene . document . StringField ( "__type" , "parent" , Store . NO ) ) ; document . add ( new org . apache . lucene . document . StringField ( "field1" , "h" , Store . NO ) ) ; w . addDocument ( document ) ; docs . clear ( ) ; document = new Document ( ) ; document . add ( new org . apache . lucene . document . StringField ( "field2" , "m" , Store . NO ) ) ; document . add ( new org . apache . lucene . document . StringField ( "filter_1" , "T" , Store . NO ) ) ; docs . add ( document ) ; document = new Document ( ) ; document . add ( new org . apache . lucene . document . StringField ( "field2" , "n" , Store . NO ) ) ; document . add ( new org . apache . lucene . document . StringField ( "filter_1" , "F" , Store . NO ) ) ; docs . add ( document ) ; document = new Document ( ) ; document . add ( new org . apache . lucene . document . StringField ( "field2" , "o" , Store . NO ) ) ; document . add ( new org . apache . lucene . document . StringField ( "filter_1" , "F" , Store . NO ) ) ; docs . add ( document ) ; document = new Document ( ) ; document . add ( new org . apache . lucene . document . StringField ( "__type" , "parent" , Store . NO ) ) ; document . add ( new org . apache . lucene . document . StringField ( "field1" , "i" , Store . NO ) ) ; docs . add ( document ) ; w . addDocuments ( docs ) ; w . commit ( ) ; document = new Document ( ) ; document . add ( new org . apache . lucene . document . StringField ( "fieldXXX" , "x" , Store . NO ) ) ; w . addDocument ( document ) ; document = new Document ( ) ; document . add ( new org . apache . lucene . document . StringField ( "fieldXXX" , "x" , Store . NO ) ) ; w . addDocument ( document ) ; document = new Document ( ) ; document . add ( new org . apache . lucene . document . StringField ( "fieldXXX" , "x" , Store . NO ) ) ; w . addDocument ( document ) ; IndexSearcher searcher = new IndexSearcher ( DirectoryReader . open ( w . w , false ) ) ; <START_BUG> w . close ( ) ; <END_BUG> Filter parentFilter = new QueryWrapperFilter ( new org . apache . lucene . search . TermQuery ( new Term ( "__type" , "parent" ) ) ) ; Filter childFilter = new QueryWrapperFilter ( new org . apache . lucene . search . PrefixQuery ( new Term ( "field2" ) ) ) ; ToParentBlockJoinQuery query = new ToParentBlockJoinQuery ( new org . apache . lucene . search . FilteredQuery ( new MatchAllDocsQuery ( ) , childFilter ) , new FixedBitSetCachingWrapperFilter ( parentFilter ) , ScoreMode . None ) ; ToParentBlockJoinSortField sortField = new ToParentBlockJoinSortField ( "field2" , Type . STRING , false , wrap ( parentFilter ) , wrap ( childFilter ) ) ; Sort sort = new Sort ( sortField ) ; TopFieldDocs topDocs = searcher . search ( query , 5 , sort ) ; assertEquals ( 7 , topDocs . totalHits ) ; assertEquals ( 5 , topDocs . scoreDocs . length ) ; assertEquals ( 3 , topDocs . scoreDocs [ 0 ] . doc ) ; assertEquals ( "a" , ( ( org . apache . lucene . util . BytesRef ) ( ( ( org . apache . lucene . search . FieldDoc ) ( topDocs . scoreDocs [ 0 ] ) ) . fields [ 0 ] ) ) . utf8ToString ( ) ) ; assertEquals ( 7 , topDocs . scoreDocs [ 1 ] . doc ) ; assertEquals ( "c" , ( ( org . apache . lucene . util . BytesRef ) ( ( ( org . apache . lucene . search . FieldDoc ) ( topDocs . scoreDocs [ 1 ] ) ) . fields [ 0 ] ) ) . utf8ToString ( ) ) ; assertEquals ( 11 , topDocs . scoreDocs [ 2 ] . doc ) ; assertEquals ( "e" , ( ( org . apache . lucene . util . BytesRef ) ( ( ( org . apache . lucene . search . FieldDoc ) ( topDocs . scoreDocs [ 2 ] ) ) . fields [ 0 ] ) ) . utf8ToString ( ) ) ; assertEquals ( 15 , topDocs . scoreDocs [ 3 ] . doc ) ; assertEquals ( "g" , ( ( org . apache . lucene . util . BytesRef ) ( ( ( org . apache . lucene . search . FieldDoc ) ( topDocs . scoreDocs [ 3 ] ) ) . fields [ 0 ] ) ) . utf8ToString ( ) ) ; assertEquals ( 19 , topDocs . scoreDocs [ 4 ] . doc ) ; assertEquals ( "i" , ( ( org . apache . lucene . util . BytesRef ) ( ( ( org . apache . lucene . search . FieldDoc ) ( topDocs . scoreDocs [ 4 ] ) ) . fields [ 0 ] ) ) . utf8ToString ( ) ) ;

Fix:
w . shutdown ( ) ;