Lucene.Net  3.0.3
Lucene.Net is a .NET port of the Java Lucene Indexing Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties
DisjunctionSumScorer.cs
Go to the documentation of this file.
1 /*
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
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17 
18 using System;
19 
20 using ScorerDocQueue = Lucene.Net.Util.ScorerDocQueue;
21 
22 namespace Lucene.Net.Search
23 {
24 
29  {
31  private int nrScorers;
32 
34  protected internal System.Collections.Generic.IList<Scorer> subScorers;
35 
37  private int minimumNrMatchers;
38 
51  private ScorerDocQueue scorerDocQueue;
52 
54  private int currentDoc = - 1;
55 
57  protected internal int nrMatchers = - 1;
58 
59  private float currentScore = System.Single.NaN;
60 
72  public DisjunctionSumScorer(System.Collections.Generic.IList<Scorer> subScorers, int minimumNrMatchers):base(null)
73  {
74 
75  nrScorers = subScorers.Count;
76 
77  if (minimumNrMatchers <= 0)
78  {
79  throw new System.ArgumentException("Minimum nr of matchers must be positive");
80  }
81  if (nrScorers <= 1)
82  {
83  throw new System.ArgumentException("There must be at least 2 subScorers");
84  }
85 
86  this.minimumNrMatchers = minimumNrMatchers;
87  this.subScorers = subScorers;
88 
89  InitScorerDocQueue();
90  }
91 
95  public DisjunctionSumScorer(System.Collections.Generic.IList<Scorer> subScorers)
96  : this(subScorers, 1)
97  {
98  }
99 
103  private void InitScorerDocQueue()
104  {
105  scorerDocQueue = new ScorerDocQueue(nrScorers);
106  foreach(Scorer se in subScorers)
107  {
108  if (se.NextDoc() != NO_MORE_DOCS)
109  {
110  // doc() method will be used in scorerDocQueue.
111  scorerDocQueue.Insert(se);
112  }
113  }
114  }
115 
118  public override void Score(Collector collector)
119  {
120  collector.SetScorer(this);
121  while (NextDoc() != NO_MORE_DOCS)
122  {
123  collector.Collect(currentDoc);
124  }
125  }
126 
138  public /*protected internal*/ override bool Score(Collector collector, int max, int firstDocID)
139  {
140  // firstDocID is ignored since nextDoc() sets 'currentDoc'
141  collector.SetScorer(this);
142  while (currentDoc < max)
143  {
144  collector.Collect(currentDoc);
145  if (NextDoc() == NO_MORE_DOCS)
146  {
147  return false;
148  }
149  }
150  return true;
151  }
152 
153  public override int NextDoc()
154  {
155  if (scorerDocQueue.Size() < minimumNrMatchers || !AdvanceAfterCurrent())
156  {
157  currentDoc = NO_MORE_DOCS;
158  }
159  return currentDoc;
160  }
161 
181  protected internal virtual bool AdvanceAfterCurrent()
182  {
183  do
184  {
185  // repeat until minimum nr of matchers
186  currentDoc = scorerDocQueue.TopDoc();
187  currentScore = scorerDocQueue.TopScore();
188  nrMatchers = 1;
189  do
190  {
191  // Until all subscorers are after currentDoc
192  if (!scorerDocQueue.TopNextAndAdjustElsePop())
193  {
194  if (scorerDocQueue.Size() == 0)
195  {
196  break; // nothing more to advance, check for last match.
197  }
198  }
199  if (scorerDocQueue.TopDoc() != currentDoc)
200  {
201  break; // All remaining subscorers are after currentDoc.
202  }
203  currentScore += scorerDocQueue.TopScore();
204  nrMatchers++;
205  }
206  while (true);
207 
208  if (nrMatchers >= minimumNrMatchers)
209  {
210  return true;
211  }
212  else if (scorerDocQueue.Size() < minimumNrMatchers)
213  {
214  return false;
215  }
216  }
217  while (true);
218  }
219 
223  public override float Score()
224  {
225  return currentScore;
226  }
227 
228  public override int DocID()
229  {
230  return currentDoc;
231  }
232 
236  public virtual int NrMatchers()
237  {
238  return nrMatchers;
239  }
240 
251  public override int Advance(int target)
252  {
253  if (scorerDocQueue.Size() < minimumNrMatchers)
254  {
255  return currentDoc = NO_MORE_DOCS;
256  }
257  if (target <= currentDoc)
258  {
259  return currentDoc;
260  }
261  do
262  {
263  if (scorerDocQueue.TopDoc() >= target)
264  {
265  return AdvanceAfterCurrent()?currentDoc:(currentDoc = NO_MORE_DOCS);
266  }
267  else if (!scorerDocQueue.TopSkipToAndAdjustElsePop(target))
268  {
269  if (scorerDocQueue.Size() < minimumNrMatchers)
270  {
271  return currentDoc = NO_MORE_DOCS;
272  }
273  }
274  }
275  while (true);
276  }
277  }
278 }