Skip to content

Commit 15932d5

Browse files
authored
Refactor aggregator test case (#90149)
Refactor `AggregatorTestCase` to eliminate many overloads of `searchAndReduce`. Introduce a parameter object, and default many common arguments.
1 parent b9f20e9 commit 15932d5

File tree

54 files changed

+355
-518
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+355
-518
lines changed

modules/aggs-matrix-stats/src/test/java/org/elasticsearch/search/aggregations/matrix/stats/MatrixStatsAggregatorTests.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import org.apache.lucene.document.StringField;
1414
import org.apache.lucene.index.IndexReader;
1515
import org.apache.lucene.search.IndexSearcher;
16-
import org.apache.lucene.search.MatchAllDocsQuery;
1716
import org.apache.lucene.store.Directory;
1817
import org.apache.lucene.tests.index.RandomIndexWriter;
1918
import org.apache.lucene.util.NumericUtils;
@@ -41,7 +40,7 @@ public void testNoData() throws Exception {
4140
MatrixStatsAggregationBuilder aggBuilder = new MatrixStatsAggregationBuilder("my_agg").fields(
4241
Collections.singletonList("field")
4342
);
44-
InternalMatrixStats stats = searchAndReduce(searcher, new MatchAllDocsQuery(), aggBuilder, ft);
43+
InternalMatrixStats stats = searchAndReduce(new AggTestConfig(searcher, aggBuilder, ft));
4544
assertNull(stats.getStats());
4645
assertEquals(0L, stats.getDocCount());
4746
}
@@ -60,7 +59,7 @@ public void testUnmapped() throws Exception {
6059
MatrixStatsAggregationBuilder aggBuilder = new MatrixStatsAggregationBuilder("my_agg").fields(
6160
Collections.singletonList("bogus")
6261
);
63-
InternalMatrixStats stats = searchAndReduce(searcher, new MatchAllDocsQuery(), aggBuilder, ft);
62+
InternalMatrixStats stats = searchAndReduce(new AggTestConfig(searcher, aggBuilder, ft));
6463
assertNull(stats.getStats());
6564
assertEquals(0L, stats.getDocCount());
6665
}
@@ -95,7 +94,7 @@ public void testTwoFields() throws Exception {
9594
MatrixStatsAggregationBuilder aggBuilder = new MatrixStatsAggregationBuilder("my_agg").fields(
9695
Arrays.asList(fieldA, fieldB)
9796
);
98-
InternalMatrixStats stats = searchAndReduce(searcher, new MatchAllDocsQuery(), aggBuilder, ftA, ftB);
97+
InternalMatrixStats stats = searchAndReduce(new AggTestConfig(searcher, aggBuilder, ftA, ftB));
9998
multiPassStats.assertNearlyEqual(stats);
10099
assertTrue(MatrixAggregationInspectionHelper.hasValue(stats));
101100
}

modules/parent-join/src/test/java/org/elasticsearch/join/aggregations/ChildrenToParentAggregatorTests.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ private void testCase(Query query, IndexSearcher indexSearcher, Consumer<Interna
268268
aggregationBuilder.subAggregation(new MinAggregationBuilder("in_parent").field("number"));
269269

270270
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.LONG);
271-
InternalParent result = searchAndReduce(indexSearcher, query, aggregationBuilder, withJoinFields(fieldType));
271+
InternalParent result = searchAndReduce(new AggTestConfig(indexSearcher, query, aggregationBuilder, withJoinFields(fieldType)));
272272
verify.accept(result);
273273
}
274274

@@ -278,7 +278,7 @@ private void testCaseTerms(Query query, IndexSearcher indexSearcher, Consumer<In
278278
aggregationBuilder.subAggregation(new TermsAggregationBuilder("value_terms").field("number"));
279279

280280
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.LONG);
281-
InternalParent result = searchAndReduce(indexSearcher, query, aggregationBuilder, withJoinFields(fieldType));
281+
InternalParent result = searchAndReduce(new AggTestConfig(indexSearcher, query, aggregationBuilder, withJoinFields(fieldType)));
282282
verify.accept(result);
283283
}
284284

@@ -293,7 +293,9 @@ private void testCaseTermsParentTerms(Query query, IndexSearcher indexSearcher,
293293

294294
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.LONG);
295295
MappedFieldType subFieldType = new NumberFieldMapper.NumberFieldType("subNumber", NumberFieldMapper.NumberType.LONG);
296-
LongTerms result = searchAndReduce(indexSearcher, query, aggregationBuilder, withJoinFields(fieldType, subFieldType));
296+
LongTerms result = searchAndReduce(
297+
new AggTestConfig(indexSearcher, query, aggregationBuilder, withJoinFields(fieldType, subFieldType))
298+
);
297299
verify.accept(result);
298300
}
299301

modules/parent-join/src/test/java/org/elasticsearch/join/aggregations/ParentToChildrenAggregatorTests.java

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,7 @@ public void testParentChildAsSubAgg() throws IOException {
156156
expectedOddMin = Math.min(expectedOddMin, e.getValue().v2());
157157
}
158158
}
159-
StringTerms result = searchAndReduce(
160-
indexSearcher,
161-
new MatchAllDocsQuery(),
162-
request,
163-
withJoinFields(longField("number"), kwd)
164-
);
159+
StringTerms result = searchAndReduce(new AggTestConfig(indexSearcher, request, withJoinFields(longField("number"), kwd)));
165160

166161
StringTerms.Bucket evenBucket = result.getBucketByKey("even");
167162
InternalChildren evenChildren = evenBucket.getAggregations().get("children");
@@ -209,10 +204,12 @@ public void testBestDeferringCollectorWithSubAggOfChildrenAggNeedingScores() thr
209204
var e = expectThrows(
210205
RuntimeException.class,
211206
() -> searchAndReduce(
212-
indexSearcher,
213-
new TermQuery(new Term("join_field", "parent_type")),
214-
aggregationBuilder,
215-
withJoinFields(fieldType, fieldType2)
207+
new AggTestConfig(
208+
indexSearcher,
209+
new TermQuery(new Term("join_field", "parent_type")),
210+
aggregationBuilder,
211+
withJoinFields(fieldType, fieldType2)
212+
)
216213
)
217214
);
218215
assertThat(
@@ -239,10 +236,12 @@ public void testBestDeferringCollectorWithSubAggOfChildrenAggNeedingScores() thr
239236
var fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.LONG);
240237
var fieldType2 = new KeywordFieldMapper.KeywordFieldType("string_field", false, true, Map.of());
241238
InternalChildren result = searchAndReduce(
242-
indexSearcher,
243-
new TermQuery(new Term("join_field", "parent_type")),
244-
aggregationBuilder,
245-
withJoinFields(fieldType, fieldType2)
239+
new AggTestConfig(
240+
indexSearcher,
241+
new TermQuery(new Term("join_field", "parent_type")),
242+
aggregationBuilder,
243+
withJoinFields(fieldType, fieldType2)
244+
)
246245
);
247246

248247
Terms terms = result.getAggregations().get("_name2");
@@ -312,7 +311,7 @@ private void testCase(Query query, IndexSearcher indexSearcher, Consumer<Interna
312311
aggregationBuilder.subAggregation(new MinAggregationBuilder("in_child").field("number"));
313312

314313
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.LONG);
315-
InternalChildren result = searchAndReduce(indexSearcher, query, aggregationBuilder, withJoinFields(fieldType));
314+
InternalChildren result = searchAndReduce(new AggTestConfig(indexSearcher, query, aggregationBuilder, withJoinFields(fieldType)));
316315
verify.accept(result);
317316
}
318317

server/src/test/java/org/elasticsearch/search/aggregations/bucket/composite/CompositeAggregatorTests.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
public class CompositeAggregatorTests extends AggregatorTestCase {
126126
private static MappedFieldType[] FIELD_TYPES;
127127
private List<ObjectMapper> objectMappers;
128+
private Sort indexSort;
128129

129130
@Override
130131
@Before
@@ -3295,8 +3296,7 @@ private <T extends AggregationBuilder, V extends InternalAggregation> void execu
32953296
assert create.size() == verify.size() : "create and verify should be the same size";
32963297
Map<String, MappedFieldType> types = Arrays.stream(FIELD_TYPES)
32973298
.collect(Collectors.toMap(MappedFieldType::name, Function.identity()));
3298-
Sort indexSort = useIndexSort ? buildIndexSort(sources, types) : null;
3299-
IndexSettings indexSettings = createIndexSettings(indexSort);
3299+
indexSort = useIndexSort ? buildIndexSort(sources, types) : null;
33003300
try (Directory directory = newDirectory()) {
33013301
IndexWriterConfig config = newIndexWriterConfig(random(), new MockAnalyzer(random()));
33023302
if (indexSort != null) {
@@ -3335,17 +3335,18 @@ private <T extends AggregationBuilder, V extends InternalAggregation> void execu
33353335
try (IndexReader indexReader = DirectoryReader.open(directory)) {
33363336
IndexSearcher indexSearcher = new IndexSearcher(indexReader);
33373337
for (int i = 0; i < create.size(); i++) {
3338-
verify.get(i).accept(searchAndReduce(indexSettings, indexSearcher, query, create.get(i).get(), FIELD_TYPES));
3338+
verify.get(i).accept(searchAndReduce(new AggTestConfig(indexSearcher, query, create.get(i).get(), FIELD_TYPES)));
33393339
}
33403340
}
33413341
}
33423342
}
33433343

3344-
private static IndexSettings createIndexSettings(Sort sort) {
3344+
@Override
3345+
protected IndexSettings createIndexSettings() {
33453346
Settings.Builder builder = Settings.builder();
3346-
if (sort != null) {
3347-
String[] fields = Arrays.stream(sort.getSort()).map(SortField::getField).toArray(String[]::new);
3348-
String[] orders = Arrays.stream(sort.getSort()).map((o) -> o.getReverse() ? "desc" : "asc").toArray(String[]::new);
3347+
if (indexSort != null) {
3348+
String[] fields = Arrays.stream(indexSort.getSort()).map(SortField::getField).toArray(String[]::new);
3349+
String[] orders = Arrays.stream(indexSort.getSort()).map((o) -> o.getReverse() ? "desc" : "asc").toArray(String[]::new);
33493350
builder.putList("index.sort.field", fields);
33503351
builder.putList("index.sort.order", orders);
33513352
}

server/src/test/java/org/elasticsearch/search/aggregations/bucket/filter/FilterAggregatorTests.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import org.apache.lucene.index.DirectoryReader;
1313
import org.apache.lucene.index.IndexReader;
1414
import org.apache.lucene.search.IndexSearcher;
15-
import org.apache.lucene.search.MatchAllDocsQuery;
1615
import org.apache.lucene.store.Directory;
1716
import org.apache.lucene.tests.index.RandomIndexWriter;
1817
import org.elasticsearch.index.mapper.KeywordFieldMapper;
@@ -52,7 +51,7 @@ public void testEmpty() throws Exception {
5251
IndexSearcher indexSearcher = newSearcher(indexReader, true, true);
5352
QueryBuilder filter = QueryBuilders.termQuery("field", randomAlphaOfLength(5));
5453
FilterAggregationBuilder builder = new FilterAggregationBuilder("test", filter);
55-
InternalFilter response = searchAndReduce(indexSearcher, new MatchAllDocsQuery(), builder, fieldType);
54+
InternalFilter response = searchAndReduce(new AggTestConfig(indexSearcher, builder, fieldType));
5655
assertEquals(response.getDocCount(), 0);
5756
assertFalse(AggregationInspectionHelper.hasValue(response));
5857
indexReader.close();
@@ -87,7 +86,7 @@ public void testRandom() throws Exception {
8786
QueryBuilder filter = QueryBuilders.termQuery("field", Integer.toString(value));
8887
FilterAggregationBuilder builder = new FilterAggregationBuilder("test", filter);
8988

90-
final InternalFilter response = searchAndReduce(indexSearcher, new MatchAllDocsQuery(), builder, fieldType);
89+
final InternalFilter response = searchAndReduce(new AggTestConfig(indexSearcher, builder, fieldType));
9190
assertEquals(response.getDocCount(), (long) expectedBucketCount[value]);
9291
if (expectedBucketCount[value] > 0) {
9392
assertTrue(AggregationInspectionHelper.hasValue(response));

server/src/test/java/org/elasticsearch/search/aggregations/bucket/filter/FiltersAggregatorTests.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,7 @@ public void testEmpty() throws Exception {
120120
FiltersAggregationBuilder builder = new FiltersAggregationBuilder("test", filters);
121121
builder.otherBucketKey("other");
122122
InternalFilters response = searchAndReduce(
123-
indexSearcher,
124-
new MatchAllDocsQuery(),
125-
builder,
126-
new KeywordFieldMapper.KeywordFieldType("field")
123+
new AggTestConfig(indexSearcher, new MatchAllDocsQuery(), builder, new KeywordFieldType("field"))
127124
);
128125
assertEquals(response.getBuckets().size(), numFilters);
129126
for (InternalFilters.InternalBucket filter : response.getBuckets()) {
@@ -221,10 +218,7 @@ public void testKeyedFilter() throws Exception {
221218
builder.otherBucket(true);
222219
builder.otherBucketKey("other");
223220
final InternalFilters filters = searchAndReduce(
224-
indexSearcher,
225-
new MatchAllDocsQuery(),
226-
builder,
227-
new KeywordFieldMapper.KeywordFieldType("field")
221+
new AggTestConfig(indexSearcher, new MatchAllDocsQuery(), builder, new KeywordFieldType("field"))
228222
);
229223
assertEquals(filters.getBuckets().size(), 7);
230224
assertEquals(filters.getBucketByKey("foobar").getDocCount(), 2);
@@ -281,10 +275,7 @@ public void testRandom() throws Exception {
281275
builder.otherBucketKey("other");
282276

283277
final InternalFilters response = searchAndReduce(
284-
indexSearcher,
285-
new MatchAllDocsQuery(),
286-
builder,
287-
new KeywordFieldMapper.KeywordFieldType("field")
278+
new AggTestConfig(indexSearcher, new MatchAllDocsQuery(), builder, new KeywordFieldType("field"))
288279
);
289280
List<InternalFilters.InternalBucket> buckets = response.getBuckets();
290281
assertEquals(buckets.size(), filters.length + 1);

server/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/AutoDateHistogramAggregatorTests.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -942,12 +942,7 @@ private void testSearchCase(
942942
MappedFieldType numericFieldType = new NumberFieldMapper.NumberFieldType(NUMERIC_FIELD, NumberFieldMapper.NumberType.LONG);
943943

944944
final InternalAutoDateHistogram histogram = searchAndReduce(
945-
indexSearcher,
946-
query,
947-
aggregationBuilder,
948-
fieldType,
949-
instantFieldType,
950-
numericFieldType
945+
new AggTestConfig(indexSearcher, query, aggregationBuilder, fieldType, instantFieldType, numericFieldType)
951946
);
952947
verify.accept(histogram);
953948
}

server/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/DateHistogramAggregatorTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1174,7 +1174,9 @@ private void testSearchCase(
11741174
configure.accept(aggregationBuilder);
11751175
}
11761176

1177-
InternalDateHistogram histogram = searchAndReduce(indexSearcher, query, aggregationBuilder, maxBucket, fieldType);
1177+
InternalDateHistogram histogram = searchAndReduce(
1178+
new AggTestConfig(indexSearcher, query, aggregationBuilder, fieldType).withMaxBuckets(maxBucket)
1179+
);
11781180
verify.accept(histogram);
11791181
}
11801182
}

server/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/DateRangeHistogramAggregatorTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1084,7 +1084,7 @@ private void testCase(
10841084
try (IndexReader indexReader = DirectoryReader.open(directory)) {
10851085
IndexSearcher indexSearcher = newSearcher(indexReader, true, true);
10861086

1087-
InternalDateHistogram histogram = searchAndReduce(indexSearcher, query, aggregationBuilder, fieldType);
1087+
InternalDateHistogram histogram = searchAndReduce(new AggTestConfig(indexSearcher, query, aggregationBuilder, fieldType));
10881088
verify.accept(histogram);
10891089
}
10901090
}

0 commit comments

Comments
 (0)