1 package org.apache.lucene.search; 2 3 18 19 import java.io.IOException ; 20 import java.util.BitSet ; 21 22 import org.apache.lucene.store.Directory; 23 import org.apache.lucene.document.Document; 24 import org.apache.lucene.index.IndexReader; 25 import org.apache.lucene.index.Term; 26 import org.apache.lucene.util.PriorityQueue; 27 28 36 public class TopDocCollector extends HitCollector { 37 private int numHits; 38 private float minScore = 0.0f; 39 40 int totalHits; 41 PriorityQueue hq; 42 43 46 public TopDocCollector(int numHits) { 47 this(numHits, new HitQueue(numHits)); 48 } 49 50 TopDocCollector(int numHits, PriorityQueue hq) { 51 this.numHits = numHits; 52 this.hq = hq; 53 } 54 55 public void collect(int doc, float score) { 57 if (score > 0.0f) { 58 totalHits++; 59 if (hq.size() < numHits || score >= minScore) { 60 hq.insert(new ScoreDoc(doc, score)); 61 minScore = ((ScoreDoc)hq.top()).score; } 63 } 64 } 65 66 67 public int getTotalHits() { return totalHits; } 68 69 70 public TopDocs topDocs() { 71 ScoreDoc[] scoreDocs = new ScoreDoc[hq.size()]; 72 for (int i = hq.size()-1; i >= 0; i--) scoreDocs[i] = (ScoreDoc)hq.pop(); 74 75 float maxScore = (totalHits==0) 76 ? Float.NEGATIVE_INFINITY 77 : scoreDocs[0].score; 78 79 return new TopDocs(totalHits, scoreDocs, maxScore); 80 } 81 } 82 | Popular Tags |