Skip to content

Commit

Permalink
Use the AggTestConfig object in testCase (#90699)
Browse files Browse the repository at this point in the history
  • Loading branch information
not-napoleon committed Oct 6, 2022
1 parent db2882c commit 4a26dda
Show file tree
Hide file tree
Showing 89 changed files with 1,215 additions and 1,201 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
package org.elasticsearch.aggregations.bucket;

import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.elasticsearch.aggregations.AggregationsPlugin;
import org.elasticsearch.common.util.Maps;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
Expand Down Expand Up @@ -44,7 +43,7 @@ public void testTooManyFilters() {
AdjacencyMatrixAggregationBuilder tooBig = new AdjacencyMatrixAggregationBuilder("dummy", filters);
IllegalArgumentException ex = expectThrows(
IllegalArgumentException.class,
() -> testCase(tooBig, new MatchAllDocsQuery(), iw -> {}, r -> {})
() -> testCase(iw -> {}, r -> {}, new AggTestConfig(tooBig))
);
assertThat(
ex.getMessage(),
Expand All @@ -56,18 +55,18 @@ public void testTooManyFilters() {

public void testNoFilters() throws IOException {
AdjacencyMatrixAggregationBuilder aggregationBuilder = new AdjacencyMatrixAggregationBuilder("dummy", Map.of());
testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> iw.addDocument(List.of()), r -> {
testCase(iw -> iw.addDocument(List.of()), r -> {
InternalAdjacencyMatrix result = (InternalAdjacencyMatrix) r;
assertThat(result.getBuckets(), equalTo(List.of()));
});
}, new AggTestConfig(aggregationBuilder));
}

public void testAFewFilters() throws IOException {
AdjacencyMatrixAggregationBuilder aggregationBuilder = new AdjacencyMatrixAggregationBuilder(
"dummy",
Map.of("a", new MatchAllQueryBuilder(), "b", new MatchAllQueryBuilder())
);
testCase(aggregationBuilder, new MatchAllDocsQuery(), iw -> iw.addDocument(List.of()), r -> {
testCase(iw -> iw.addDocument(List.of()), r -> {
InternalAdjacencyMatrix result = (InternalAdjacencyMatrix) r;
assertThat(result.getBuckets(), hasSize(3));
InternalAdjacencyMatrix.InternalBucket a = result.getBucketByKey("a");
Expand All @@ -76,6 +75,6 @@ public void testAFewFilters() throws IOException {
assertThat(a.getDocCount(), equalTo(1L));
assertThat(b.getDocCount(), equalTo(1L));
assertThat(ab.getDocCount(), equalTo(1L));
});
}, new AggTestConfig(aggregationBuilder));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void testNoData() throws Exception {
MatrixStatsAggregationBuilder aggBuilder = new MatrixStatsAggregationBuilder("my_agg").fields(
Collections.singletonList("field")
);
InternalMatrixStats stats = searchAndReduce(new AggTestConfig(searcher, aggBuilder, ft));
InternalMatrixStats stats = searchAndReduce(searcher, new AggTestConfig(aggBuilder, ft));
assertNull(stats.getStats());
assertEquals(0L, stats.getDocCount());
}
Expand All @@ -59,7 +59,7 @@ public void testUnmapped() throws Exception {
MatrixStatsAggregationBuilder aggBuilder = new MatrixStatsAggregationBuilder("my_agg").fields(
Collections.singletonList("bogus")
);
InternalMatrixStats stats = searchAndReduce(new AggTestConfig(searcher, aggBuilder, ft));
InternalMatrixStats stats = searchAndReduce(searcher, new AggTestConfig(aggBuilder, ft));
assertNull(stats.getStats());
assertEquals(0L, stats.getDocCount());
}
Expand Down Expand Up @@ -94,7 +94,7 @@ public void testTwoFields() throws Exception {
MatrixStatsAggregationBuilder aggBuilder = new MatrixStatsAggregationBuilder("my_agg").fields(
Arrays.asList(fieldA, fieldB)
);
InternalMatrixStats stats = searchAndReduce(new AggTestConfig(searcher, aggBuilder, ftA, ftB));
InternalMatrixStats stats = searchAndReduce(searcher, new AggTestConfig(aggBuilder, ftA, ftB));
multiPassStats.assertNearlyEqual(stats);
assertTrue(MatrixAggregationInspectionHelper.hasValue(stats));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,8 @@ private void testCase(Query query, IndexSearcher indexSearcher, Consumer<Interna

MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.LONG);
InternalParent result = searchAndReduce(
new AggTestConfig(indexSearcher, aggregationBuilder, withJoinFields(fieldType)).withQuery(query)
indexSearcher,
new AggTestConfig(aggregationBuilder, withJoinFields(fieldType)).withQuery(query)
);
verify.accept(result);
}
Expand All @@ -281,7 +282,8 @@ private void testCaseTerms(Query query, IndexSearcher indexSearcher, Consumer<In

MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.LONG);
InternalParent result = searchAndReduce(
new AggTestConfig(indexSearcher, aggregationBuilder, withJoinFields(fieldType)).withQuery(query)
indexSearcher,
new AggTestConfig(aggregationBuilder, withJoinFields(fieldType)).withQuery(query)
);
verify.accept(result);
}
Expand All @@ -298,7 +300,8 @@ private void testCaseTermsParentTerms(Query query, IndexSearcher indexSearcher,
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.LONG);
MappedFieldType subFieldType = new NumberFieldMapper.NumberFieldType("subNumber", NumberFieldMapper.NumberType.LONG);
LongTerms result = searchAndReduce(
new AggTestConfig(indexSearcher, aggregationBuilder, withJoinFields(fieldType, subFieldType)).withQuery(query)
indexSearcher,
new AggTestConfig(aggregationBuilder, withJoinFields(fieldType, subFieldType)).withQuery(query)
);
verify.accept(result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public void testParentChildAsSubAgg() throws IOException {
expectedOddMin = Math.min(expectedOddMin, e.getValue().v2());
}
}
StringTerms result = searchAndReduce(new AggTestConfig(indexSearcher, request, withJoinFields(longField("number"), kwd)));
StringTerms result = searchAndReduce(indexSearcher, new AggTestConfig(request, withJoinFields(longField("number"), kwd)));

StringTerms.Bucket evenBucket = result.getBucketByKey("even");
InternalChildren evenChildren = evenBucket.getAggregations().get("children");
Expand Down Expand Up @@ -201,14 +201,14 @@ public void testBestDeferringCollectorWithSubAggOfChildrenAggNeedingScores() thr

var fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.LONG);
var fieldType2 = new KeywordFieldMapper.KeywordFieldType("string_field", false, true, Map.of());
var e = expectThrows(
RuntimeException.class,
() -> searchAndReduce(
new AggTestConfig(indexSearcher, aggregationBuilder, withJoinFields(fieldType, fieldType2)).withQuery(
var e = expectThrows(RuntimeException.class, () -> {
searchAndReduce(
indexSearcher,
new AggTestConfig(aggregationBuilder, withJoinFields(fieldType, fieldType2)).withQuery(
new TermQuery(new Term("join_field", "parent_type"))
)
)
);
);
});
assertThat(
e.getMessage(),
containsString(
Expand All @@ -233,7 +233,8 @@ public void testBestDeferringCollectorWithSubAggOfChildrenAggNeedingScores() thr
var fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.LONG);
var fieldType2 = new KeywordFieldMapper.KeywordFieldType("string_field", false, true, Map.of());
InternalChildren result = searchAndReduce(
new AggTestConfig(indexSearcher, aggregationBuilder, withJoinFields(fieldType, fieldType2)).withQuery(
indexSearcher,
new AggTestConfig(aggregationBuilder, withJoinFields(fieldType, fieldType2)).withQuery(
new TermQuery(new Term("join_field", "parent_type"))
)
);
Expand Down Expand Up @@ -306,7 +307,8 @@ private void testCase(Query query, IndexSearcher indexSearcher, Consumer<Interna

MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.LONG);
InternalChildren result = searchAndReduce(
new AggTestConfig(indexSearcher, aggregationBuilder, withJoinFields(fieldType)).withQuery(query)
indexSearcher,
new AggTestConfig(aggregationBuilder, withJoinFields(fieldType)).withQuery(query)
);
verify.accept(result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,6 @@ private void testAggregation(Query query, CheckedConsumer<RandomIndexWriter, IOE
AggregationBuilder builder = new FilterAggregationBuilder("f", new MatchAllQueryBuilder());
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NUMBER_FIELD, NumberFieldMapper.NumberType.LONG);
MappedFieldType docCountFieldType = new DocCountFieldMapper.DocCountFieldType();
testCase(builder, query, indexer, verify, fieldType, docCountFieldType);
testCase(indexer, verify, new AggTestConfig(builder, fieldType, docCountFieldType).withQuery(query));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ private void testCase(
aggregationBuilder.subAggregation(new MinAggregationBuilder("in_global").field("number"));
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("number", NumberFieldMapper.NumberType.LONG);

testCase(aggregationBuilder, topLevelQuery, buildIndex, (InternalGlobal result) -> {
testCase(buildIndex, (InternalGlobal result) -> {
Min min = result.getAggregations().get("in_global");
verify.accept(result, min);
}, fieldType);
}, new AggTestConfig(aggregationBuilder, fieldType).withQuery(topLevelQuery));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ public void testUsingTestCase() throws Exception {
createDocument("keyword", "c")
)
);
testCase(new CompositeAggregationBuilder("name", Collections.singletonList(terms)), new MatchAllDocsQuery(), iw -> {
testCase(iw -> {
Document document = new Document();
int id = 0;
for (Map<String, List<Object>> fields : dataset) {
Expand All @@ -746,7 +746,7 @@ public void testUsingTestCase() throws Exception {
assertEquals(2L, result.getBuckets().get(1).getDocCount());
assertEquals("{keyword=d}", result.getBuckets().get(2).getKeyAsString());
assertEquals(1L, result.getBuckets().get(2).getDocCount());
}, FIELD_TYPES);
}, new AggTestConfig(new CompositeAggregationBuilder("name", Collections.singletonList(terms)), FIELD_TYPES));
}

/**
Expand All @@ -762,7 +762,7 @@ public void testSubAggregationOfNested() throws Exception {
NestedAggregationBuilder builder = new NestedAggregationBuilder("nestedAggName", nestedPath);
builder.subAggregation(new CompositeAggregationBuilder("compositeAggName", Collections.singletonList(terms)));
// Without after
testCase(builder, new MatchAllDocsQuery(), iw -> {
testCase(iw -> {
// Sub-Docs
List<Iterable<IndexableField>> documents = new ArrayList<>();
documents.add(createNestedDocument("1", nestedPath, leafNameField, "Pens and Stuff", "price", 10L));
Expand Down Expand Up @@ -795,8 +795,11 @@ public void testSubAggregationOfNested() throws Exception {
assertEquals("{keyword=Stationary}", result.getBuckets().get(2).getKeyAsString());
assertEquals(1L, result.getBuckets().get(2).getDocCount());
},
new KeywordFieldMapper.KeywordFieldType(nestedPath + "." + leafNameField),
new NumberFieldMapper.NumberFieldType("price", NumberFieldMapper.NumberType.LONG)
new AggTestConfig(
builder,
new KeywordFieldMapper.KeywordFieldType(nestedPath + "." + leafNameField),
new NumberFieldMapper.NumberFieldType("price", NumberFieldMapper.NumberType.LONG)
)
);
}

Expand All @@ -816,7 +819,9 @@ public void testSubAggregationOfNestedAggregateAfter() throws Exception {
createAfterKey("keyword", "Pens and Stuff")
)
);
testCase(builder, new MatchAllDocsQuery(), iw -> {
// Sub-Docs
// Root docs
testCase(iw -> {
// Sub-Docs
List<Iterable<IndexableField>> documents = new ArrayList<>();
documents.add(createNestedDocument("1", nestedPath, leafNameField, "Pens and Stuff", "price", 10L));
Expand Down Expand Up @@ -845,8 +850,11 @@ public void testSubAggregationOfNestedAggregateAfter() throws Exception {
assertEquals("{keyword=Stationary}", result.getBuckets().get(0).getKeyAsString());
assertEquals(1L, result.getBuckets().get(0).getDocCount());
},
new KeywordFieldMapper.KeywordFieldType(nestedPath + "." + leafNameField),
new NumberFieldMapper.NumberFieldType("price", NumberFieldMapper.NumberType.LONG)
new AggTestConfig(
builder,
new KeywordFieldMapper.KeywordFieldType(nestedPath + "." + leafNameField),
new NumberFieldMapper.NumberFieldType("price", NumberFieldMapper.NumberType.LONG)
)
);
}

Expand Down Expand Up @@ -3336,7 +3344,7 @@ private <T extends AggregationBuilder, V extends InternalAggregation> void execu
IndexSearcher indexSearcher = new IndexSearcher(indexReader);
for (int i = 0; i < create.size(); i++) {
verify.get(i)
.accept(searchAndReduce(new AggTestConfig(indexSearcher, create.get(i).get(), FIELD_TYPES).withQuery(query)));
.accept(searchAndReduce(indexSearcher, new AggTestConfig(create.get(i).get(), FIELD_TYPES).withQuery(query)));
}
}
}
Expand Down

0 comments on commit 4a26dda

Please sign in to comment.