From 4925a4983a74572b2fba84f46111f9d04225950e Mon Sep 17 00:00:00 2001 From: Luke Elmers Date: Mon, 13 Jul 2020 09:11:11 -0600 Subject: [PATCH] Ensure Other bucket works on scripted fields. (#71329) --- .../_terms_other_bucket_helper.test.ts | 77 +++++++++++++++++++ .../buckets/_terms_other_bucket_helper.ts | 10 ++- 2 files changed, 83 insertions(+), 4 deletions(-) diff --git a/src/plugins/data/public/search/aggs/buckets/_terms_other_bucket_helper.test.ts b/src/plugins/data/public/search/aggs/buckets/_terms_other_bucket_helper.test.ts index 8e862b5692ca3..e9b4629ba88cf 100644 --- a/src/plugins/data/public/search/aggs/buckets/_terms_other_bucket_helper.test.ts +++ b/src/plugins/data/public/search/aggs/buckets/_terms_other_bucket_helper.test.ts @@ -316,6 +316,83 @@ describe('Terms Agg Other bucket helper', () => { } }); + test('excludes exists filter for scripted fields', () => { + const aggConfigs = getAggConfigs(nestedTerm.aggs); + aggConfigs.aggs[1].params.field.scripted = true; + const agg = buildOtherBucketAgg( + aggConfigs, + aggConfigs.aggs[1] as IBucketAggConfig, + nestedTermResponse + ); + const expectedResponse = { + 'other-filter': { + aggs: undefined, + filters: { + filters: { + '-IN': { + bool: { + must: [], + filter: [{ match_phrase: { 'geo.src': 'IN' } }], + should: [], + must_not: [ + { + script: { + script: { + lang: undefined, + params: { value: 'ios' }, + source: '(undefined) == value', + }, + }, + }, + { + script: { + script: { + lang: undefined, + params: { value: 'win xp' }, + source: '(undefined) == value', + }, + }, + }, + ], + }, + }, + '-US': { + bool: { + must: [], + filter: [{ match_phrase: { 'geo.src': 'US' } }], + should: [], + must_not: [ + { + script: { + script: { + lang: undefined, + params: { value: 'ios' }, + source: '(undefined) == value', + }, + }, + }, + { + script: { + script: { + lang: undefined, + params: { value: 'win xp' }, + source: '(undefined) == value', + }, + }, + }, + ], + }, + }, + }, + }, + }, + }; + expect(agg).toBeDefined(); + if (agg) { + expect(agg()).toEqual(expectedResponse); + } + }); + test('returns false when nested terms agg has no buckets', () => { const aggConfigs = getAggConfigs(nestedTerm.aggs); const agg = buildOtherBucketAgg( diff --git a/src/plugins/data/public/search/aggs/buckets/_terms_other_bucket_helper.ts b/src/plugins/data/public/search/aggs/buckets/_terms_other_bucket_helper.ts index fba3d35f002af..1a7deafb548ae 100644 --- a/src/plugins/data/public/search/aggs/buckets/_terms_other_bucket_helper.ts +++ b/src/plugins/data/public/search/aggs/buckets/_terms_other_bucket_helper.ts @@ -202,10 +202,12 @@ export const buildOtherBucketAgg = ( return; } - if ( - !aggWithOtherBucket.params.missingBucket || - agg.buckets.some((bucket: { key: string }) => bucket.key === '__missing__') - ) { + const hasScriptedField = !!aggWithOtherBucket.params.field.scripted; + const hasMissingBucket = !!aggWithOtherBucket.params.missingBucket; + const hasMissingBucketKey = agg.buckets.some( + (bucket: { key: string }) => bucket.key === '__missing__' + ); + if (!hasScriptedField && (!hasMissingBucket || hasMissingBucketKey)) { filters.push( buildExistsFilter( aggWithOtherBucket.params.field,