public override Query Rewrite(IndexReader reader, MultiTermQuery query) { FilteredTermEnum enumerator = query.GetEnum(reader); BooleanQuery result = new BooleanQuery(true); int count = 0; try { do { Term t = enumerator.Term(); if (t != null) { TermQuery tq = new TermQuery(t); // found a match tq.SetBoost(query.GetBoost() * enumerator.Difference()); // set the boost result.Add(tq, BooleanClause.Occur.SHOULD); // add to query count++; } }while (enumerator.Next()); } finally { enumerator.Close(); } query.IncTotalNumberOfTerms(count); return(result); }
public override Query Rewrite(IndexReader reader) { if (!termLongEnough) { // can only match if it's exact return(new TermQuery(term)); } FilteredTermEnum enumerator = GetEnum(reader); int maxClauseCount = BooleanQuery.GetMaxClauseCount(); ScoreTermQueue stQueue = new ScoreTermQueue(maxClauseCount); ScoreTerm reusableST = null; try { do { float score = 0.0f; Term t = enumerator.Term(); if (t != null) { score = enumerator.Difference(); if (reusableST == null) { reusableST = new ScoreTerm(t, score); } else if (score >= reusableST.score) { // reusableST holds the last "rejected" entry, so, if // this new score is not better than that, there's no // need to try inserting it reusableST.score = score; reusableST.term = t; } else { continue; } reusableST = (ScoreTerm)stQueue.InsertWithOverflow(reusableST); } }while (enumerator.Next()); } finally { enumerator.Close(); } BooleanQuery query = new BooleanQuery(true); int size = stQueue.Size(); for (int i = 0; i < size; i++) { ScoreTerm st = (ScoreTerm)stQueue.Pop(); 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); }