20 namespace Lucene.Net.Search
37 this.enclosingInstance = enclosingInstance;
44 return enclosingInstance;
48 internal AnonymousClassDisjunctionSumScorer(
BooleanScorer2 enclosingInstance, System.Collections.Generic.IList<
Scorer> scorers,
int minNrShouldMatch)
49 : base(scorers, minNrShouldMatch)
51 InitBlock(enclosingInstance);
53 private int lastScoredDoc = - 1;
56 private float lastDocScore = System.Single.NaN;
57 public override float Score()
60 if (doc >= lastScoredDoc)
62 if (doc > lastScoredDoc)
64 lastDocScore = base.Score();
67 Enclosing_Instance.coordinator.nrMatchers += base.nrMatchers;
74 private void InitBlock(
int requiredNrMatchers,
BooleanScorer2 enclosingInstance)
76 this.requiredNrMatchers = requiredNrMatchers;
77 this.enclosingInstance = enclosingInstance;
79 private int requiredNrMatchers;
85 return enclosingInstance;
89 internal AnonymousClassConjunctionScorer(
int requiredNrMatchers,
BooleanScorer2 enclosingInstance, Lucene.Net.Search.
Similarity defaultSimilarity, System.Collections.Generic.IList<
Scorer> requiredScorers)
90 : base(defaultSimilarity, requiredScorers)
92 InitBlock(requiredNrMatchers, enclosingInstance);
94 private int lastScoredDoc = - 1;
97 private float lastDocScore = System.Single.NaN;
98 public override float Score()
101 if (doc >= lastScoredDoc)
103 if (doc > lastScoredDoc)
105 lastDocScore = base.Score();
108 Enclosing_Instance.coordinator.nrMatchers += requiredNrMatchers;
118 private System.Collections.Generic.List<
Scorer> requiredScorers;
119 private System.Collections.Generic.List<
Scorer> optionalScorers;
120 private System.Collections.Generic.List<
Scorer> prohibitedScorers;
122 private class Coordinator
126 InitBlock(enclosingInstance);
130 this.enclosingInstance = enclosingInstance;
137 return enclosingInstance;
141 internal float[] coordFactors = null;
142 internal int maxCoord = 0;
143 internal int nrMatchers;
145 internal virtual void Init()
148 coordFactors =
new float[maxCoord + 1];
150 for (
int i = 0; i <= maxCoord; i++)
152 coordFactors[i] = sim.
Coord(i, maxCoord);
157 private Coordinator coordinator;
162 private Scorer countingSumScorer;
165 private int minNrShouldMatch;
167 private int doc = - 1;
187 System.Collections.Generic.List<
Scorer> required,
188 System.Collections.Generic.List<
Scorer> prohibited,
189 System.Collections.Generic.List<
Scorer> optional)
192 if (minNrShouldMatch < 0)
194 throw new System.ArgumentException(
"Minimum number of optional scorers should not be negative");
196 coordinator =
new Coordinator(
this);
197 this.minNrShouldMatch = minNrShouldMatch;
199 optionalScorers = optional;
200 coordinator.maxCoord += optional.Count;
202 requiredScorers = required;
203 coordinator.maxCoord += required.Count;
205 prohibitedScorers = prohibited;
208 countingSumScorer = MakeCountingSumScorer();
212 private class SingleMatchScorer:
Scorer
216 this.enclosingInstance = enclosingInstance;
218 private BooleanScorer2 enclosingInstance;
219 public BooleanScorer2 Enclosing_Instance
223 return enclosingInstance;
228 private int lastScoredDoc = - 1;
231 private float lastDocScore = System.Single.NaN;
233 internal SingleMatchScorer(BooleanScorer2 enclosingInstance,
Scorer scorer):base(scorer.
Similarity)
235 InitBlock(enclosingInstance);
236 this.scorer = scorer;
238 public override float Score()
241 if (doc >= lastScoredDoc)
243 if (doc > lastScoredDoc)
245 lastDocScore = scorer.Score();
248 Enclosing_Instance.coordinator.nrMatchers++;
253 public override int DocID()
255 return scorer.DocID();
258 public override int NextDoc()
260 return scorer.NextDoc();
263 public override int Advance(
int target)
265 return scorer.Advance(target);
269 private Scorer CountingDisjunctionSumScorer(System.Collections.Generic.List<
Scorer> scorers,
int minNrShouldMatch)
272 return new AnonymousClassDisjunctionSumScorer(
this, scorers, minNrShouldMatch);
275 private static readonly
Similarity defaultSimilarity;
277 private Scorer CountingConjunctionSumScorer(System.Collections.Generic.List<
Scorer> requiredScorers)
280 int requiredNrMatchers = requiredScorers.Count;
281 return new AnonymousClassConjunctionScorer(requiredNrMatchers,
this, defaultSimilarity, requiredScorers);
287 return new ConjunctionScorer(defaultSimilarity,
new Scorer[]{req1, req2});
297 private Scorer MakeCountingSumScorer()
300 return (requiredScorers.Count == 0)?MakeCountingSumScorerNoReq():MakeCountingSumScorerSomeReq();
303 private Scorer MakeCountingSumScorerNoReq()
307 int nrOptRequired = (minNrShouldMatch < 1)?1:minNrShouldMatch;
308 Scorer requiredCountingSumScorer;
309 if (optionalScorers.Count > nrOptRequired)
310 requiredCountingSumScorer = CountingDisjunctionSumScorer(optionalScorers, nrOptRequired);
311 else if (optionalScorers.Count == 1)
312 requiredCountingSumScorer =
new SingleMatchScorer(
this, optionalScorers[0]);
314 requiredCountingSumScorer = CountingConjunctionSumScorer(optionalScorers);
315 return AddProhibitedScorers(requiredCountingSumScorer);
318 private Scorer MakeCountingSumScorerSomeReq()
321 if (optionalScorers.Count == minNrShouldMatch)
324 var allReq =
new System.Collections.Generic.List<
Scorer>(requiredScorers);
325 allReq.AddRange(optionalScorers);
326 return AddProhibitedScorers(CountingConjunctionSumScorer(allReq));
331 Scorer requiredCountingSumScorer =
332 requiredScorers.Count == 1
333 ?
new SingleMatchScorer(
this, requiredScorers[0])
334 : CountingConjunctionSumScorer(requiredScorers);
335 if (minNrShouldMatch > 0)
338 return AddProhibitedScorers(DualConjunctionSumScorer(requiredCountingSumScorer, CountingDisjunctionSumScorer(optionalScorers, minNrShouldMatch)));
343 return new ReqOptSumScorer(AddProhibitedScorers(requiredCountingSumScorer),
344 optionalScorers.Count == 1
345 ?
new SingleMatchScorer(
this, optionalScorers[0])
346 : CountingDisjunctionSumScorer(optionalScorers, 1));
356 private Scorer AddProhibitedScorers(
Scorer requiredCountingSumScorer)
358 return (prohibitedScorers.Count == 0)
359 ? requiredCountingSumScorer
360 :
new ReqExclScorer(requiredCountingSumScorer,
361 ((prohibitedScorers.Count == 1)
362 ? prohibitedScorers[0]
363 :
new DisjunctionSumScorer(prohibitedScorers)));
372 while ((doc = countingSumScorer.NextDoc()) != NO_MORE_DOCS)
378 public override bool Score(
Collector collector,
int max,
int firstDocID)
385 doc = countingSumScorer.NextDoc();
387 return doc != NO_MORE_DOCS;
390 public override int DocID()
395 public override int NextDoc()
397 return doc = countingSumScorer.NextDoc();
400 public override float Score()
402 coordinator.nrMatchers = 0;
403 float sum = countingSumScorer.Score();
404 return sum * coordinator.coordFactors[coordinator.nrMatchers];
407 public override int Advance(
int target)
409 return doc = countingSumScorer.Advance(target);