2 * Copyright 2004 The Apache Software Foundation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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.
26 /// <p>Applications usually need only call the inherited {@link #Search(Query)}
27 /// or {@link #Search(Query,Filter)} methods.
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
)
40 this.totalHits
= totalHits
;
43 this.enclosingInstance
= enclosingInstance
;
45 private System
.Collections
.BitArray bits
;
46 private int[] totalHits
;
47 private Lucene
.Net
.Search
.HitQueue hq
;
49 private IndexSearcher enclosingInstance
;
50 public IndexSearcher Enclosing_Instance
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
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
)
82 this.totalHits
= totalHits
;
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
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
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
)
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
)
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
)
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.
169 public override void Close()
176 public override int DocFreq(Term term
)
178 return reader
.DocFreq(term
);
182 public override Document
Doc(int i
)
184 return reader
.Document(i
);
188 public override int MaxDoc()
190 return reader
.MaxDoc();
194 public override TopDocs
Search(Query query
, Filter filter
, int nDocs
)
196 Scorer scorer
= query
.Weight(this).Scorer(reader
);
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
--)
208 scoreDocs
[i
] = (ScoreDoc
) hq
.Pop();
210 return new TopDocs(totalHits
[0], scoreDocs
);
214 public override TopFieldDocs
Search(Query query
, Filter filter
, int nDocs
, Sort sort
)
216 Scorer scorer
= query
.Weight(this).Scorer(reader
);
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
--)
228 scoreDocs
[i
] = hq
.FillFields((FieldDoc
) hq
.Pop());
230 return new TopFieldDocs(totalHits
[0], scoreDocs
, hq
.GetFields());
235 public override void Search(Query query
, Filter filter
, HitCollector results
)
237 HitCollector collector
= results
;
240 System
.Collections
.BitArray bits
= filter
.Bits(reader
);
241 collector
= new AnonymousClassHitCollector2(bits
, results
, this);
244 Scorer scorer
= query
.Weight(this).Scorer(reader
);
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
;
260 public override Explanation
Explain(Query query
, int doc
)
262 return query
.Weight(this).Explain(reader
, doc
);