@@ -50,9 +50,10 @@ public class FirstPassGroupingCollector<T> extends SimpleCollector {
50
50
private final LeafFieldComparator [] leafComparators ;
51
51
private final int [] reversed ;
52
52
private final int topNGroups ;
53
- private final boolean needsScores ;
54
53
private final HashMap <T , CollectedSearchGroup <T >> groupMap ;
55
54
private final int compIDXEnd ;
55
+ private final ScoreMode scoreMode ;
56
+ private final boolean canSetMinScore ;
56
57
57
58
// Set once we reach topNGroups unique groups:
58
59
/**
@@ -62,6 +63,9 @@ public class FirstPassGroupingCollector<T> extends SimpleCollector {
62
63
63
64
private int docBase ;
64
65
private int spareSlot ;
66
+ private Scorable scorer ;
67
+ private int bottomSlot ;
68
+ private float minCompetitiveScore ;
65
69
66
70
/**
67
71
* Create the first pass collector.
@@ -83,7 +87,6 @@ public FirstPassGroupingCollector(
83
87
// and specialize it?
84
88
85
89
this .topNGroups = topNGroups ;
86
- this .needsScores = groupSort .needsScores ();
87
90
final SortField [] sortFields = groupSort .getSort ();
88
91
comparators = new FieldComparator <?>[sortFields .length ];
89
92
leafComparators = new LeafFieldComparator [sortFields .length ];
@@ -105,13 +108,21 @@ public FirstPassGroupingCollector(
105
108
reversed [i ] = sortField .getReverse () ? -1 : 1 ;
106
109
}
107
110
111
+ if (SortField .FIELD_SCORE .equals (sortFields [0 ]) == true ) {
112
+ scoreMode = ScoreMode .TOP_SCORES ;
113
+ canSetMinScore = true ;
114
+ } else {
115
+ scoreMode = groupSort .needsScores () ? ScoreMode .TOP_DOCS_WITH_SCORES : ScoreMode .TOP_DOCS ;
116
+ canSetMinScore = false ;
117
+ }
118
+
108
119
spareSlot = topNGroups ;
109
120
groupMap = CollectionUtil .newHashMap (topNGroups );
110
121
}
111
122
112
123
@ Override
113
124
public ScoreMode scoreMode () {
114
- return needsScores ? ScoreMode . COMPLETE : ScoreMode . COMPLETE_NO_SCORES ;
125
+ return scoreMode ;
115
126
}
116
127
117
128
/**
@@ -162,10 +173,12 @@ public Collection<SearchGroup<T>> getTopGroups(int groupOffset) throws IOExcepti
162
173
163
174
@ Override
164
175
public void setScorer (Scorable scorer ) throws IOException {
176
+ this .scorer = scorer ;
165
177
groupSelector .setScorer (scorer );
166
178
for (LeafFieldComparator comparator : leafComparators ) {
167
179
comparator .setScorer (scorer );
168
180
}
181
+ setMinCompetitiveScore (scorer );
169
182
}
170
183
171
184
private boolean isCompetitive (int doc ) throws IOException {
@@ -273,9 +286,7 @@ private void collectNewGroup(final int doc) throws IOException {
273
286
assert orderedGroups .size () == topNGroups ;
274
287
275
288
final int lastComparatorSlot = orderedGroups .last ().comparatorSlot ;
276
- for (LeafFieldComparator fc : leafComparators ) {
277
- fc .setBottom (lastComparatorSlot );
278
- }
289
+ setBottomSlot (lastComparatorSlot );
279
290
}
280
291
}
281
292
@@ -331,9 +342,7 @@ private void collectExistingGroup(final int doc, final CollectedSearchGroup<T> g
331
342
// If we changed the value of the last group, or changed which group was last, then update
332
343
// bottom:
333
344
if (group == newLast || prevLast != newLast ) {
334
- for (LeafFieldComparator fc : leafComparators ) {
335
- fc .setBottom (newLast .comparatorSlot );
336
- }
345
+ setBottomSlot (newLast .comparatorSlot );
337
346
}
338
347
}
339
348
}
@@ -364,13 +373,12 @@ public int compare(CollectedSearchGroup<?> o1, CollectedSearchGroup<?> o2) {
364
373
orderedGroups .addAll (groupMap .values ());
365
374
assert orderedGroups .size () > 0 ;
366
375
367
- for (LeafFieldComparator fc : leafComparators ) {
368
- fc .setBottom (orderedGroups .last ().comparatorSlot );
369
- }
376
+ setBottomSlot (orderedGroups .last ().comparatorSlot );
370
377
}
371
378
372
379
@ Override
373
380
protected void doSetNextReader (LeafReaderContext readerContext ) throws IOException {
381
+ minCompetitiveScore = 0f ;
374
382
docBase = readerContext .docBase ;
375
383
for (int i = 0 ; i < comparators .length ; i ++) {
376
384
leafComparators [i ] = comparators [i ].getLeafComparator (readerContext );
@@ -388,4 +396,25 @@ public GroupSelector<T> getGroupSelector() {
388
396
private boolean isGroupMapFull () {
389
397
return groupMap .size () >= topNGroups ;
390
398
}
399
+
400
+ private void setBottomSlot (final int bottomSlot ) throws IOException {
401
+ for (LeafFieldComparator fc : leafComparators ) {
402
+ fc .setBottom (bottomSlot );
403
+ }
404
+
405
+ this .bottomSlot = bottomSlot ;
406
+ setMinCompetitiveScore (scorer );
407
+ }
408
+
409
+ private void setMinCompetitiveScore (final Scorable scorer ) throws IOException {
410
+ if (canSetMinScore == false || isGroupMapFull () == false ) {
411
+ return ;
412
+ }
413
+
414
+ final float minScore = (float ) comparators [0 ].value (bottomSlot );
415
+ if (minScore > minCompetitiveScore ) {
416
+ scorer .setMinCompetitiveScore (minScore );
417
+ minCompetitiveScore = minScore ;
418
+ }
419
+ }
391
420
}
0 commit comments