Buggy code:
if ( limit < 0 ) { limit = Integer . MAX_VALUE ; } String lowerStr = params . get ( TERMS_LOWER ) ; String upperStr = params . get ( TERMS_UPPER ) ; boolean upperIncl = params . getBool ( TERMS_UPPER_INCLUSIVE , false ) ; boolean lowerIncl = params . getBool ( TERMS_LOWER_INCLUSIVE , true ) ; boolean sort = ! ( TERMS_SORT_INDEX . equals ( params . get ( TERMS_SORT , TERMS_SORT_COUNT ) ) ) ; int freqmin = params . getInt ( TERMS_MINCOUNT , 1 ) ; int freqmax = params . getInt ( TERMS_MAXCOUNT , TermsComponent . UNLIMITED_MAX_COUNT ) ; if ( freqmax < 0 ) { freqmax = Integer . MAX_VALUE ; } String prefix = params . get ( TERMS_PREFIX_STR ) ; String regexp = params . get ( TERMS_REGEXP_STR ) ; Pattern pattern = ( regexp != null ) ? Pattern . compile ( regexp , resolveRegexpFlags ( params ) ) : null ; boolean raw = params . getBool ( TERMS_RAW , false ) ; final IndexReader indexReader = rb . req . getSearcher ( ) . getTopReaderContext ( ) . reader ; Fields lfields = MultiFields . getFields ( indexReader ) ; for ( String field : fields ) { NamedList < Integer > fieldTerms = new NamedList < Integer > ( ) ; termsResult . add ( field , fieldTerms ) ; Terms terms = ( lfields == null ) ? null : lfields . terms ( field ) ; if ( terms == null ) { continue ; } FieldType ft = ( raw ) ? null : rb . req . getSchema ( ) . getFieldTypeNoEx ( field ) ; if ( ft == null ) ft = new StrField ( ) ; BytesRef prefixBytes = ( prefix == null ) ? null : new BytesRef ( prefix ) ; BytesRef upperBytes = null ; if ( upperStr != null ) { upperBytes = new BytesRef ( ) ; ft . readableToIndexed ( upperStr , upperBytes ) ; } BytesRef lowerBytes ; if ( lowerStr == null ) { lowerBytes = prefixBytes ; } else { lowerBytes = new BytesRef ( ) ; if ( raw ) { lowerBytes = new BytesRef ( lowerStr ) ; } else { lowerBytes = new BytesRef ( ) ; ft . readableToIndexed ( lowerStr , lowerBytes ) ; } } TermsEnum termsEnum = terms . iterator ( null ) ; BytesRef term = null ; if ( lowerBytes != null ) { if ( ( termsEnum . seekCeil ( lowerBytes , true ) ) == ( SeekStatus . END ) ) { termsEnum = null ; } else { term = termsEnum . term ( ) ; if ( ( lowerIncl == false ) && ( term . equals ( lowerBytes ) ) ) { term = termsEnum . next ( ) ; } } } else { term = termsEnum . next ( ) ; } int i = 0 ; BoundedTreeSet < CountPair < BytesRef , Integer > > queue = ( sort ) ? new BoundedTreeSet < CountPair < BytesRef , Integer > > ( limit ) : null ; CharsRef external = new CharsRef ( ) ; while ( ( term != null ) && ( ( i < limit ) || sort ) ) { boolean externalized = false ; if ( ( prefixBytes != null ) && ( ! ( term . startsWith ( prefixBytes ) ) ) ) break ; if ( pattern != null ) { ft . indexedToReadable ( term , external ) ; externalized = true ; if ( ! ( pattern . matcher ( external ) . matches ( ) ) ) { term = termsEnum . next ( ) ; continue ; } } if ( upperBytes != null ) { int upperCmp = term . compareTo ( upperBytes ) ; if ( ( upperCmp > 0 ) || ( ( upperCmp == 0 ) && ( ! upperIncl ) ) ) break ; } int docFreq = termsEnum . docFreq ( ) ; if ( ( docFreq >= freqmin ) && ( docFreq <= freqmax ) ) { if ( sort ) { <START_BUG> queue . add ( new CountPair < BytesRef , Integer > ( new BytesRef ( term ) , docFreq ) ) ; <END_BUG> } else { if ( ! externalized ) { ft . indexedToReadable ( term , external ) ; } fieldTerms . add ( external . toString ( ) , docFreq ) ; i ++ ; } } term = termsEnum . next ( ) ; } if ( sort ) { for ( CountPair < BytesRef , Integer > item : queue ) { if ( i >= limit ) break ; ft . indexedToReadable ( item . key , external ) ; fieldTerms . add ( external . toString ( ) , item . val ) ; i ++ ; } } } } int resolveRegexpFlags ( SolrParams params ) { } @ Override public int distributedProcess ( ResponseBuilder rb ) throws IOException { } @ Override public void handleResponses ( ResponseBuilder rb , ShardRequest sreq ) { } @ Override public void finishStage ( ResponseBuilder rb ) { } private ShardRequest createShardQuery ( SolrParams params ) { } public class TermsHelper { private HashMap < String , HashMap < String , TermsResponse . Term > > fieldmap ; private SolrParams params ; public TermsHelper ( ) { } public void init ( SolrParams params ) { } public void parse ( NamedList < NamedList < Number > > terms ) { } public NamedList buildResponse ( ) { } private Number num ( long val ) { } public Term [ ] getLexSorted ( HashMap < String , TermsResponse . Term > data ) { } public Term [ ] getCountSorted ( HashMap < String , TermsResponse . Term > data ) { } }

Fix:
queue . add ( new CountPair < BytesRef , Integer > ( BytesRef . deepCopyOf ( term ) , docFreq ) ) ;