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

Update CompositeAggregationBuilder.java #36278

Closed
wants to merge 2 commits into from
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 @@ -29,6 +29,8 @@
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregatorFactories;
import org.elasticsearch.search.aggregations.AggregatorFactory;
import org.elasticsearch.search.aggregations.bucket.nested.NestedAggregationBuilder;
import org.elasticsearch.search.aggregations.bucket.nested.ReverseNestedAggregatorFactory;
import org.elasticsearch.search.internal.SearchContext;

import java.io.IOException;
Expand Down Expand Up @@ -154,7 +156,7 @@ public int size() {
@Override
protected AggregatorFactory<?> doBuild(SearchContext context, AggregatorFactory<?> parent,
AggregatorFactories.Builder subfactoriesBuilder) throws IOException {
if (parent != null) {
if(!checkParentIsNullOrNested(parent)) {
throw new IllegalArgumentException("[composite] aggregation cannot be used with a parent aggregation");
}
CompositeValuesSourceConfig[] configs = new CompositeValuesSourceConfig[sources.size()];
Expand Down Expand Up @@ -222,4 +224,14 @@ protected boolean doEquals(Object obj) {
Objects.equals(sources, other.sources) &&
Objects.equals(after, other.after);
}

private boolean checkParentIsNullOrNested(AggregatorFactory<?> parent) {
if (parent == null) {
return true;
} else if (parent instanceof ReverseNestedAggregatorFactory) {
return checkParentIsNullOrNested(parent.getParent());
} else {
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@
import org.elasticsearch.index.mapper.TypeFieldMapper;
import org.elasticsearch.index.mapper.Uid;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.search.aggregations.AggregatorTestCase;
import org.elasticsearch.search.aggregations.BucketOrder;
import org.elasticsearch.search.aggregations.InternalAggregation;
import org.elasticsearch.script.Script;
import org.elasticsearch.search.aggregations.*;
import org.elasticsearch.search.aggregations.bucket.composite.*;
import org.elasticsearch.search.aggregations.bucket.filter.Filter;
import org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder;
import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval;
import org.elasticsearch.search.aggregations.bucket.terms.StringTerms;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
Expand All @@ -64,6 +65,7 @@
import org.elasticsearch.search.aggregations.metrics.InternalSum;
import org.elasticsearch.search.aggregations.metrics.SumAggregationBuilder;
import org.elasticsearch.search.aggregations.support.ValueType;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.test.VersionUtils;

import java.io.IOException;
Expand All @@ -79,6 +81,8 @@
import static org.elasticsearch.search.aggregations.AggregationBuilders.max;
import static org.elasticsearch.search.aggregations.AggregationBuilders.nested;



public class NestedAggregatorTests extends AggregatorTestCase {

private static final String VALUE_FIELD_NAME = "number";
Expand Down Expand Up @@ -129,6 +133,114 @@ public void testNoDocs() throws IOException {
}
}

private DateHistogramValuesSourceBuilder randomDateHistogramSourceBuilder() {
DateHistogramValuesSourceBuilder histo = new DateHistogramValuesSourceBuilder(randomAlphaOfLengthBetween(5, 10));
if (randomBoolean()) {
histo.field(randomAlphaOfLengthBetween(1, 20));
} else {
histo.script(new Script(randomAlphaOfLengthBetween(10, 20)));
}
if (randomBoolean()) {
histo.dateHistogramInterval(randomFrom(DateHistogramInterval.days(10),
DateHistogramInterval.minutes(1), DateHistogramInterval.weeks(1)));
} else {
histo.interval(randomNonNegativeLong());
}
if (randomBoolean()) {
histo.timeZone(randomDateTimeZone());
}
if (randomBoolean()) {
histo.missingBucket(true);
}
return histo;
}

private TermsValuesSourceBuilder randomTermsSourceBuilder() {
TermsValuesSourceBuilder terms = new TermsValuesSourceBuilder(randomAlphaOfLengthBetween(5, 10));
if (randomBoolean()) {
terms.field(randomAlphaOfLengthBetween(1, 20));
} else {
terms.script(new Script(randomAlphaOfLengthBetween(10, 20)));
}
terms.order(randomFrom(SortOrder.values()));
if (randomBoolean()) {
terms.missingBucket(true);
}
return terms;
}

private HistogramValuesSourceBuilder randomHistogramSourceBuilder() {
HistogramValuesSourceBuilder histo = new HistogramValuesSourceBuilder(randomAlphaOfLengthBetween(5, 10));
if (randomBoolean()) {
histo.field(randomAlphaOfLengthBetween(1, 20));
} else {
histo.script(new Script(randomAlphaOfLengthBetween(10, 20)));
}
if (randomBoolean()) {
histo.missingBucket(true);
}
histo.interval(randomDoubleBetween(Math.nextUp(0), Double.MAX_VALUE, false));
return histo;
}


public void testNestedWithComposite() throws IOException {
int numRootDocs = randomIntBetween(1, 20);
int expectedNestedDocs = 0;
double expectedMaxValue = Double.NEGATIVE_INFINITY;


int numSources = randomIntBetween(1, 10);
List<CompositeValuesSourceBuilder<?>> sources = new ArrayList<>();
for (int i = 0; i < numSources; i++) {
int type = randomIntBetween(0, 2);
switch (type) {
case 0:
sources.add(randomTermsSourceBuilder());
break;
case 1:
sources.add(randomDateHistogramSourceBuilder());
break;
case 2:
sources.add(randomHistogramSourceBuilder());
break;
default:
throw new AssertionError("wrong branch");
}
}
CompositeAggregationBuilder compositeBuilder = new CompositeAggregationBuilder(randomAlphaOfLength(10), sources);

try (Directory directory = newDirectory()) {
try (RandomIndexWriter iw = new RandomIndexWriter(random(), directory)) {
for (int i = 0; i < numRootDocs; i++) {
List<Document> documents = new ArrayList<>();
int numNestedDocs = randomIntBetween(0, 20);
expectedMaxValue = Math.max(expectedMaxValue, generateMaxDocs(documents, numNestedDocs, i, NESTED_OBJECT, VALUE_FIELD_NAME));
expectedNestedDocs += numNestedDocs;

Document document = new Document();
document.add(new Field(IdFieldMapper.NAME, Uid.encodeId(Integer.toString(i)), IdFieldMapper.Defaults.FIELD_TYPE));
document.add(new Field(TypeFieldMapper.NAME, "test", TypeFieldMapper.Defaults.FIELD_TYPE));
document.add(sequenceIDFields.primaryTerm);
documents.add(document);
iw.addDocuments(documents);
}
iw.commit();
}
try (IndexReader indexReader = wrap(DirectoryReader.open(directory))) {
NestedAggregationBuilder nestedBuilder = new NestedAggregationBuilder(NESTED_AGG, NESTED_OBJECT);
MaxAggregationBuilder maxAgg = new MaxAggregationBuilder(MAX_AGG_NAME).field(VALUE_FIELD_NAME);
nestedBuilder.subAggregation(compositeBuilder);
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG);
fieldType.setName(VALUE_FIELD_NAME);

assertEquals(1, nestedBuilder.getSubAggregations().size());
assertEquals(CompositeAggregationBuilder.class, nestedBuilder.getSubAggregations().toArray()[0].getClass());
assertEquals("composite", compositeBuilder.getType());
}
}
}

public void testSingleNestingMax() throws IOException {
int numRootDocs = randomIntBetween(1, 20);
int expectedNestedDocs = 0;
Expand Down