question: private long collectTerms ( LeafReaderContext context , TermsEnum termsEnum , List < TermAndState > terms ) throws IOException { long count = 0 ; final int threshold = Math . min ( BOOLEAN_REWRITE_TERM_COUNT_THRESHOLD , BooleanQuery . getMaxClauseCount ( ) ) ; for ( int i = 0 ; i < threshold ; ++ i ) { final BytesRef term = termsEnum . next ( ) ; if ( term == null ) { return - count - 1 ; } TermState state = termsEnum . termState ( ) ; int df = termsEnum . docFreq ( ) ; count += df ; terms . add ( new TermAndState ( BytesRef . deepCopyOf ( term ) , state , df , termsEnum . totalTermFreq ( ) ) ) ; } return termsEnum . next ( ) == null ? ( - count - 1 ) : count ; }
target:  Try to collect terms from the given terms enum and return count=sum(df) for terms visited so far or (-count - 1) if this should be rewritten into a boolean query. The termEnum will already be positioned on the next term if not exhausted.