KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > search > TopFieldDocCollector


1 package org.apache.lucene.search;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.io.IOException JavaDoc;
20 import java.util.BitSet JavaDoc;
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
27 /** A {@link HitCollector} implementation that collects the top-sorting
28  * documents, returning them as a {@link TopFieldDocs}. This is used by {@link
29  * IndexSearcher} to implement {@link TopFieldDocs}-based search.
30  *
31  * <p>This may be extended, overriding the collect method to, e.g.,
32  * conditionally invoke <code>super()</code> in order to filter which
33  * documents are collected.
34  **/

35 public class TopFieldDocCollector extends TopDocCollector {
36
37   /** Construct to collect a given number of hits.
38    * @param reader the index to be searched
39    * @param sort the sort criteria
40    * @param numHits the maximum number of hits to collect
41    */

42   public TopFieldDocCollector(IndexReader reader, Sort sort, int numHits)
43     throws IOException JavaDoc {
44     super(numHits, new FieldSortedHitQueue(reader, sort.fields, numHits));
45   }
46
47   // javadoc inherited
48
public void collect(int doc, float score) {
49     if (score > 0.0f) {
50       totalHits++;
51       hq.insert(new FieldDoc(doc, score));
52     }
53   }
54
55   // javadoc inherited
56
public TopDocs topDocs() {
57     FieldSortedHitQueue fshq = (FieldSortedHitQueue)hq;
58     ScoreDoc[] scoreDocs = new ScoreDoc[fshq.size()];
59     for (int i = fshq.size()-1; i >= 0; i--) // put docs in array
60
scoreDocs[i] = fshq.fillFields ((FieldDoc) fshq.pop());
61
62     return new TopFieldDocs(totalHits, scoreDocs,
63                             fshq.getFields(), fshq.getMaxScore());
64   }
65 }
66
Popular Tags