Buggy code:
static class SortedDocValuesCache extends FieldCacheImpl . Cache { SortedDocValuesCache ( FieldCacheImpl wrapper ) { } @ Override protected Object createValue ( AtomicReader reader , FieldCacheImpl . CacheKey key , boolean setDocsWithField ) throws IOException { final int maxDoc = reader . maxDoc ( ) ; Terms terms = reader . terms ( key . field ) ; final float acceptableOverheadRatio = ( ( Float ) ( key . custom ) ) . floatValue ( ) ; final PagedBytes bytes = new PagedBytes ( 15 ) ; int startBytesBPV ; int startTermsBPV ; int startNumUniqueTerms ; final int termCountHardLimit ; if ( maxDoc == ( Integer . MAX_VALUE ) ) { termCountHardLimit = Integer . MAX_VALUE ; } else { termCountHardLimit = maxDoc + 1 ; } if ( terms != null ) { long numUniqueTerms = terms . size ( ) ; if ( numUniqueTerms != ( - 1L ) ) { if ( numUniqueTerms > termCountHardLimit ) { numUniqueTerms = termCountHardLimit ; } startBytesBPV = PackedInts . bitsRequired ( ( numUniqueTerms * 4 ) ) ; startTermsBPV = PackedInts . bitsRequired ( numUniqueTerms ) ; startNumUniqueTerms = ( ( int ) ( numUniqueTerms ) ) ; } else { startBytesBPV = 1 ; startTermsBPV = 1 ; startNumUniqueTerms = 1 ; } } else { startBytesBPV = 1 ; startTermsBPV = 1 ; startNumUniqueTerms = 1 ; } GrowableWriter termOrdToBytesOffset = new GrowableWriter ( startBytesBPV , ( 1 + startNumUniqueTerms ) , acceptableOverheadRatio ) ; final GrowableWriter docToTermOrd = new GrowableWriter ( startTermsBPV , maxDoc , acceptableOverheadRatio ) ; int termOrd = 0 ; if ( terms != null ) { final TermsEnum termsEnum = terms . iterator ( null ) ; DocsEnum docs = null ; while ( true ) { final BytesRef term = termsEnum . next ( ) ; if ( term == null ) { break ; } if ( termOrd >= termCountHardLimit ) { break ; } if ( termOrd == ( termOrdToBytesOffset . size ( ) ) ) { termOrdToBytesOffset = termOrdToBytesOffset . resize ( ArrayUtil . oversize ( ( 1 + termOrd ) , 1 ) ) ; } termOrdToBytesOffset . set ( termOrd , bytes . copyUsingLengthPrefix ( term ) ) ; docs = termsEnum . docs ( null , docs , FLAG_NONE ) ; while ( true ) { final int docID = docs . nextDoc ( ) ; if ( docID == ( DocIdSetIterator . NO_MORE_DOCS ) ) { break ; } docToTermOrd . set ( docID , ( 1 + termOrd ) ) ; } termOrd ++ ; } if ( ( termOrdToBytesOffset . size ( ) ) > termOrd ) { termOrdToBytesOffset = termOrdToBytesOffset . resize ( termOrd ) ; } } return new FieldCacheImpl . SortedDocValuesImpl ( bytes . freeze ( true ) , termOrdToBytesOffset . getMutable ( ) , docToTermOrd . getMutable ( ) , termOrd ) ; } } private static class BinaryDocValuesImpl extends BinaryDocValues { private final Reader bytes ; private final PackedInts . Reader docToOffset ; public BinaryDocValuesImpl ( PagedBytes . Reader bytes , PackedInts . Reader docToOffset ) { } @ Override public void get ( int docID , BytesRef ret ) { } } public BinaryDocValues getTerms ( AtomicReader reader , String field ) throws IOException { } public BinaryDocValues getTerms ( AtomicReader reader , String field , float acceptableOverheadRatio ) throws IOException { } static final class BinaryDocValuesCache extends FieldCacheImpl . Cache { BinaryDocValuesCache ( FieldCacheImpl wrapper ) { } @ Override protected Object createValue ( AtomicReader reader , FieldCacheImpl . CacheKey key , boolean setDocsWithField ) throws IOException { final int maxDoc = reader . maxDoc ( ) ; Terms terms = reader . terms ( key . field ) ; final float acceptableOverheadRatio = ( ( Float ) ( key . custom ) ) . floatValue ( ) ; final int termCountHardLimit = maxDoc ; final PagedBytes bytes = new PagedBytes ( 15 ) ; int startBPV ; if ( terms != null ) { long numUniqueTerms = terms . size ( ) ; if ( numUniqueTerms != ( - 1L ) ) { if ( numUniqueTerms > termCountHardLimit ) { numUniqueTerms = termCountHardLimit ; } startBPV = PackedInts . bitsRequired ( ( numUniqueTerms * 4 ) ) ; } else { startBPV = 1 ; } } else { startBPV = 1 ; } final GrowableWriter docToOffset = new GrowableWriter ( startBPV , maxDoc , acceptableOverheadRatio ) ; bytes . copyUsingLengthPrefix ( new BytesRef ( ) ) ; if ( terms != null ) { int termCount = 0 ; final TermsEnum termsEnum = terms . iterator ( null ) ; DocsEnum docs = null ; while ( true ) { if ( ( termCount ++ ) == termCountHardLimit ) { break ; } final BytesRef term = termsEnum . next ( ) ; if ( term == null ) { break ; } final long pointer = bytes . copyUsingLengthPrefix ( term ) ; docs = termsEnum . docs ( null , docs , FLAG_NONE ) ; while ( true ) { final int docID = docs . nextDoc ( ) ; if ( docID == ( DocIdSetIterator . NO_MORE_DOCS ) ) { break ; } docToOffset . set ( docID , pointer ) ; } } } return new FieldCacheImpl . BinaryDocValuesImpl ( bytes . freeze ( true ) , docToOffset . getMutable ( ) ) ; } } public SortedSetDocValues getDocTermOrds ( AtomicReader reader , String field ) throws IOException { } static final class DocTermOrdsCache extends FieldCacheImpl . Cache { DocTermOrdsCache ( FieldCacheImpl wrapper ) { } @ Override protected Object createValue ( AtomicReader reader , FieldCacheImpl . CacheKey key , boolean setDocsWithField ) throws IOException { <START_BUG> return new org . apache . lucene . index . DocTermOrds ( reader , key . field ) ; <END_BUG> } } private volatile PrintStream infoStream ; public void setInfoStream ( PrintStream stream ) { } public PrintStream getInfoStream ( ) { } }

Fix:
return new org . apache . lucene . index . DocTermOrds ( reader , null , key . field ) ;