@Override
  public Query rewrite(IndexReader reader) throws IOException {
    if(!termLongEnough) {  // can only match if it's exact
      return new TermQuery(term);
    }

    int maxSize = BooleanQuery.getMaxClauseCount();
    PriorityQueue<ScoreTerm> stQueue = new PriorityQueue<ScoreTerm>();
    FilteredTermEnum enumerator = getEnum(reader);
    try {
      ScoreTerm st = new ScoreTerm();
      do {
        final Term t = enumerator.term();
        if (t == null) break;
        final float score = enumerator.difference();
        // ignore uncompetetive hits
        if (stQueue.size() >= maxSize && score <= stQueue.peek().score)
          continue;
        // add new entry in PQ
        st.term = t;
        st.score = score;
        stQueue.offer(st);
        // possibly drop entries from queue
        st = (stQueue.size() > maxSize) ? stQueue.poll() : new ScoreTerm();
      } while (enumerator.next());
    } finally {
      enumerator.close();
    }
    
    BooleanQuery query = new BooleanQuery(true);
    for (final ScoreTerm st : stQueue) {
      TermQuery tq = new TermQuery(st.term);      // found a match
      tq.setBoost(getBoost() * st.score); // set the boost
      query.add(tq, BooleanClause.Occur.SHOULD);          // add to query
    }

    return query;
  }