2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 using PriorityQueue = Mono.Lucene.Net.Util.PriorityQueue;
22 namespace Mono.Lucene.Net.Search
25 /// <summary>A {@link HitCollector} implementation that collects the top-scoring
26 /// documents, returning them as a {@link TopDocs}. This is used by {@link
27 /// IndexSearcher} to implement {@link TopDocs}-based search.
29 /// <p/>This may be extended, overriding the collect method to, e.g.,
30 /// conditionally invoke <code>super()</code> in order to filter which
31 /// documents are collected.
34 /// <deprecated> Please use {@link TopScoreDocCollector}
35 /// instead, which has better performance.
38 [Obsolete("Please use TopScoreDocCollector instead, which has better performance.")]
39 public class TopDocCollector:HitCollector
42 private ScoreDoc reusableSD;
44 /// <summary>The total number of hits the collector encountered. </summary>
45 protected internal int totalHits;
47 /// <summary>The priority queue which holds the top-scoring documents. </summary>
48 protected internal PriorityQueue hq;
50 /// <summary>Construct to collect a given number of hits.</summary>
51 /// <param name="numHits">the maximum number of hits to collect
53 public TopDocCollector(int numHits):this(new HitQueue(numHits, false))
57 /// <deprecated> use TopDocCollector(hq) instead. numHits is not used by this
58 /// constructor. It will be removed in a future release.
60 [Obsolete("use TopDocCollector(hq) instead. numHits is not used by this constructor. It will be removed in a future release.")]
61 internal TopDocCollector(int numHits, PriorityQueue hq)
66 /// <summary>Constructor to collect the top-scoring documents by using the given PQ.</summary>
67 /// <param name="hq">the PQ to use by this instance.
69 protected internal TopDocCollector(PriorityQueue hq)
75 public override void Collect(int doc, float score)
80 if (reusableSD == null)
82 reusableSD = new ScoreDoc(doc, score);
84 else if (score >= reusableSD.score)
86 // reusableSD holds the last "rejected" entry, so, if
87 // this new score is not better than that, there's no
88 // need to try inserting it
90 reusableSD.score = score;
96 reusableSD = (ScoreDoc) hq.InsertWithOverflow(reusableSD);
100 /// <summary>The total number of documents that matched this query. </summary>
101 public virtual int GetTotalHits()
106 /// <summary>The top-scoring hits. </summary>
107 public virtual TopDocs TopDocs()
109 ScoreDoc[] scoreDocs = new ScoreDoc[hq.Size()];
110 for (int i = hq.Size() - 1; i >= 0; i--)
112 scoreDocs[i] = (ScoreDoc) hq.Pop();
114 float maxScore = (totalHits == 0)?System.Single.NegativeInfinity:scoreDocs[0].score;
116 return new TopDocs(totalHits, scoreDocs, maxScore);