Thumbnail file hits. Based on a patch from D Bera
[beagle.git] / beagled / Lucene.Net / Search / IndexSearcher.cs
blobc0eae88539d9be4a45fc3f2cc31a05be2ac6daac
1 /*
2 * Copyright 2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 using System;
17 using Document = Lucene.Net.Documents.Document;
18 using IndexReader = Lucene.Net.Index.IndexReader;
19 using Term = Lucene.Net.Index.Term;
20 using Directory = Lucene.Net.Store.Directory;
21 namespace Lucene.Net.Search
24 /// <summary>Implements search over a single IndexReader.
25 ///
26 /// <p>Applications usually need only call the inherited {@link #Search(Query)}
27 /// or {@link #Search(Query,Filter)} methods.
28 /// </summary>
29 public class IndexSearcher : Searcher
31 private class AnonymousClassHitCollector : HitCollector
33 public AnonymousClassHitCollector(System.Collections.BitArray bits, int[] totalHits, Lucene.Net.Search.HitQueue hq, int nDocs, IndexSearcher enclosingInstance)
35 InitBlock(bits, totalHits, hq, nDocs, enclosingInstance);
37 private void InitBlock(System.Collections.BitArray bits, int[] totalHits, Lucene.Net.Search.HitQueue hq, int nDocs, IndexSearcher enclosingInstance)
39 this.bits = bits;
40 this.totalHits = totalHits;
41 this.hq = hq;
42 this.nDocs = nDocs;
43 this.enclosingInstance = enclosingInstance;
45 private System.Collections.BitArray bits;
46 private int[] totalHits;
47 private Lucene.Net.Search.HitQueue hq;
48 private int nDocs;
49 private IndexSearcher enclosingInstance;
50 public IndexSearcher Enclosing_Instance
52 get
54 return enclosingInstance;
58 private float minScore = 0.0f;
59 public override void Collect(int doc, float score)
61 if (score > 0.0f && (bits == null || bits.Get(doc)))
63 // skip docs not in bits
64 totalHits[0]++;
65 if (hq.Size() < nDocs || score >= minScore)
67 hq.Insert(new ScoreDoc(doc, score));
68 minScore = ((ScoreDoc) hq.Top()).score; // maintain minScore
73 private class AnonymousClassHitCollector1 : HitCollector
75 public AnonymousClassHitCollector1(System.Collections.BitArray bits, int[] totalHits, Lucene.Net.Search.FieldSortedHitQueue hq, IndexSearcher enclosingInstance)
77 InitBlock(bits, totalHits, hq, enclosingInstance);
79 private void InitBlock(System.Collections.BitArray bits, int[] totalHits, Lucene.Net.Search.FieldSortedHitQueue hq, IndexSearcher enclosingInstance)
81 this.bits = bits;
82 this.totalHits = totalHits;
83 this.hq = hq;
84 this.enclosingInstance = enclosingInstance;
86 private System.Collections.BitArray bits;
87 private int[] totalHits;
88 private Lucene.Net.Search.FieldSortedHitQueue hq;
89 private IndexSearcher enclosingInstance;
90 public IndexSearcher Enclosing_Instance
92 get
94 return enclosingInstance;
98 public override void Collect(int doc, float score)
100 if (score > 0.0f && (bits == null || bits.Get(doc)))
102 // skip docs not in bits
103 totalHits[0]++;
104 hq.Insert(new FieldDoc(doc, score));
108 private class AnonymousClassHitCollector2 : HitCollector
110 public AnonymousClassHitCollector2(System.Collections.BitArray bits, Lucene.Net.Search.HitCollector results, IndexSearcher enclosingInstance)
112 InitBlock(bits, results, enclosingInstance);
114 private void InitBlock(System.Collections.BitArray bits, Lucene.Net.Search.HitCollector results, IndexSearcher enclosingInstance)
116 this.bits = bits;
117 this.results = results;
118 this.enclosingInstance = enclosingInstance;
120 private System.Collections.BitArray bits;
121 private Lucene.Net.Search.HitCollector results;
122 private IndexSearcher enclosingInstance;
123 public IndexSearcher Enclosing_Instance
127 return enclosingInstance;
131 public override void Collect(int doc, float score)
133 if (bits.Get(doc))
135 // skip docs not in bits
136 results.Collect(doc, score);
140 public /*internal*/ IndexReader reader;
141 private bool closeReader;
143 /// <summary>Creates a searcher searching the index in the named directory. </summary>
144 public IndexSearcher(System.String path) : this(IndexReader.Open(path), true)
148 /// <summary>Creates a searcher searching the index in the provided directory. </summary>
149 public IndexSearcher(Directory directory) : this(IndexReader.Open(directory), true)
153 /// <summary>Creates a searcher searching the provided index. </summary>
154 public IndexSearcher(IndexReader r) : this(r, false)
158 private IndexSearcher(IndexReader r, bool closeReader)
160 reader = r;
161 this.closeReader = closeReader;
164 /// <summary> Note that the underlying IndexReader is not closed, if
165 /// IndexSearcher was constructed with IndexSearcher(IndexReader r).
166 /// If the IndexReader was supplied implicitly by specifying a directory, then
167 /// the IndexReader gets closed.
168 /// </summary>
169 public override void Close()
171 if (closeReader)
172 reader.Close();
175 // inherit javadoc
176 public override int DocFreq(Term term)
178 return reader.DocFreq(term);
181 // inherit javadoc
182 public override Document Doc(int i)
184 return reader.Document(i);
187 // inherit javadoc
188 public override int MaxDoc()
190 return reader.MaxDoc();
193 // inherit javadoc
194 public override TopDocs Search(Query query, Filter filter, int nDocs)
196 Scorer scorer = query.Weight(this).Scorer(reader);
197 if (scorer == null)
198 return new TopDocs(0, new ScoreDoc[0]);
200 System.Collections.BitArray bits = filter != null ? filter.Bits(reader) : null;
201 HitQueue hq = new HitQueue(nDocs);
202 int[] totalHits = new int[1];
203 scorer.Score(new AnonymousClassHitCollector(bits, totalHits, hq, nDocs, this));
205 ScoreDoc[] scoreDocs = new ScoreDoc[hq.Size()];
206 for (int i = hq.Size() - 1; i >= 0; i--)
207 // put docs in array
208 scoreDocs[i] = (ScoreDoc) hq.Pop();
210 return new TopDocs(totalHits[0], scoreDocs);
213 // inherit javadoc
214 public override TopFieldDocs Search(Query query, Filter filter, int nDocs, Sort sort)
216 Scorer scorer = query.Weight(this).Scorer(reader);
217 if (scorer == null)
218 return new TopFieldDocs(0, new ScoreDoc[0], sort.fields);
220 System.Collections.BitArray bits = filter != null ? filter.Bits(reader) : null;
221 FieldSortedHitQueue hq = new FieldSortedHitQueue(reader, sort.fields, nDocs);
222 int[] totalHits = new int[1];
223 scorer.Score(new AnonymousClassHitCollector1(bits, totalHits, hq, this));
225 ScoreDoc[] scoreDocs = new ScoreDoc[hq.Size()];
226 for (int i = hq.Size() - 1; i >= 0; i--)
227 // put docs in array
228 scoreDocs[i] = hq.FillFields((FieldDoc) hq.Pop());
230 return new TopFieldDocs(totalHits[0], scoreDocs, hq.GetFields());
234 // inherit javadoc
235 public override void Search(Query query, Filter filter, HitCollector results)
237 HitCollector collector = results;
238 if (filter != null)
240 System.Collections.BitArray bits = filter.Bits(reader);
241 collector = new AnonymousClassHitCollector2(bits, results, this);
244 Scorer scorer = query.Weight(this).Scorer(reader);
245 if (scorer == null)
246 return ;
247 scorer.Score(collector);
250 public override Query Rewrite(Query original)
252 Query query = original;
253 for (Query rewrittenQuery = query.Rewrite(reader); rewrittenQuery != query; rewrittenQuery = query.Rewrite(reader))
255 query = rewrittenQuery;
257 return query;
260 public override Explanation Explain(Query query, int doc)
262 return query.Weight(this).Explain(reader, doc);