lucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Che Dong" <>
Subject Hard to customize sort method in IndexSearcher via HitCollector
Date Thu, 28 Feb 2002 03:46:24 GMT
Hi all:
I don't know if you have the same problem like this:
some time you need use score as default sort method,
but if you want sort by a field(such as "rank") of document or sort by a
field of document and multi by score("rank * score"). you need to extent
Searcher and overwrite TopDocs method in your own MyIndexSearcher with
customized HitColloctor.

here is example for sort result with score multi by rank field;

.......line 113
scorer.score(new HitCollector() {
 public final void collect(int doc, float score) {
   if (score > 0.0f &&     // ignore zeroed buckets
       (bits==null || bits.get(doc))) {   // skip docs not in bits
     String rank = reader.doc(doc).getField("rank").stringValue();
     float r = Float.parseFloat(rank);
     score = score * r; //recalculate score with rank
     hq.put(new ScoreDoc(doc, score));   // update hit queue
       if (hq.size() > nDocs) {    // if hit queue overfull
          hq.pop();     // remove lowest in hit queue
      }, reader.maxDoc());

I think if Search have a abstract layer for Sorter and use Searcher like
search(Query,Filter,Sorter) will make lucene convenience for more


Che Dong

Do You Yahoo!?
Get your free address at

To unsubscribe, e-mail:   <>
For additional commands, e-mail: <>

View raw message