Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parameter object to Aggregator Test Case #90320

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(new AggTestConfig<>(searcher, 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(new AggTestConfig<>(searcher, 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(new AggTestConfig<>(searcher, aggBuilder, ftA, ftB));
multiPassStats.assertNearlyEqual(stats);
assertTrue(MatrixAggregationInspectionHelper.hasValue(stats));
}
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(new AggTestConfig<>(builder, verify, fieldType, docCountFieldType).withQuery(query).withIndexBuilder(indexer));
}
}
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(new AggTestConfig<InternalGlobal>(aggregationBuilder, result -> {
Min min = result.getAggregations().get("in_global");
verify.accept(result, min);
}, fieldType);
}, fieldType).withQuery(topLevelQuery).withIndexBuilder(buildIndex));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
package org.elasticsearch.search.aggregations.bucket.adjacency;

import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.elasticsearch.common.util.Maps;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
Expand All @@ -35,7 +34,7 @@ public void testTooManyFilters() {
AdjacencyMatrixAggregationBuilder tooBig = new AdjacencyMatrixAggregationBuilder("dummy", filters);
IllegalArgumentException ex = expectThrows(
IllegalArgumentException.class,
() -> testCase(tooBig, new MatchAllDocsQuery(), iw -> {}, r -> {})
() -> testCase(new AggTestConfig<>(tooBig, r -> {}).withEmptyIndex())
);
assertThat(
ex.getMessage(),
Expand All @@ -47,26 +46,27 @@ 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 -> {
InternalAdjacencyMatrix result = (InternalAdjacencyMatrix) r;
assertThat(result.getBuckets(), equalTo(List.of()));
});
testCase(
new AggTestConfig<InternalAdjacencyMatrix>(
aggregationBuilder,
result -> assertThat(result.getBuckets(), equalTo(List.of()))
).withIndexBuilder(iw -> iw.addDocument(List.of()))
);
}

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 -> {
InternalAdjacencyMatrix result = (InternalAdjacencyMatrix) r;
testCase(new AggTestConfig<InternalAdjacencyMatrix>(aggregationBuilder, result -> {
assertThat(result.getBuckets(), hasSize(3));
InternalAdjacencyMatrix.InternalBucket a = result.getBucketByKey("a");
InternalAdjacencyMatrix.InternalBucket b = result.getBucketByKey("b");
InternalAdjacencyMatrix.InternalBucket ab = result.getBucketByKey("a&b");
assertThat(a.getDocCount(), equalTo(1L));
assertThat(b.getDocCount(), equalTo(1L));
assertThat(ab.getDocCount(), equalTo(1L));
});
}).withIndexBuilder(iw -> iw.addDocument(List.of())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -728,16 +728,7 @@ public void testUsingTestCase() throws Exception {
createDocument("keyword", "c")
)
);
testCase(new CompositeAggregationBuilder("name", Collections.singletonList(terms)), new MatchAllDocsQuery(), iw -> {
Document document = new Document();
int id = 0;
for (Map<String, List<Object>> fields : dataset) {
document.clear();
addToDocument(id, document, fields);
iw.addDocument(document);
id++;
}
}, (InternalComposite result) -> {
testCase(new AggTestConfig<InternalComposite>(new CompositeAggregationBuilder("name", Collections.singletonList(terms)), result -> {
assertEquals(3, result.getBuckets().size());
assertEquals("{keyword=d}", result.afterKey().toString());
assertEquals("{keyword=a}", result.getBuckets().get(0).getKeyAsString());
Expand All @@ -746,7 +737,18 @@ 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);
}, FIELD_TYPES).withIndexBuilder(
iw -> {
Document document = new Document();
int id = 0;
for (Map<String, List<Object>> fields : dataset) {
document.clear();
addToDocument(id, document, fields);
iw.addDocument(document);
id++;
}
}
));
}

/**
Expand All @@ -762,28 +764,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 -> {
// Sub-Docs
List<Iterable<IndexableField>> documents = new ArrayList<>();
documents.add(createNestedDocument("1", nestedPath, leafNameField, "Pens and Stuff", "price", 10L));
documents.add(createNestedDocument("1", nestedPath, leafNameField, "Pen World", "price", 9L));
documents.add(createNestedDocument("2", nestedPath, leafNameField, "Pens and Stuff", "price", 5L));
documents.add(createNestedDocument("2", nestedPath, leafNameField, "Stationary", "price", 7L));
// Root docs
LuceneDocument root;
root = new LuceneDocument();
root.add(new Field(IdFieldMapper.NAME, Uid.encodeId("1"), ProvidedIdFieldMapper.Defaults.FIELD_TYPE));
sequenceIDFields.addFields(root);
root.add(new StringField(rootNameField, new BytesRef("Ballpoint"), Field.Store.NO));
documents.add(root);

root = new LuceneDocument();
root.add(new Field(IdFieldMapper.NAME, Uid.encodeId("2"), ProvidedIdFieldMapper.Defaults.FIELD_TYPE));
root.add(new StringField(rootNameField, new BytesRef("Notebook"), Field.Store.NO));
sequenceIDFields.addFields(root);
documents.add(root);
iw.addDocuments(documents);
}, (InternalSingleBucketAggregation parent) -> {
testCase(new AggTestConfig<InternalSingleBucketAggregation>(builder, parent -> {
assertEquals(1, parent.getAggregations().asList().size());
InternalComposite result = (InternalComposite) parent.getProperty("compositeAggName");
assertEquals(3, result.getBuckets().size());
Expand All @@ -797,7 +778,30 @@ public void testSubAggregationOfNested() throws Exception {
},
new KeywordFieldMapper.KeywordFieldType(nestedPath + "." + leafNameField),
new NumberFieldMapper.NumberFieldType("price", NumberFieldMapper.NumberType.LONG)
);
).withIndexBuilder(
iw -> {
// Sub-Docs
List<Iterable<IndexableField>> documents = new ArrayList<>();
documents.add(createNestedDocument("1", nestedPath, leafNameField, "Pens and Stuff", "price", 10L));
documents.add(createNestedDocument("1", nestedPath, leafNameField, "Pen World", "price", 9L));
documents.add(createNestedDocument("2", nestedPath, leafNameField, "Pens and Stuff", "price", 5L));
documents.add(createNestedDocument("2", nestedPath, leafNameField, "Stationary", "price", 7L));
// Root docs
LuceneDocument root;
root = new LuceneDocument();
root.add(new Field(IdFieldMapper.NAME, Uid.encodeId("1"), ProvidedIdFieldMapper.Defaults.FIELD_TYPE));
sequenceIDFields.addFields(root);
root.add(new StringField(rootNameField, new BytesRef("Ballpoint"), Field.Store.NO));
documents.add(root);

root = new LuceneDocument();
root.add(new Field(IdFieldMapper.NAME, Uid.encodeId("2"), ProvidedIdFieldMapper.Defaults.FIELD_TYPE));
root.add(new StringField(rootNameField, new BytesRef("Notebook"), Field.Store.NO));
sequenceIDFields.addFields(root);
documents.add(root);
iw.addDocuments(documents);
}
));
}

/**
Expand All @@ -816,28 +820,7 @@ public void testSubAggregationOfNestedAggregateAfter() throws Exception {
createAfterKey("keyword", "Pens and Stuff")
)
);
testCase(builder, new MatchAllDocsQuery(), iw -> {
// Sub-Docs
List<Iterable<IndexableField>> documents = new ArrayList<>();
documents.add(createNestedDocument("1", nestedPath, leafNameField, "Pens and Stuff", "price", 10L));
documents.add(createNestedDocument("1", nestedPath, leafNameField, "Pen World", "price", 9L));
documents.add(createNestedDocument("2", nestedPath, leafNameField, "Pens and Stuff", "price", 5L));
documents.add(createNestedDocument("2", nestedPath, leafNameField, "Stationary", "price", 7L));
// Root docs
LuceneDocument root;
root = new LuceneDocument();
root.add(new Field(IdFieldMapper.NAME, Uid.encodeId("1"), ProvidedIdFieldMapper.Defaults.FIELD_TYPE));
sequenceIDFields.addFields(root);
root.add(new StringField(rootNameField, new BytesRef("Ballpoint"), Field.Store.NO));
documents.add(root);

root = new LuceneDocument();
root.add(new Field(IdFieldMapper.NAME, Uid.encodeId("2"), ProvidedIdFieldMapper.Defaults.FIELD_TYPE));
root.add(new StringField(rootNameField, new BytesRef("Notebook"), Field.Store.NO));
sequenceIDFields.addFields(root);
documents.add(root);
iw.addDocuments(documents);
}, (InternalSingleBucketAggregation parent) -> {
testCase(new AggTestConfig<InternalSingleBucketAggregation>(builder, parent -> {
assertEquals(1, parent.getAggregations().asList().size());
InternalComposite result = (InternalComposite) parent.getProperty("compositeAggName");
assertEquals(1, result.getBuckets().size());
Expand All @@ -847,7 +830,30 @@ public void testSubAggregationOfNestedAggregateAfter() throws Exception {
},
new KeywordFieldMapper.KeywordFieldType(nestedPath + "." + leafNameField),
new NumberFieldMapper.NumberFieldType("price", NumberFieldMapper.NumberType.LONG)
);
).withIndexBuilder(
iw -> {
// Sub-Docs
List<Iterable<IndexableField>> documents = new ArrayList<>();
documents.add(createNestedDocument("1", nestedPath, leafNameField, "Pens and Stuff", "price", 10L));
documents.add(createNestedDocument("1", nestedPath, leafNameField, "Pen World", "price", 9L));
documents.add(createNestedDocument("2", nestedPath, leafNameField, "Pens and Stuff", "price", 5L));
documents.add(createNestedDocument("2", nestedPath, leafNameField, "Stationary", "price", 7L));
// Root docs
LuceneDocument root;
root = new LuceneDocument();
root.add(new Field(IdFieldMapper.NAME, Uid.encodeId("1"), ProvidedIdFieldMapper.Defaults.FIELD_TYPE));
sequenceIDFields.addFields(root);
root.add(new StringField(rootNameField, new BytesRef("Ballpoint"), Field.Store.NO));
documents.add(root);

root = new LuceneDocument();
root.add(new Field(IdFieldMapper.NAME, Uid.encodeId("2"), ProvidedIdFieldMapper.Defaults.FIELD_TYPE));
root.add(new StringField(rootNameField, new BytesRef("Notebook"), Field.Store.NO));
sequenceIDFields.addFields(root);
documents.add(root);
iw.addDocuments(documents);
}
));
}

public void testWithKeywordAndMissingBucket() throws Exception {
Expand Down Expand Up @@ -3335,7 +3341,7 @@ private <T extends AggregationBuilder, V extends InternalAggregation> void execu
try (IndexReader indexReader = DirectoryReader.open(directory)) {
IndexSearcher indexSearcher = new IndexSearcher(indexReader);
for (int i = 0; i < create.size(); i++) {
verify.get(i).accept(searchAndReduce(new AggTestConfig(indexSearcher, query, create.get(i).get(), FIELD_TYPES)));
verify.get(i).accept(searchAndReduce(new AggTestConfig<>(indexSearcher, query, create.get(i).get(), FIELD_TYPES)));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void testEmpty() throws Exception {
IndexSearcher indexSearcher = newSearcher(indexReader, true, true);
QueryBuilder filter = QueryBuilders.termQuery("field", randomAlphaOfLength(5));
FilterAggregationBuilder builder = new FilterAggregationBuilder("test", filter);
InternalFilter response = searchAndReduce(new AggTestConfig(indexSearcher, builder, fieldType));
InternalFilter response = searchAndReduce(new AggTestConfig<>(indexSearcher, builder, fieldType));
assertEquals(response.getDocCount(), 0);
assertFalse(AggregationInspectionHelper.hasValue(response));
indexReader.close();
Expand Down Expand Up @@ -86,7 +86,7 @@ public void testRandom() throws Exception {
QueryBuilder filter = QueryBuilders.termQuery("field", Integer.toString(value));
FilterAggregationBuilder builder = new FilterAggregationBuilder("test", filter);

final InternalFilter response = searchAndReduce(new AggTestConfig(indexSearcher, builder, fieldType));
final InternalFilter response = searchAndReduce(new AggTestConfig<>(indexSearcher, builder, fieldType));
assertEquals(response.getDocCount(), (long) expectedBucketCount[value]);
if (expectedBucketCount[value] > 0) {
assertTrue(AggregationInspectionHelper.hasValue(response));
Expand Down
Loading