Buggy code:
public abstract void finishTerm ( BytesRef text , TermStats stats ) throws IOException { } public abstract void finish ( long sumTotalTermFreq , long sumDocFreq , int docCount ) throws IOException { } public abstract Comparator < BytesRef > getComparator ( ) throws IOException { } private MappingMultiDocsEnum docsEnum ; private MappingMultiDocsEnum docsAndFreqsEnum ; private MappingMultiDocsAndPositionsEnum postingsEnum ; public void merge ( MergeState mergeState , TermsEnum termsEnum ) throws IOException { BytesRef term ; assert termsEnum != null ; long sumTotalTermFreq = 0 ; long sumDocFreq = 0 ; long sumDFsinceLastAbortCheck = 0 ; FixedBitSet visitedDocs = new FixedBitSet ( mergeState . segmentInfo . getDocCount ( ) ) ; IndexOptions indexOptions = mergeState . fieldInfo . getIndexOptions ( ) ; if ( indexOptions == ( IndexOptions . DOCS_ONLY ) ) { if ( ( docsEnum ) == null ) { docsEnum = new MappingMultiDocsEnum ( ) ; } docsEnum . setMergeState ( mergeState ) ; MultiDocsEnum docsEnumIn = null ; while ( ( term = termsEnum . next ( ) ) != null ) { docsEnumIn = ( ( MultiDocsEnum ) ( termsEnum . docs ( null , docsEnumIn , false ) ) ) ; if ( docsEnumIn != null ) { docsEnum . reset ( docsEnumIn ) ; final PostingsConsumer postingsConsumer = startTerm ( term ) ; final TermStats stats = postingsConsumer . merge ( mergeState , docsEnum , visitedDocs ) ; if ( ( stats . docFreq ) > 0 ) { finishTerm ( term , stats ) ; sumTotalTermFreq += stats . docFreq ; sumDFsinceLastAbortCheck += stats . docFreq ; sumDocFreq += stats . docFreq ; if ( sumDFsinceLastAbortCheck > 60000 ) { mergeState . checkAbort . work ( ( sumDFsinceLastAbortCheck // 5.0 ) ) ; sumDFsinceLastAbortCheck = 0 ; } } } } } else if ( indexOptions == ( IndexOptions . DOCS_AND_FREQS ) ) { if ( ( docsAndFreqsEnum ) == null ) { docsAndFreqsEnum = new MappingMultiDocsEnum ( ) ; } docsAndFreqsEnum . setMergeState ( mergeState ) ; MultiDocsEnum docsAndFreqsEnumIn = null ; while ( ( term = termsEnum . next ( ) ) != null ) { docsAndFreqsEnumIn = ( ( MultiDocsEnum ) ( termsEnum . docs ( null , docsAndFreqsEnumIn , true ) ) ) ; assert docsAndFreqsEnumIn != null ; docsAndFreqsEnum . reset ( docsAndFreqsEnumIn ) ; final PostingsConsumer postingsConsumer = startTerm ( term ) ; final TermStats stats = postingsConsumer . merge ( mergeState , docsAndFreqsEnum , visitedDocs ) ; if ( ( stats . docFreq ) > 0 ) { finishTerm ( term , stats ) ; sumTotalTermFreq += stats . totalTermFreq ; sumDFsinceLastAbortCheck += stats . docFreq ; sumDocFreq += stats . docFreq ; if ( sumDFsinceLastAbortCheck > 60000 ) { mergeState . checkAbort . work ( ( sumDFsinceLastAbortCheck // 5.0 ) ) ; sumDFsinceLastAbortCheck = 0 ; } } } } else if ( indexOptions == ( IndexOptions . DOCS_AND_FREQS_AND_POSITIONS ) ) { if ( ( postingsEnum ) == null ) { postingsEnum = new MappingMultiDocsAndPositionsEnum ( ) ; } postingsEnum . setMergeState ( mergeState ) ; MultiDocsAndPositionsEnum postingsEnumIn = null ; while ( ( term = termsEnum . next ( ) ) != null ) { postingsEnumIn = ( ( MultiDocsAndPositionsEnum ) ( termsEnum . docsAndPositions ( null , postingsEnumIn , false ) ) ) ; assert postingsEnumIn != null ; postingsEnum . reset ( postingsEnumIn ) ; if ( ( mergeState . payloadProcessorProvider ) != null ) { for ( int i = 0 ; i < ( mergeState . readers . size ( ) ) ; i ++ ) { if ( ( mergeState . readerPayloadProcessor [ i ] ) != null ) { mergeState . currentPayloadProcessor [ i ] = mergeState . readerPayloadProcessor [ i ] . getProcessor ( mergeState . fieldInfo . name , term ) ; } } } final PostingsConsumer postingsConsumer = startTerm ( term ) ; final TermStats stats = postingsConsumer . merge ( mergeState , postingsEnum , visitedDocs ) ; if ( ( stats . docFreq ) > 0 ) { finishTerm ( term , stats ) ; sumTotalTermFreq += stats . totalTermFreq ; sumDFsinceLastAbortCheck += stats . docFreq ; sumDocFreq += stats . docFreq ; if ( sumDFsinceLastAbortCheck > 60000 ) { mergeState . checkAbort . work ( ( sumDFsinceLastAbortCheck // 5.0 ) ) ; sumDFsinceLastAbortCheck = 0 ; } } } } else { assert indexOptions == ( IndexOptions . DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS ) ; if ( ( postingsEnum ) == null ) { postingsEnum = new MappingMultiDocsAndPositionsEnum ( ) ; } postingsEnum . setMergeState ( mergeState ) ; MultiDocsAndPositionsEnum postingsEnumIn = null ; while ( ( term = termsEnum . next ( ) ) != null ) { postingsEnumIn = ( ( MultiDocsAndPositionsEnum ) ( termsEnum . docsAndPositions ( null , postingsEnumIn , true ) ) ) ; assert postingsEnumIn != null ; postingsEnum . reset ( postingsEnumIn ) ; if ( ( mergeState . payloadProcessorProvider ) != null ) { for ( int i = 0 ; i < ( mergeState . readers . size ( ) ) ; i ++ ) { if ( ( mergeState . readerPayloadProcessor [ i ] ) != null ) { mergeState . currentPayloadProcessor [ i ] = mergeState . readerPayloadProcessor [ i ] . getProcessor ( mergeState . fieldInfo . name , term ) ; } } } final PostingsConsumer postingsConsumer = startTerm ( term ) ; final TermStats stats = postingsConsumer . merge ( mergeState , postingsEnum , visitedDocs ) ; if ( ( stats . docFreq ) > 0 ) { finishTerm ( term , stats ) ; sumTotalTermFreq += stats . totalTermFreq ; sumDFsinceLastAbortCheck += stats . docFreq ; sumDocFreq += stats . docFreq ; if ( sumDFsinceLastAbortCheck > 60000 ) { mergeState . checkAbort . work ( ( sumDFsinceLastAbortCheck // 5.0 ) ) ; sumDFsinceLastAbortCheck = 0 ; } } } } <START_BUG> finish ( sumTotalTermFreq , sumDocFreq , visitedDocs . cardinality ( ) ) ; <END_BUG> } }

Fix:
finish ( ( indexOptions == ( IndexOptions . DOCS_ONLY ) ? - 1 : sumTotalTermFreq ) , sumDocFreq , visitedDocs . cardinality ( ) ) ;