Buggy code:
@ BeforeClass public static void createPostings ( ) throws IOException { } private FieldInfos currentFieldInfos ; private FieldsProducer buildIndex ( Directory dir , IndexOptions maxAllowed , boolean allowPayloads ) throws IOException { SegmentInfo segmentInfo = new SegmentInfo ( dir , Constants . LUCENE_MAIN_VERSION , "_0" , ( 1 + ( TestPostingsFormat . maxDocID ) ) , false , Codec . getDefault ( ) , null , null ) ; int maxIndexOption = Arrays . asList ( IndexOptions . values ( ) ) . indexOf ( maxAllowed ) ; if ( VERBOSE ) { System . out . println ( "\nTEST:<seq2seq4repair_space>now<seq2seq4repair_space>build<seq2seq4repair_space>index" ) ; } int maxIndexOptionNoOffsets = Arrays . asList ( IndexOptions . values ( ) ) . indexOf ( DOCS_AND_FREQS_AND_POSITIONS ) ; FieldInfo [ ] newFieldInfoArray = new FieldInfo [ TestPostingsFormat . fields . size ( ) ] ; for ( int fieldUpto = 0 ; fieldUpto < ( TestPostingsFormat . fields . size ( ) ) ; fieldUpto ++ ) { FieldInfo oldFieldInfo = TestPostingsFormat . fieldInfos . fieldInfo ( fieldUpto ) ; String pf = _TestUtil . getPostingsFormat ( oldFieldInfo . name ) ; int fieldMaxIndexOption ; if ( doesntSupportOffsets . contains ( pf ) ) { fieldMaxIndexOption = Math . min ( maxIndexOptionNoOffsets , maxIndexOption ) ; } else { fieldMaxIndexOption = maxIndexOption ; } IndexOptions indexOptions = IndexOptions . values ( ) [ random ( ) . nextInt ( ( 1 + fieldMaxIndexOption ) ) ] ; boolean doPayloads = ( ( indexOptions . compareTo ( DOCS_AND_FREQS_AND_POSITIONS ) ) >= 0 ) && allowPayloads ; newFieldInfoArray [ fieldUpto ] = new FieldInfo ( oldFieldInfo . name , true , fieldUpto , false , false , doPayloads , indexOptions , null , Type . FIXED_INTS_8 , null ) ; } FieldInfos newFieldInfos = new FieldInfos ( newFieldInfoArray ) ; SegmentWriteState writeState = new SegmentWriteState ( null , dir , segmentInfo , newFieldInfos , 32 , null , IOContext . DEFAULT ) ; FieldsConsumer fieldsConsumer = Codec . getDefault ( ) . postingsFormat ( ) . fieldsConsumer ( writeState ) ; for ( Map . Entry < String , Map < BytesRef , List < TestPostingsFormat . Posting > > > fieldEnt : TestPostingsFormat . fields . entrySet ( ) ) { String field = fieldEnt . getKey ( ) ; Map < BytesRef , List < TestPostingsFormat . Posting > > terms = fieldEnt . getValue ( ) ; FieldInfo fieldInfo = newFieldInfos . fieldInfo ( field ) ; IndexOptions indexOptions = fieldInfo . getIndexOptions ( ) ; if ( VERBOSE ) { System . out . println ( ( ( ( "field=" + field ) + "<seq2seq4repair_space>indexOtions=" ) + indexOptions ) ) ; } boolean doFreq = ( indexOptions . compareTo ( DOCS_AND_FREQS ) ) >= 0 ; boolean doPos = ( indexOptions . compareTo ( DOCS_AND_FREQS_AND_POSITIONS ) ) >= 0 ; boolean doPayloads = ( ( indexOptions . compareTo ( DOCS_AND_FREQS_AND_POSITIONS ) ) >= 0 ) && allowPayloads ; boolean doOffsets = ( indexOptions . compareTo ( DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS ) ) >= 0 ; TermsConsumer termsConsumer = fieldsConsumer . addField ( fieldInfo ) ; long sumTotalTF = 0 ; long sumDF = 0 ; FixedBitSet seenDocs = new FixedBitSet ( ( ( TestPostingsFormat . maxDocID ) + 1 ) ) ; for ( Map . Entry < BytesRef , List < TestPostingsFormat . Posting > > termEnt : terms . entrySet ( ) ) { BytesRef term = termEnt . getKey ( ) ; List < TestPostingsFormat . Posting > postings = termEnt . getValue ( ) ; if ( VERBOSE ) { System . out . println ( ( ( ( ( ( "<seq2seq4repair_space>term=" + field ) + ":" ) + ( term . utf8ToString ( ) ) ) + "<seq2seq4repair_space>docFreq=" ) + ( postings . size ( ) ) ) ) ; } PostingsConsumer postingsConsumer = termsConsumer . startTerm ( term ) ; long totalTF = 0 ; int docCount = 0 ; for ( TestPostingsFormat . Posting posting : postings ) { if ( VERBOSE ) { System . out . println ( ( ( ( ( ( "<seq2seq4repair_space>" + docCount ) + ":<seq2seq4repair_space>docID=" ) + ( posting . docID ) ) + "<seq2seq4repair_space>freq=" ) + ( posting . positions . size ( ) ) ) ) ; } <START_BUG> postingsConsumer . startDoc ( posting . docID , posting . positions . size ( ) ) ; <END_BUG> seenDocs . set ( posting . docID ) ; if ( doPos ) { totalTF += posting . positions . size ( ) ; for ( TestPostingsFormat . Position pos : posting . positions ) { if ( VERBOSE ) { if ( doPayloads ) { System . out . println ( ( ( ( "<seq2seq4repair_space>pos=" + ( pos . position ) ) + "<seq2seq4repair_space>payload=" ) + ( ( pos . payload ) == null ? "null" : ( pos . payload . length ) + "<seq2seq4repair_space>bytes" ) ) ) ; } else { System . out . println ( ( "<seq2seq4repair_space>pos=" + ( pos . position ) ) ) ; } } postingsConsumer . addPosition ( pos . position , ( doPayloads && ( ( pos . payload ) != null ) ? new BytesRef ( pos . payload ) : null ) , ( doOffsets ? pos . startOffset : - 1 ) , ( doOffsets ? pos . endOffset : - 1 ) ) ; } } else if ( doFreq ) { totalTF += posting . positions . size ( ) ; } else { totalTF ++ ; } postingsConsumer . finishDoc ( ) ; docCount ++ ; } termsConsumer . finishTerm ( term , new TermStats ( postings . size ( ) , ( doFreq ? totalTF : - 1 ) ) ) ; sumTotalTF += totalTF ; sumDF += postings . size ( ) ; } termsConsumer . finish ( ( doFreq ? sumTotalTF : - 1 ) , sumDF , seenDocs . cardinality ( ) ) ; }

Fix:
postingsConsumer . startDoc ( posting . docID , ( doFreq ? posting . positions . size ( ) : - 1 ) ) ;