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,