KickJava   Java API By Example, From Geeks To Geeks.

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


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 /** Implements search over a single IndexReader.
28  *
29  * <p>Applications usually need only call the inherited {@link #search(Query)}
30  * or {@link #search(Query,Filter)} methods. For performance reasons it is
31  * recommended to open only one IndexSearcher and use it for all of your searches.
32  *
33  * <p>Note that you can only access Hits from an IndexSearcher as long as it is
34  * not yet closed, otherwise an IOException will be thrown.
35  */

36 public class IndexSearcher extends Searcher {
37   IndexReader reader;
38   private boolean closeReader;
39
40   /** Creates a searcher searching the index in the named directory. */
41   public IndexSearcher(String JavaDoc path) throws IOException JavaDoc {
42     this(IndexReader.open(path), true);
43   }
44
45   /** Creates a searcher searching the index in the provided directory. */
46   public IndexSearcher(Directory directory) throws IOException JavaDoc {
47     this(IndexReader.open(directory), true);
48   }
49
50   /** Creates a searcher searching the provided index. */
51   public IndexSearcher(IndexReader r) {
52     this(r, false);
53   }
54   
55   private IndexSearcher(IndexReader r, boolean closeReader) {
56     reader = r;
57     this.closeReader = closeReader;
58   }
59
60   /** Return the {@link IndexReader} this searches. */
61   public IndexReader getIndexReader() {
62     return reader;
63   }
64
65   /**
66    * Note that the underlying IndexReader is not closed, if
67    * IndexSearcher was constructed with IndexSearcher(IndexReader r).
68    * If the IndexReader was supplied implicitly by specifying a directory, then
69    * the IndexReader gets closed.
70    */

71   public void close() throws IOException JavaDoc {
72     if(closeReader)
73       reader.close();
74   }
75
76   // inherit javadoc
77
public int docFreq(Term term) throws IOException JavaDoc {
78     return reader.docFreq(term);
79   }
80
81   // inherit javadoc
82
public Document doc(int i) throws IOException JavaDoc {
83     return reader.document(i);
84   }
85
86   // inherit javadoc
87
public int maxDoc() throws IOException JavaDoc {
88     return reader.maxDoc();
89   }
90
91   // inherit javadoc
92
public TopDocs search(Weight weight, Filter filter, final int nDocs)
93        throws IOException JavaDoc {
94
95     if (nDocs <= 0) // null might be returned from hq.top() below.
96
throw new IllegalArgumentException JavaDoc("nDocs must be > 0");
97
98     TopDocCollector collector = new TopDocCollector(nDocs);
99     search(weight, filter, collector);
100     return collector.topDocs();
101   }
102
103   // inherit javadoc
104
public TopFieldDocs search(Weight weight, Filter filter, final int nDocs,
105                              Sort sort)
106       throws IOException JavaDoc {
107
108     TopFieldDocCollector collector =
109       new TopFieldDocCollector(reader, sort, nDocs);
110     search(weight, filter, collector);
111     return (TopFieldDocs)collector.topDocs();
112   }
113
114   // inherit javadoc
115
public void search(Weight weight, Filter filter,
116                      final HitCollector results) throws IOException JavaDoc {
117     HitCollector collector = results;
118     if (filter != null) {
119       final BitSet JavaDoc bits = filter.bits(reader);
120       collector = new HitCollector() {
121           public final void collect(int doc, float score) {
122             if (bits.get(doc)) { // skip docs not in bits
123
results.collect(doc, score);
124             }
125           }
126         };
127     }
128
129     Scorer scorer = weight.scorer(reader);
130     if (scorer == null)
131       return;
132     scorer.score(collector);
133   }
134
135   public Query rewrite(Query original) throws IOException JavaDoc {
136     Query query = original;
137     for (Query rewrittenQuery = query.rewrite(reader); rewrittenQuery != query;
138          rewrittenQuery = query.rewrite(reader)) {
139       query = rewrittenQuery;
140     }
141     return query;
142   }
143
144   public Explanation explain(Weight weight, int doc) throws IOException JavaDoc {
145     return weight.explain(reader, doc);
146   }
147 }
148
Popular Tags