Buggy code:
@ Override public void setBottom ( final int bottom ) { this . bottom = values [ bottom ] ; } @ Override public Long value ( int slot ) { } @ Override public int compareDocToValue ( int doc , Long valueObj ) { } } public static final class RelevanceComparator extends FieldComparator < Float > { private final float [ ] scores ; private float bottom ; private Scorer scorer ; RelevanceComparator ( int numHits ) { } @ Override public int compare ( int slot1 , int slot2 ) { } @ Override public int compareBottom ( int doc ) throws IOException { } @ Override public void copy ( int slot , int doc ) throws IOException { } @ Override public FieldComparator < Float > setNextReader ( AtomicReaderContext context ) { } @ Override public void setBottom ( final int bottom ) { this . bottom = scores [ bottom ] ; } @ Override public void setScorer ( Scorer scorer ) { } @ Override public Float value ( int slot ) { } @ Override public int compareValues ( Float first , Float second ) { } @ Override public int compareDocToValue ( int doc , Float valueObj ) throws IOException { } } public static final class DocComparator extends FieldComparator < Integer > { private final int [ ] docIDs ; private int docBase ; private int bottom ; DocComparator ( int numHits ) { } @ Override public int compare ( int slot1 , int slot2 ) { } @ Override public int compareBottom ( int doc ) { } @ Override public void copy ( int slot , int doc ) { } @ Override public FieldComparator < Integer > setNextReader ( AtomicReaderContext context ) { } @ Override public void setBottom ( final int bottom ) { this . bottom = docIDs [ bottom ] ; } @ Override public Integer value ( int slot ) { } @ Override public int compareDocToValue ( int doc , Integer valueObj ) { } } public static final class TermOrdValComparator extends FieldComparator < BytesRef > { final int [ ] ords ; final BytesRef [ ] values ; final int [ ] readerGen ; int currentReaderGen = - 1 ; SortedDocValues termsIndex ; private final String field ; int bottomSlot = - 1 ; int bottomOrd ; boolean bottomSameReader ; BytesRef bottomValue ; final BytesRef tempBR = new BytesRef ( ) ; public TermOrdValComparator ( int numHits , String field ) { } @ Override public int compare ( int slot1 , int slot2 ) { } @ Override public int compareBottom ( int doc ) { } @ Override public void copy ( int slot , int doc ) { } @ Override public int compareDocToValue ( int doc , BytesRef value ) { } abstract class PerSegmentComparator extends FieldComparator < BytesRef > { @ Override public FieldComparator < BytesRef > setNextReader ( AtomicReaderContext context ) throws IOException { } @ Override public int compare ( int slot1 , int slot2 ) { } @ Override public void setBottom ( final int bottom ) { FieldComparator . TermOrdValComparator . this . setBottom ( bottom ) ; } @ Override public BytesRef value ( int slot ) { } @ Override public int compareValues ( BytesRef val1 , BytesRef val2 ) { } @ Override public int compareDocToValue ( int doc , BytesRef value ) { } } private final class AnyOrdComparator extends FieldComparator . TermOrdValComparator . PerSegmentComparator { private final SortedDocValues termsIndex ; private final int docBase ; public AnyOrdComparator ( SortedDocValues termsIndex , int docBase ) { } @ Override public int compareBottom ( int doc ) { } @ Override public void copy ( int slot , int doc ) { } } @ Override public FieldComparator < BytesRef > setNextReader ( AtomicReaderContext context ) throws IOException { } @ Override public void setBottom ( final int bottom ) { bottomSlot = bottom ; bottomValue = values [ bottomSlot ] ; if ( ( currentReaderGen ) == ( readerGen [ bottomSlot ] ) ) { bottomOrd = ords [ bottomSlot ] ; bottomSameReader = true ; } else { if ( ( bottomValue ) == null ) { assert ( ords [ bottomSlot ] ) == ( - 1 ) ; bottomOrd = - 1 ; bottomSameReader = true ; readerGen [ bottomSlot ] = currentReaderGen ; } else { <START_BUG> final int index = termsIndex . lookupTerm ( bottomValue , tempBR ) ; <END_BUG> if ( index < 0 ) { bottomOrd = ( - index ) - 2 ; bottomSameReader = false ; } else { bottomOrd = index ; bottomSameReader = true ; readerGen [ bottomSlot ] = currentReaderGen ; ords [ bottomSlot ] = bottomOrd ; } } } } @ Override public BytesRef value ( int slot ) { } } public static final class TermValComparator extends FieldComparator < BytesRef > { private BytesRef [ ] values ; private BinaryDocValues docTerms ; private final String field ; private BytesRef bottom ; private final BytesRef tempBR = new BytesRef ( ) ; TermValComparator ( int numHits , String field ) { } @ Override public int compare ( int slot1 , int slot2 ) { } @ Override public int compareBottom ( int doc ) { } @ Override public void copy ( int slot , int doc ) { } @ Override public FieldComparator < BytesRef > setNextReader ( AtomicReaderContext context ) throws IOException { } @ Override public void setBottom ( final int bottom ) { this . bottom = values [ bottom ] ; } @ Override public BytesRef value ( int slot ) { } @ Override public int compareValues ( BytesRef val1 , BytesRef val2 ) { } @ Override public int compareDocToValue ( int doc , BytesRef value ) { } } }

Fix:
final int index = termsIndex . lookupTerm ( bottomValue ) ;