19 using Lucene.Net.Util;
21 using Entry = Lucene.Net.Search.FieldValueHitQueue.Entry;
23 namespace Lucene.Net.Search
49 internal int reverseMul;
51 public OneComparatorNonScoringCollector(
FieldValueHitQueue queue,
int numHits,
bool fillFields):base(queue, numHits, fillFields)
53 comparator = queue.GetComparators()[0];
54 reverseMul = queue.GetReverseMul()[0];
57 internal void UpdateBottom(
int doc)
60 bottom.Doc = docBase + doc;
61 bottom = pq.UpdateTop();
64 public override void Collect(
int doc)
69 if ((reverseMul * comparator.CompareBottom(doc)) <= 0)
78 comparator.Copy(bottom.slot, doc);
80 comparator.SetBottom(bottom.slot);
85 int slot = internalTotalHits - 1;
87 comparator.Copy(slot, doc);
88 Add(slot, doc, System.Single.NaN);
91 comparator.SetBottom(bottom.slot);
96 public override void SetNextReader(
IndexReader reader,
int docBase)
98 this.docBase = docBase;
99 comparator.SetNextReader(reader, docBase);
102 public override void SetScorer(
Scorer scorer)
104 comparator.SetScorer(scorer);
113 private class OutOfOrderOneComparatorNonScoringCollector:OneComparatorNonScoringCollector
116 public OutOfOrderOneComparatorNonScoringCollector(
FieldValueHitQueue queue,
int numHits,
bool fillFields):base(queue, numHits, fillFields)
120 public override void Collect(
int doc)
126 int cmp = reverseMul * comparator.CompareBottom(doc);
127 if (cmp < 0 || (cmp == 0 && doc + docBase > bottom.Doc))
133 comparator.Copy(bottom.slot, doc);
135 comparator.SetBottom(bottom.slot);
140 int slot = internalTotalHits - 1;
142 comparator.Copy(slot, doc);
143 Add(slot, doc, System.Single.NaN);
146 comparator.SetBottom(bottom.slot);
151 public override bool AcceptsDocsOutOfOrder
161 private class OneComparatorScoringNoMaxScoreCollector : OneComparatorNonScoringCollector
166 public OneComparatorScoringNoMaxScoreCollector(
FieldValueHitQueue queue,
int numHits,
bool fillFields):base(queue, numHits, fillFields)
170 internal void UpdateBottom(
int doc,
float score)
172 bottom.Doc = docBase + doc;
173 bottom.Score = score;
174 bottom = pq.UpdateTop();
177 public override void Collect(
int doc)
182 if ((reverseMul * comparator.CompareBottom(doc)) <= 0)
191 float score = scorer.Score();
194 comparator.Copy(bottom.slot, doc);
195 UpdateBottom(doc, score);
196 comparator.SetBottom(bottom.slot);
201 float score = scorer.Score();
204 int slot = internalTotalHits - 1;
206 comparator.Copy(slot, doc);
207 Add(slot, doc, score);
210 comparator.SetBottom(bottom.slot);
215 public override void SetScorer(
Scorer scorer)
217 this.scorer = scorer;
218 comparator.SetScorer(scorer);
227 private class OutOfOrderOneComparatorScoringNoMaxScoreCollector : OneComparatorScoringNoMaxScoreCollector
230 public OutOfOrderOneComparatorScoringNoMaxScoreCollector(
FieldValueHitQueue queue,
int numHits,
bool fillFields):base(queue, numHits, fillFields)
234 public override void Collect(
int doc)
240 int cmp = reverseMul * comparator.CompareBottom(doc);
241 if (cmp < 0 || (cmp == 0 && doc + docBase > bottom.Doc))
247 float score = scorer.Score();
250 comparator.Copy(bottom.slot, doc);
251 UpdateBottom(doc, score);
252 comparator.SetBottom(bottom.slot);
257 float score = scorer.Score();
260 int slot = internalTotalHits - 1;
262 comparator.Copy(slot, doc);
263 Add(slot, doc, score);
266 comparator.SetBottom(bottom.slot);
271 public override bool AcceptsDocsOutOfOrder
281 private class OneComparatorScoringMaxScoreCollector:OneComparatorNonScoringCollector
286 public OneComparatorScoringMaxScoreCollector(
FieldValueHitQueue queue,
int numHits,
bool fillFields):base(queue, numHits, fillFields)
289 maxScore = System.Single.NegativeInfinity;
292 internal void UpdateBottom(
int doc,
float score)
294 bottom.Doc = docBase + doc;
295 bottom.Score = score;
296 bottom = pq.UpdateTop();
299 public override void Collect(
int doc)
301 float score = scorer.Score();
302 if (score > maxScore)
309 if ((reverseMul * comparator.CompareBottom(doc)) <= 0)
318 comparator.Copy(bottom.slot, doc);
319 UpdateBottom(doc, score);
320 comparator.SetBottom(bottom.slot);
325 int slot = internalTotalHits - 1;
327 comparator.Copy(slot, doc);
328 Add(slot, doc, score);
331 comparator.SetBottom(bottom.slot);
336 public override void SetScorer(
Scorer scorer)
338 this.scorer = scorer;
339 base.SetScorer(scorer);
348 private class OutOfOrderOneComparatorScoringMaxScoreCollector : OneComparatorScoringMaxScoreCollector
351 public OutOfOrderOneComparatorScoringMaxScoreCollector(
FieldValueHitQueue queue,
int numHits,
bool fillFields):base(queue, numHits, fillFields)
355 public override void Collect(
int doc)
357 float score = scorer.Score();
358 if (score > maxScore)
366 int cmp = reverseMul * comparator.CompareBottom(doc);
367 if (cmp < 0 || (cmp == 0 && doc + docBase > bottom.Doc))
373 comparator.Copy(bottom.slot, doc);
374 UpdateBottom(doc, score);
375 comparator.SetBottom(bottom.slot);
380 int slot = internalTotalHits - 1;
382 comparator.Copy(slot, doc);
383 Add(slot, doc, score);
386 comparator.SetBottom(bottom.slot);
391 public override bool AcceptsDocsOutOfOrder
404 internal int[] reverseMul;
406 public MultiComparatorNonScoringCollector(
FieldValueHitQueue queue,
int numHits,
bool fillFields):base(queue, numHits, fillFields)
408 comparators = queue.GetComparators();
409 reverseMul = queue.GetReverseMul();
412 internal void UpdateBottom(
int doc)
415 bottom.Doc = docBase + doc;
416 bottom = pq.UpdateTop();
419 public override void Collect(
int doc)
425 for (
int i = 0; ; i++)
427 int c = reverseMul[i] * comparators[i].CompareBottom(doc);
438 else if (i == comparators.Length - 1)
448 for (
int i = 0; i < comparators.Length; i++)
450 comparators[i].Copy(bottom.slot, doc);
455 for (
int i = 0; i < comparators.Length; i++)
457 comparators[i].SetBottom(bottom.slot);
463 int slot = internalTotalHits - 1;
465 for (
int i = 0; i < comparators.Length; i++)
467 comparators[i].Copy(slot, doc);
469 Add(slot, doc, System.Single.NaN);
472 for (
int i = 0; i < comparators.Length; i++)
474 comparators[i].SetBottom(bottom.slot);
480 public override void SetNextReader(
IndexReader reader,
int docBase)
482 this.docBase = docBase;
483 for (
int i = 0; i < comparators.Length; i++)
485 comparators[i].SetNextReader(reader, docBase);
489 public override void SetScorer(
Scorer scorer)
492 for (
int i = 0; i < comparators.Length; i++)
494 comparators[i].SetScorer(scorer);
504 private class OutOfOrderMultiComparatorNonScoringCollector:MultiComparatorNonScoringCollector
507 public OutOfOrderMultiComparatorNonScoringCollector(
FieldValueHitQueue queue,
int numHits,
bool fillFields):base(queue, numHits, fillFields)
511 public override void Collect(
int doc)
517 for (
int i = 0; ; i++)
519 int c = reverseMul[i] * comparators[i].CompareBottom(doc);
530 else if (i == comparators.Length - 1)
533 if (doc + docBase > bottom.Doc)
543 for (
int i = 0; i < comparators.Length; i++)
545 comparators[i].Copy(bottom.slot, doc);
550 for (
int i = 0; i < comparators.Length; i++)
552 comparators[i].SetBottom(bottom.slot);
558 int slot = internalTotalHits - 1;
560 for (
int i = 0; i < comparators.Length; i++)
562 comparators[i].Copy(slot, doc);
564 Add(slot, doc, System.Single.NaN);
567 for (
int i = 0; i < comparators.Length; i++)
569 comparators[i].SetBottom(bottom.slot);
575 public override bool AcceptsDocsOutOfOrder
585 private class MultiComparatorScoringMaxScoreCollector : MultiComparatorNonScoringCollector
590 public MultiComparatorScoringMaxScoreCollector(
FieldValueHitQueue queue,
int numHits,
bool fillFields):base(queue, numHits, fillFields)
593 maxScore = System.Single.NegativeInfinity;
596 internal void UpdateBottom(
int doc,
float score)
598 bottom.Doc = docBase + doc;
599 bottom.Score = score;
600 bottom = pq.UpdateTop();
603 public override void Collect(
int doc)
605 float score = scorer.Score();
606 if (score > maxScore)
614 for (
int i = 0; ; i++)
616 int c = reverseMul[i] * comparators[i].CompareBottom(doc);
627 else if (i == comparators.Length - 1)
637 for (
int i = 0; i < comparators.Length; i++)
639 comparators[i].Copy(bottom.slot, doc);
642 UpdateBottom(doc, score);
644 for (
int i = 0; i < comparators.Length; i++)
646 comparators[i].SetBottom(bottom.slot);
652 int slot = internalTotalHits - 1;
654 for (
int i = 0; i < comparators.Length; i++)
656 comparators[i].Copy(slot, doc);
658 Add(slot, doc, score);
661 for (
int i = 0; i < comparators.Length; i++)
663 comparators[i].SetBottom(bottom.slot);
669 public override void SetScorer(
Scorer scorer)
671 this.scorer = scorer;
672 base.SetScorer(scorer);
681 private sealed
class OutOfOrderMultiComparatorScoringMaxScoreCollector:MultiComparatorScoringMaxScoreCollector
684 public OutOfOrderMultiComparatorScoringMaxScoreCollector(
FieldValueHitQueue queue,
int numHits,
bool fillFields):base(queue, numHits, fillFields)
688 public override void Collect(
int doc)
690 float score = scorer.Score();
691 if (score > maxScore)
699 for (
int i = 0; ; i++)
701 int c = reverseMul[i] * comparators[i].CompareBottom(doc);
712 else if (i == comparators.Length - 1)
715 if (doc + docBase > bottom.Doc)
725 for (
int i = 0; i < comparators.Length; i++)
727 comparators[i].Copy(bottom.slot, doc);
730 UpdateBottom(doc, score);
732 for (
int i = 0; i < comparators.Length; i++)
734 comparators[i].SetBottom(bottom.slot);
740 int slot = internalTotalHits - 1;
742 for (
int i = 0; i < comparators.Length; i++)
744 comparators[i].Copy(slot, doc);
746 Add(slot, doc, score);
749 for (
int i = 0; i < comparators.Length; i++)
751 comparators[i].SetBottom(bottom.slot);
757 public override bool AcceptsDocsOutOfOrder
767 private class MultiComparatorScoringNoMaxScoreCollector:MultiComparatorNonScoringCollector
772 public MultiComparatorScoringNoMaxScoreCollector(
FieldValueHitQueue queue,
int numHits,
bool fillFields):base(queue, numHits, fillFields)
776 internal void UpdateBottom(
int doc,
float score)
778 bottom.Doc = docBase + doc;
779 bottom.Score = score;
780 bottom = pq.UpdateTop();
783 public override void Collect(
int doc)
789 for (
int i = 0; ; i++)
791 int c = reverseMul[i] * comparators[i].CompareBottom(doc);
802 else if (i == comparators.Length - 1)
812 for (
int i = 0; i < comparators.Length; i++)
814 comparators[i].Copy(bottom.slot, doc);
818 float score = scorer.Score();
819 UpdateBottom(doc, score);
821 for (
int i = 0; i < comparators.Length; i++)
823 comparators[i].SetBottom(bottom.slot);
829 int slot = internalTotalHits - 1;
831 for (
int i = 0; i < comparators.Length; i++)
833 comparators[i].Copy(slot, doc);
837 float score = scorer.Score();
838 Add(slot, doc, score);
841 for (
int i = 0; i < comparators.Length; i++)
843 comparators[i].SetBottom(bottom.slot);
849 public override void SetScorer(
Scorer scorer)
851 this.scorer = scorer;
852 base.SetScorer(scorer);
861 private sealed
class OutOfOrderMultiComparatorScoringNoMaxScoreCollector:MultiComparatorScoringNoMaxScoreCollector
864 public OutOfOrderMultiComparatorScoringNoMaxScoreCollector(
FieldValueHitQueue queue,
int numHits,
bool fillFields):base(queue, numHits, fillFields)
868 public override void Collect(
int doc)
874 for (
int i = 0; ; i++)
876 int c = reverseMul[i] * comparators[i].CompareBottom(doc);
887 else if (i == comparators.Length - 1)
890 if (doc + docBase > bottom.Doc)
900 for (
int i = 0; i < comparators.Length; i++)
902 comparators[i].Copy(bottom.slot, doc);
906 float score = scorer.Score();
907 UpdateBottom(doc, score);
909 for (
int i = 0; i < comparators.Length; i++)
911 comparators[i].SetBottom(bottom.slot);
917 int slot = internalTotalHits - 1;
919 for (
int i = 0; i < comparators.Length; i++)
921 comparators[i].Copy(slot, doc);
925 float score = scorer.Score();
926 Add(slot, doc, score);
929 for (
int i = 0; i < comparators.Length; i++)
931 comparators[i].SetBottom(bottom.slot);
937 public override void SetScorer(
Scorer scorer)
939 this.scorer = scorer;
940 base.SetScorer(scorer);
943 public override bool AcceptsDocsOutOfOrder
951 private bool fillFields;
957 internal float maxScore = System.Single.NaN;
959 internal int numHits;
961 internal bool queueFull;
962 internal int docBase;
972 this.numHits = numHits;
973 this.fillFields = fillFields;
1012 public static TopFieldCollector Create(
Sort sort,
int numHits,
bool fillFields,
bool trackDocScores,
bool trackMaxScore,
bool docsScoredInOrder)
1014 if (sort.fields.Length == 0)
1016 throw new System.ArgumentException(
"Sort must contain at least one field");
1020 if (queue.GetComparators().Length == 1)
1022 if (docsScoredInOrder)
1026 return new OneComparatorScoringMaxScoreCollector(queue, numHits, fillFields);
1028 else if (trackDocScores)
1030 return new OneComparatorScoringNoMaxScoreCollector(queue, numHits, fillFields);
1034 return new OneComparatorNonScoringCollector(queue, numHits, fillFields);
1041 return new OutOfOrderOneComparatorScoringMaxScoreCollector(queue, numHits, fillFields);
1043 else if (trackDocScores)
1045 return new OutOfOrderOneComparatorScoringNoMaxScoreCollector(queue, numHits, fillFields);
1049 return new OutOfOrderOneComparatorNonScoringCollector(queue, numHits, fillFields);
1055 if (docsScoredInOrder)
1059 return new MultiComparatorScoringMaxScoreCollector(queue, numHits, fillFields);
1061 else if (trackDocScores)
1063 return new MultiComparatorScoringNoMaxScoreCollector(queue, numHits, fillFields);
1067 return new MultiComparatorNonScoringCollector(queue, numHits, fillFields);
1074 return new OutOfOrderMultiComparatorScoringMaxScoreCollector(queue, numHits, fillFields);
1076 else if (trackDocScores)
1078 return new OutOfOrderMultiComparatorScoringNoMaxScoreCollector(queue, numHits, fillFields);
1082 return new OutOfOrderMultiComparatorNonScoringCollector(queue, numHits, fillFields);
1087 internal void Add(
int slot,
int doc,
float score)
1089 bottom = pq.Add(
new Entry(slot, docBase + doc, score));
1090 queueFull = internalTotalHits == numHits;
1098 protected internal override void PopulateResults(
ScoreDoc[] results,
int howMany)
1103 FieldValueHitQueue queue = (FieldValueHitQueue) pq;
1104 for (
int i = howMany - 1; i >= 0; i--)
1106 results[i] = queue.FillFields(queue.Pop());
1111 for (
int i = howMany - 1; i >= 0; i--)
1113 Entry entry = pq.Pop();
1114 results[i] =
new FieldDoc(entry.Doc, entry.Score);
1121 if (results == null)
1123 results = EMPTY_SCOREDOCS;
1125 maxScore = System.Single.NaN;
1132 public override bool AcceptsDocsOutOfOrder
1134 get {
return false; }