From e8a4058a62160b1e7b24df0325cd3c51f4b99ad0 Mon Sep 17 00:00:00 2001 From: Lukas Olson Date: Wed, 22 Sep 2021 13:36:39 -0700 Subject: [PATCH] [data.search.aggs] Use fields instead of _source in top_hits agg (#109531) * [data.search] Handle warnings inside of headers * Update docs * Add tests * Remove isWarningResponse * [data.search.aggs] Use fields instead of _source in top_hits agg Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> --- .../search/aggs/metrics/top_hit.test.ts | 24 +++++++++---------- .../common/search/aggs/metrics/top_hit.ts | 6 ++--- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/plugins/data/common/search/aggs/metrics/top_hit.test.ts b/src/plugins/data/common/search/aggs/metrics/top_hit.test.ts index 04d382f1aa6d1..37ce9c4edb8d1 100644 --- a/src/plugins/data/common/search/aggs/metrics/top_hit.test.ts +++ b/src/plugins/data/common/search/aggs/metrics/top_hit.test.ts @@ -133,28 +133,28 @@ describe('Top hit metric', () => { }); it('should request the _source field', () => { - init({ field: '_source' }); - expect(aggDsl.top_hits._source).toBeTruthy(); - expect(aggDsl.top_hits.docvalue_fields).toBeUndefined(); + init({ fieldName: '_source' }); + expect(aggDsl.top_hits._source).toBe(true); + expect(aggDsl.top_hits.fields).toBeUndefined(); }); - it('requests both source and docvalues_fields for non-text aggregatable fields', () => { + it('requests fields for non-text aggregatable fields', () => { init({ fieldName: 'bytes', readFromDocValues: true }); - expect(aggDsl.top_hits._source).toBe('bytes'); - expect(aggDsl.top_hits.docvalue_fields).toEqual([{ field: 'bytes' }]); + expect(aggDsl.top_hits._source).toBe(false); + expect(aggDsl.top_hits.fields).toEqual([{ field: 'bytes' }]); }); - it('requests both source and docvalues_fields for date aggregatable fields', () => { + it('requests fields for date aggregatable fields', () => { init({ fieldName: '@timestamp', readFromDocValues: true, fieldType: KBN_FIELD_TYPES.DATE }); - expect(aggDsl.top_hits._source).toBe('@timestamp'); - expect(aggDsl.top_hits.docvalue_fields).toEqual([{ field: '@timestamp', format: 'date_time' }]); + expect(aggDsl.top_hits._source).toBe(false); + expect(aggDsl.top_hits.fields).toEqual([{ field: '@timestamp', format: 'date_time' }]); }); - it('requests just source for aggregatable text fields', () => { + it('requests fields for aggregatable text fields', () => { init({ fieldName: 'machine.os' }); - expect(aggDsl.top_hits._source).toBe('machine.os'); - expect(aggDsl.top_hits.docvalue_fields).toBeUndefined(); + expect(aggDsl.top_hits._source).toBe(false); + expect(aggDsl.top_hits.fields).toEqual([{ field: 'machine.os' }]); }); describe('try to get the value from the top hit', () => { diff --git a/src/plugins/data/common/search/aggs/metrics/top_hit.ts b/src/plugins/data/common/search/aggs/metrics/top_hit.ts index 094b5cda9a46d..a4bd99d6b210d 100644 --- a/src/plugins/data/common/search/aggs/metrics/top_hit.ts +++ b/src/plugins/data/common/search/aggs/metrics/top_hit.ts @@ -78,8 +78,8 @@ export const getTopHitMetricAgg = () => { }, }; } else { - if (field.readFromDocValues) { - output.params.docvalue_fields = [ + if (field.name !== '_source') { + output.params.fields = [ { field: field.name, // always format date fields as date_time to avoid @@ -89,7 +89,7 @@ export const getTopHitMetricAgg = () => { }, ]; } - output.params._source = field.name === '_source' ? true : field.name; + output.params._source = field.name === '_source'; } }, },