54
54
import java .util .List ;
55
55
import java .util .Set ;
56
56
57
+ import static org .hamcrest .core .IsEqual .equalTo ;
58
+
57
59
public class CollapsingTopDocsCollectorTests extends ESTestCase {
58
60
private static class SegmentSearcher extends IndexSearcher {
59
61
private final List <LeafReaderContext > ctx ;
@@ -82,12 +84,15 @@ interface CollapsingDocValuesProducer<T extends Comparable> {
82
84
}
83
85
84
86
<T extends Comparable > void assertSearchCollapse (CollapsingDocValuesProducer <T > dvProducers , boolean numeric ) throws IOException {
85
- assertSearchCollapse (dvProducers , numeric , true );
86
- assertSearchCollapse (dvProducers , numeric , false );
87
+ assertSearchCollapse (dvProducers , numeric , true , true );
88
+ assertSearchCollapse (dvProducers , numeric , true , false );
89
+ assertSearchCollapse (dvProducers , numeric , false , true );
90
+ assertSearchCollapse (dvProducers , numeric , false , false );
87
91
}
88
92
89
93
private <T extends Comparable > void assertSearchCollapse (CollapsingDocValuesProducer <T > dvProducers ,
90
- boolean numeric , boolean multivalued ) throws IOException {
94
+ boolean numeric , boolean multivalued ,
95
+ boolean trackMaxScores ) throws IOException {
91
96
final int numDocs = randomIntBetween (1000 , 2000 );
92
97
int maxGroup = randomIntBetween (2 , 500 );
93
98
final Directory dir = newDirectory ();
@@ -118,14 +123,14 @@ private <T extends Comparable> void assertSearchCollapse(CollapsingDocValuesProd
118
123
final CollapsingTopDocsCollector collapsingCollector ;
119
124
if (numeric ) {
120
125
collapsingCollector =
121
- CollapsingTopDocsCollector .createNumeric (collapseField .getField (), sort , expectedNumGroups , false );
126
+ CollapsingTopDocsCollector .createNumeric (collapseField .getField (), sort , expectedNumGroups , trackMaxScores );
122
127
} else {
123
128
collapsingCollector =
124
- CollapsingTopDocsCollector .createKeyword (collapseField .getField (), sort , expectedNumGroups , false );
129
+ CollapsingTopDocsCollector .createKeyword (collapseField .getField (), sort , expectedNumGroups , trackMaxScores );
125
130
}
126
131
127
132
TopFieldCollector topFieldCollector =
128
- TopFieldCollector .create (sort , totalHits , true , false , false );
133
+ TopFieldCollector .create (sort , totalHits , true , trackMaxScores , trackMaxScores );
129
134
130
135
searcher .search (new MatchAllDocsQuery (), collapsingCollector );
131
136
searcher .search (new MatchAllDocsQuery (), topFieldCollector );
@@ -136,6 +141,11 @@ private <T extends Comparable> void assertSearchCollapse(CollapsingDocValuesProd
136
141
assertEquals (totalHits , collapseTopFieldDocs .totalHits );
137
142
assertEquals (totalHits , topDocs .scoreDocs .length );
138
143
assertEquals (totalHits , topDocs .totalHits );
144
+ if (trackMaxScores ) {
145
+ assertThat (collapseTopFieldDocs .getMaxScore (), equalTo (topDocs .getMaxScore ()));
146
+ } else {
147
+ assertThat (collapseTopFieldDocs .getMaxScore (), equalTo (Float .NaN ));
148
+ }
139
149
140
150
Set <Object > seen = new HashSet <>();
141
151
// collapse field is the last sort
@@ -186,14 +196,14 @@ private <T extends Comparable> void assertSearchCollapse(CollapsingDocValuesProd
186
196
}
187
197
188
198
final CollapseTopFieldDocs [] shardHits = new CollapseTopFieldDocs [subSearchers .length ];
189
- final Weight weight = searcher .createNormalizedWeight (new MatchAllDocsQuery (), false );
199
+ final Weight weight = searcher .createNormalizedWeight (new MatchAllDocsQuery (), true );
190
200
for (int shardIDX = 0 ; shardIDX < subSearchers .length ; shardIDX ++) {
191
201
final SegmentSearcher subSearcher = subSearchers [shardIDX ];
192
202
final CollapsingTopDocsCollector c ;
193
203
if (numeric ) {
194
- c = CollapsingTopDocsCollector .createNumeric (collapseField .getField (), sort , expectedNumGroups , false );
204
+ c = CollapsingTopDocsCollector .createNumeric (collapseField .getField (), sort , expectedNumGroups , trackMaxScores );
195
205
} else {
196
- c = CollapsingTopDocsCollector .createKeyword (collapseField .getField (), sort , expectedNumGroups , false );
206
+ c = CollapsingTopDocsCollector .createKeyword (collapseField .getField (), sort , expectedNumGroups , trackMaxScores );
197
207
}
198
208
subSearcher .search (weight , c );
199
209
shardHits [shardIDX ] = c .getTopDocs ();
0 commit comments