Add referenced pipeline aggs to every level of query (#31121) (#31711)

This commit is contained in:
Joe Reuter 2019-02-22 10:55:54 +01:00 committed by GitHub
parent 0cd62dd722
commit e978baa10e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 59 additions and 0 deletions

View file

@ -353,5 +353,57 @@ describe('AggConfigs', function () {
}
}(topLevelDsl));
});
it('adds the parent aggs of nested metrics at every level if the vis is hierarchical', function () {
const vis = new Vis(indexPattern, {
type: 'histogram',
aggs: [
{
id: '1',
type: 'avg_bucket',
schema: 'metric',
params: {
customBucket: {
id: '1-bucket',
type: 'date_histogram',
schema: 'bucketAgg',
params: {
field: '@timestamp'
}
},
customMetric: {
id: '1-metric',
type: 'count',
schema: 'metricAgg',
params: {}
}
}
},
{
id: '2',
type: 'terms',
schema: 'bucket',
params: {
field: 'geo.src',
}
},
{
id: '3',
type: 'terms',
schema: 'bucket',
params: {
field: 'machine.os',
}
}
]
});
vis.isHierarchical = _.constant(true);
const topLevelDsl = vis.aggs.toDsl(vis.isHierarchical())['2'];
expect(topLevelDsl.aggs).to.have.keys(['1', '1-bucket']);
expect(topLevelDsl.aggs['1'].avg_bucket).to.have.property('buckets_path', '1-bucket>_count');
expect(topLevelDsl.aggs['3'].aggs).to.have.keys(['1', '1-bucket']);
expect(topLevelDsl.aggs['3'].aggs['1'].avg_bucket).to.have.property('buckets_path', '1-bucket>_count');
});
});
});

View file

@ -193,6 +193,13 @@ class AggConfigs extends IndexedArray {
if (subAggs && nestedMetrics) {
nestedMetrics.forEach(agg => {
subAggs[agg.config.id] = agg.dsl;
// if a nested metric agg has parent aggs, we have to add them to every level of the tree
// to make sure "bucket_path" references in the nested metric agg itself are still working
if (agg.dsl.parentAggs) {
Object.entries(agg.dsl.parentAggs).forEach(([parentAggId, parentAgg]) => {
subAggs[parentAggId] = parentAgg;
});
}
});
}
});