// inherit javadoc public TopDocs search(Query query, Filter filter, final int nDocs) throws IOException { Scorer scorer = query.weight(this).scorer(reader); if (scorer == null) return new TopDocs(0, new ScoreDoc[0]); final BitSet bits = filter != null ? filter.bits(reader) : null; final HitQueue hq = new HitQueue(nDocs); final int[] totalHits = new int[1]; scorer.score( new HitCollector() { private float minScore = 0.0f; 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 totalHits[0]++; if (hq.size() < nDocs || score >= minScore) { hq.insert(new ScoreDoc(doc, score)); minScore = ((ScoreDoc) hq.top()).score; // maintain minScore } } } }); ScoreDoc[] scoreDocs = new ScoreDoc[hq.size()]; for (int i = hq.size() - 1; i >= 0; i--) // put docs in array scoreDocs[i] = (ScoreDoc) hq.pop(); return new TopDocs(totalHits[0], scoreDocs); }
public TopDocs search(Query query, Filter filter, int nDocs) throws IOException { HitQueue hq = new HitQueue(nDocs); int totalHits = 0; for (int i = 0; i < searchables.length; i++) { // search each searcher TopDocs docs = searchables[i].search(query, filter, nDocs); totalHits += docs.totalHits; // update totalHits ScoreDoc[] scoreDocs = docs.scoreDocs; for (int j = 0; j < scoreDocs.length; j++) { // merge scoreDocs into hq ScoreDoc scoreDoc = scoreDocs[j]; scoreDoc.doc += starts[i]; // convert doc if (!hq.insert(scoreDoc)) break; // no more scores > minScore } } ScoreDoc[] scoreDocs = new ScoreDoc[hq.size()]; for (int i = hq.size() - 1; i >= 0; i--) // put docs in array scoreDocs[i] = (ScoreDoc) hq.pop(); return new TopDocs(totalHits, scoreDocs); }