From 9c88d02bb7fd6cd001427bc48f118e8e323aad32 Mon Sep 17 00:00:00 2001 From: ppisljar Date: Tue, 2 Mar 2021 09:32:56 -0800 Subject: [PATCH] fix agg config sub agg dsl generation --- .../common/search/aggs/agg_config.test.ts | 45 +++++++++++++++++++ .../data/common/search/aggs/agg_config.ts | 6 ++- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/plugins/data/common/search/aggs/agg_config.test.ts b/src/plugins/data/common/search/aggs/agg_config.test.ts index 5e52779ffa218..818255f6c8bcc 100644 --- a/src/plugins/data/common/search/aggs/agg_config.test.ts +++ b/src/plugins/data/common/search/aggs/agg_config.test.ts @@ -141,6 +141,51 @@ describe('AggConfig', () => { expect(dsl.aggs[avgConfig.id]).toHaveProperty('avg'); expect(dsl.aggs[avgConfig.id].avg).toBe(football); }); + + it('merges subAggs from #write() output to the current subaggs', () => { + const configStates = [ + { + enabled: true, + type: 'avg', + schema: 'metric', + params: {}, + }, + { + enabled: true, + type: 'median', + schema: 'metric', + params: {}, + }, + { + enabled: true, + type: 'date_histogram', + schema: 'segment', + params: {}, + }, + ]; + const ac = new AggConfigs(indexPattern, configStates, { typesRegistry }); + + const histoConfig = ac.byName('date_histogram')[0]; + const avgConfig = ac.byName('avg')[0]; + const medianConfig = ac.byName('median')[0]; + const football = {}; + + jest + .spyOn(histoConfig, 'write') + .mockImplementation(() => ({ params: {}, subAggs: [avgConfig] })); + jest.spyOn(avgConfig, 'write').mockImplementation(() => ({ params: football })); + jest.spyOn(medianConfig, 'write').mockImplementation(() => ({ params: football })); + + (histoConfig as any).subAggs = [medianConfig]; + const dsl = histoConfig.toDsl(); + expect(dsl).toHaveProperty('aggs'); + expect(dsl.aggs).toHaveProperty(avgConfig.id); + expect(dsl.aggs[avgConfig.id]).toHaveProperty('avg'); + expect(dsl.aggs[avgConfig.id].avg).toBe(football); + expect(dsl.aggs).toHaveProperty(medianConfig.id); + expect(dsl.aggs[medianConfig.id]).toHaveProperty('percentiles'); + expect(dsl.aggs[medianConfig.id].percentiles).toBe(football); + }); }); describe('::ensureIds', () => { diff --git a/src/plugins/data/common/search/aggs/agg_config.ts b/src/plugins/data/common/search/aggs/agg_config.ts index 6a5bc64cbdc55..f62fedc13b32a 100644 --- a/src/plugins/data/common/search/aggs/agg_config.ts +++ b/src/plugins/data/common/search/aggs/agg_config.ts @@ -235,7 +235,11 @@ export class AggConfig { configDsl[this.type.dslName || this.type.name] = output.params; // if the config requires subAggs, write them to the dsl as well - if (this.subAggs.length && !output.subAggs) output.subAggs = this.subAggs; + if (this.subAggs.length) { + if (!output.subAggs) output.subAggs = this.subAggs; + else output.subAggs.push(...this.subAggs); + } + if (output.subAggs) { const subDslLvl = configDsl.aggs || (configDsl.aggs = {}); output.subAggs.forEach(function nestAdhocSubAggs(subAggConfig: any) {