diff --git a/x-pack/plugins/lens/public/app_plugin/show_underlying_data.test.ts b/x-pack/plugins/lens/public/app_plugin/show_underlying_data.test.ts index e74dd139e42c0..f7ae043eba1d7 100644 --- a/x-pack/plugins/lens/public/app_plugin/show_underlying_data.test.ts +++ b/x-pack/plugins/lens/public/app_plugin/show_underlying_data.test.ts @@ -112,8 +112,11 @@ describe('getLayerMetaInfo', () => { getVisualDefaults: jest.fn(), getSourceId: jest.fn(), getFilters: jest.fn(() => ({ - kuery: [[{ language: 'kuery', query: 'memory > 40000' }]], - lucene: [], + enabled: { + kuery: [[{ language: 'kuery', query: 'memory > 40000' }]], + lucene: [], + }, + disabled: { kuery: [], lucene: [] }, })), }; mockDatasource.getPublicAPI.mockReturnValue(updatedPublicAPI); @@ -128,42 +131,19 @@ describe('getLayerMetaInfo', () => { expect(error).toBeUndefined(); expect(meta?.columns).toEqual(['bytes']); expect(meta?.filters).toEqual({ - kuery: [ - [ - { - language: 'kuery', - query: 'memory > 40000', - }, + enabled: { + kuery: [ + [ + { + language: 'kuery', + query: 'memory > 40000', + }, + ], ], - ], - lucene: [], - }); - }); - - it('should return an error if datasource is not supported', () => { - const mockDatasource = createMockDatasource('testDatasource'); - const updatedPublicAPI: DatasourcePublicAPI = { - datasourceId: 'unsupportedDatasource', - getOperationForColumnId: jest.fn(), - getTableSpec: jest.fn(() => [{ columnId: 'col1', fields: ['bytes'] }]), - getVisualDefaults: jest.fn(), - getSourceId: jest.fn(), - getFilters: jest.fn(() => ({ - kuery: [[{ language: 'kuery', query: 'memory > 40000' }]], lucene: [], - })), - }; - mockDatasource.getPublicAPI.mockReturnValue(updatedPublicAPI); - const { error, meta } = getLayerMetaInfo( - mockDatasource, - {}, // the publicAPI has been mocked, so no need for a state here - { - datatable1: { type: 'datatable', columns: [], rows: [] }, }, - capabilities - ); - expect(error).toBe('Underlying data does not support the current datasource'); - expect(meta).toBeUndefined(); + disabled: { kuery: [], lucene: [] }, + }); }); }); describe('combineQueryAndFilters', () => { @@ -175,7 +155,7 @@ describe('combineQueryAndFilters', () => { { id: 'testDatasource', columns: [], - filters: { kuery: [], lucene: [] }, + filters: { enabled: { kuery: [], lucene: [] }, disabled: { kuery: [], lucene: [] } }, }, undefined ) @@ -190,7 +170,10 @@ describe('combineQueryAndFilters', () => { { id: 'testDatasource', columns: [], - filters: { kuery: [[{ language: 'kuery', query: 'otherField: *' }]], lucene: [] }, + filters: { + enabled: { kuery: [[{ language: 'kuery', query: 'otherField: *' }]], lucene: [] }, + disabled: { kuery: [], lucene: [] }, + }, }, undefined ) @@ -208,7 +191,10 @@ describe('combineQueryAndFilters', () => { { id: 'testDatasource', columns: [], - filters: { kuery: [[{ language: 'kuery', query: 'otherField: *' }]], lucene: [] }, + filters: { + enabled: { kuery: [[{ language: 'kuery', query: 'otherField: *' }]], lucene: [] }, + disabled: { kuery: [], lucene: [] }, + }, }, undefined ) @@ -225,17 +211,20 @@ describe('combineQueryAndFilters', () => { id: 'testDatasource', columns: [], filters: { - kuery: [ - [ - { language: 'kuery', query: 'myfield: *' }, - { language: 'kuery', query: 'otherField: *' }, - ], - [ - { language: 'kuery', query: 'myfieldCopy: *' }, - { language: 'kuery', query: 'otherFieldCopy: *' }, + enabled: { + kuery: [ + [ + { language: 'kuery', query: 'myfield: *' }, + { language: 'kuery', query: 'otherField: *' }, + ], + [ + { language: 'kuery', query: 'myfieldCopy: *' }, + { language: 'kuery', query: 'otherFieldCopy: *' }, + ], ], - ], - lucene: [], + lucene: [], + }, + disabled: { kuery: [], lucene: [] }, }, }, undefined @@ -258,8 +247,11 @@ describe('combineQueryAndFilters', () => { id: 'testDatasource', columns: [], filters: { - kuery: [[{ language: 'kuery', query: 'myfield: *' }]], - lucene: [], + enabled: { + kuery: [[{ language: 'kuery', query: 'myfield: *' }]], + lucene: [], + }, + disabled: { kuery: [], lucene: [] }, }, }, undefined @@ -342,8 +334,11 @@ describe('combineQueryAndFilters', () => { id: 'testDatasource', columns: [], filters: { - kuery: [], - lucene: [[{ language: 'lucene', query: 'anotherField' }]], + enabled: { + kuery: [], + lucene: [[{ language: 'lucene', query: 'anotherField' }]], + }, + disabled: { kuery: [], lucene: [] }, }, }, undefined @@ -421,8 +416,11 @@ describe('combineQueryAndFilters', () => { id: 'testDatasource', columns: [], filters: { - kuery: [[{ language: 'kuery', query: 'myfield: *' }]], - lucene: [[{ language: 'lucene', query: 'anotherField' }]], + enabled: { + kuery: [[{ language: 'kuery', query: 'myfield: *' }]], + lucene: [[{ language: 'lucene', query: 'anotherField' }]], + }, + disabled: { kuery: [], lucene: [] }, }, }, undefined @@ -513,22 +511,25 @@ describe('combineQueryAndFilters', () => { id: 'testDatasource', columns: [], filters: { - kuery: [ - [{ language: 'kuery', query: 'bytes > 4000' }], - [ - { language: 'kuery', query: 'memory > 5000' }, - { language: 'kuery', query: 'memory >= 15000' }, + enabled: { + kuery: [ + [{ language: 'kuery', query: 'bytes > 4000' }], + [ + { language: 'kuery', query: 'memory > 5000' }, + { language: 'kuery', query: 'memory >= 15000' }, + ], + [{ language: 'kuery', query: 'myField: *' }], + [{ language: 'kuery', query: 'otherField >= 15' }], ], - [{ language: 'kuery', query: 'myField: *' }], - [{ language: 'kuery', query: 'otherField >= 15' }], - ], - lucene: [ - [{ language: 'lucene', query: 'filteredField: 400' }], - [ - { language: 'lucene', query: 'aNewField' }, - { language: 'lucene', query: 'anotherNewField: 200' }, + lucene: [ + [{ language: 'lucene', query: 'filteredField: 400' }], + [ + { language: 'lucene', query: 'aNewField' }, + { language: 'lucene', query: 'anotherNewField: 200' }, + ], ], - ], + }, + disabled: { kuery: [], lucene: [] }, }, }, undefined @@ -599,4 +600,204 @@ describe('combineQueryAndFilters', () => { }, }); }); + + it('should add ignored filters as disabled', () => { + expect( + combineQueryAndFilters( + { language: 'lucene', query: 'myField' }, + [], + { + id: 'testDatasource', + columns: [], + filters: { + disabled: { + kuery: [[{ language: 'kuery', query: 'myfield: *' }]], + lucene: [[{ language: 'lucene', query: 'anotherField' }]], + }, + enabled: { kuery: [], lucene: [] }, + }, + }, + undefined + ) + ).toEqual({ + filters: [ + { + $state: { + store: 'appState', + }, + bool: { + filter: [], + must: [ + { + query_string: { + query: 'anotherField', + }, + }, + ], + must_not: [], + should: [], + }, + meta: { + alias: 'anotherField (lucene)', + disabled: true, + index: 'testDatasource', + negate: false, + type: 'custom', + }, + }, + { + $state: { + store: 'appState', + }, + bool: { + filter: [ + { + bool: { + minimum_should_match: 1, + should: [ + { + exists: { + field: 'myfield', + }, + }, + ], + }, + }, + ], + must: [], + must_not: [], + should: [], + }, + meta: { + alias: 'myfield: *', + disabled: true, + index: 'testDatasource', + negate: false, + type: 'custom', + }, + }, + ], + query: { + language: 'lucene', + query: 'myField', + }, + }); + }); + + it('should work together with enabled and disabled filters', () => { + expect( + combineQueryAndFilters( + { language: 'lucene', query: 'myField' }, + [], + { + id: 'testDatasource', + columns: [], + filters: { + disabled: { + kuery: [[{ language: 'kuery', query: 'myfield: abc' }]], + lucene: [[{ language: 'lucene', query: 'anotherField > 5000' }]], + }, + enabled: { + kuery: [[{ language: 'kuery', query: 'myfield: *' }]], + lucene: [[{ language: 'lucene', query: 'anotherField' }]], + }, + }, + }, + undefined + ) + ).toEqual({ + filters: [ + { + bool: { + must: [], + filter: [ + { + bool: { + should: [ + { + exists: { + field: 'myfield', + }, + }, + ], + minimum_should_match: 1, + }, + }, + ], + should: [], + must_not: [], + }, + meta: { + index: 'testDatasource', + type: 'custom', + disabled: false, + negate: false, + alias: 'Lens context (kuery)', + }, + $state: { + store: 'appState', + }, + }, + { + bool: { + must: [ + { + query_string: { + query: 'anotherField > 5000', + }, + }, + ], + filter: [], + should: [], + must_not: [], + }, + meta: { + index: 'testDatasource', + type: 'custom', + disabled: true, + negate: false, + alias: 'anotherField > 5000 (lucene)', + }, + $state: { + store: 'appState', + }, + }, + { + bool: { + must: [], + filter: [ + { + bool: { + should: [ + { + match: { + myfield: 'abc', + }, + }, + ], + minimum_should_match: 1, + }, + }, + ], + should: [], + must_not: [], + }, + meta: { + index: 'testDatasource', + type: 'custom', + disabled: true, + negate: false, + alias: 'myfield: abc', + }, + $state: { + store: 'appState', + }, + }, + ], + query: { + language: 'lucene', + query: '( myField ) AND ( anotherField )', + }, + }); + }); }); diff --git a/x-pack/plugins/lens/public/app_plugin/show_underlying_data.ts b/x-pack/plugins/lens/public/app_plugin/show_underlying_data.ts index e1956542f8def..7329159cc1d3b 100644 --- a/x-pack/plugins/lens/public/app_plugin/show_underlying_data.ts +++ b/x-pack/plugins/lens/public/app_plugin/show_underlying_data.ts @@ -43,10 +43,13 @@ function joinQueries(queries: Query[][] | undefined) { interface LayerMetaInfo { id: string; columns: string[]; - filters: { - kuery: Query[][] | undefined; - lucene: Query[][] | undefined; - }; + filters: Record< + 'enabled' | 'disabled', + { + kuery: Query[][] | undefined; + lucene: Query[][] | undefined; + } + >; } export function getLayerMetaInfo( @@ -137,8 +140,9 @@ export function combineQueryAndFilters( : { queryLanguage: 'kuery', filtersLanguage: 'lucene' }; let newQuery = query; - if (meta.filters[queryLanguage]?.length) { - const filtersQuery = joinQueries(meta.filters[queryLanguage]); + const enabledFilters = meta.filters.enabled; + if (enabledFilters[queryLanguage]?.length) { + const filtersQuery = joinQueries(enabledFilters[queryLanguage]); newQuery = { language: queryLanguage, query: query?.query.trim() @@ -149,17 +153,14 @@ export function combineQueryAndFilters( // make a copy as the original filters are readonly const newFilters = [...filters]; - if (meta.filters[filtersLanguage]?.length) { - const queryExpression = joinQueries(meta.filters[filtersLanguage]); + const dataView = dataViews?.find(({ id }) => id === meta.id); + if (enabledFilters[filtersLanguage]?.length) { + const queryExpression = joinQueries(enabledFilters[filtersLanguage]); // Append the new filter based on the queryExpression to the existing ones newFilters.push( buildCustomFilter( meta.id!, - buildEsQuery( - dataViews?.find(({ id }) => id === meta.id), - { language: filtersLanguage, query: queryExpression }, - [] - ), + buildEsQuery(dataView, { language: filtersLanguage, query: queryExpression }, []), false, false, i18n.translate('xpack.lens.app.lensContext', { @@ -170,5 +171,27 @@ export function combineQueryAndFilters( ) ); } + // for each disabled filter create a new custom filter and disable it + // note that both languages go into the filter bar + const disabledFilters = meta.filters.disabled; + for (const language of ['lucene', 'kuery'] as const) { + const [disabledQueries] = disabledFilters[language] || []; + for (const disabledQuery of disabledQueries || []) { + let label = disabledQuery.query as string; + if (language === 'lucene') { + label += ` (${language})`; + } + newFilters.push( + buildCustomFilter( + meta.id!, + buildEsQuery(dataView, disabledQuery, []), + true, + false, + label, + FilterStateStore.APP_STATE + ) + ); + } + } return { filters: newFilters, query: newQuery }; } diff --git a/x-pack/plugins/lens/public/indexpattern_datasource/indexpattern.test.ts b/x-pack/plugins/lens/public/indexpattern_datasource/indexpattern.test.ts index 42dd23a6d2242..ed5163921472f 100644 --- a/x-pack/plugins/lens/public/indexpattern_datasource/indexpattern.test.ts +++ b/x-pack/plugins/lens/public/indexpattern_datasource/indexpattern.test.ts @@ -1446,8 +1446,11 @@ describe('IndexPattern Data Source', () => { layerId: 'first', }); expect(publicAPI.getFilters()).toEqual({ - kuery: [[{ language: 'kuery', query: 'bytes > 1000' }]], - lucene: [[{ language: 'lucene', query: 'memory' }]], + enabled: { + kuery: [[{ language: 'kuery', query: 'bytes > 1000' }]], + lucene: [[{ language: 'lucene', query: 'memory' }]], + }, + disabled: { kuery: [], lucene: [] }, }); }); it('should ignore empty filtered metrics', () => { @@ -1474,7 +1477,10 @@ describe('IndexPattern Data Source', () => { }, layerId: 'first', }); - expect(publicAPI.getFilters()).toEqual({ kuery: [], lucene: [] }); + expect(publicAPI.getFilters()).toEqual({ + enabled: { kuery: [], lucene: [] }, + disabled: { kuery: [], lucene: [] }, + }); }); it('shuold collect top values fields as kuery existence filters if no data is provided', () => { publicAPI = indexPatternDatasource.getPublicAPI({ @@ -1517,14 +1523,17 @@ describe('IndexPattern Data Source', () => { layerId: 'first', }); expect(publicAPI.getFilters()).toEqual({ - kuery: [ - [{ language: 'kuery', query: 'geo.src: *' }], - [ - { language: 'kuery', query: 'geo.dest: *' }, - { language: 'kuery', query: 'myField: *' }, + enabled: { + kuery: [ + [{ language: 'kuery', query: 'geo.src: *' }], + [ + { language: 'kuery', query: 'geo.dest: *' }, + { language: 'kuery', query: 'myField: *' }, + ], ], - ], - lucene: [], + lucene: [], + }, + disabled: { kuery: [], lucene: [] }, }); }); it('shuold collect top values fields and terms as kuery filters if data is provided', () => { @@ -1581,17 +1590,20 @@ describe('IndexPattern Data Source', () => { }, }; expect(publicAPI.getFilters(data)).toEqual({ - kuery: [ - [ - { language: 'kuery', query: 'geo.src: "US"' }, - { language: 'kuery', query: 'geo.src: "IN"' }, - ], - [ - { language: 'kuery', query: 'geo.dest: "IT" AND myField: "MyValue"' }, - { language: 'kuery', query: 'geo.dest: "DE" AND myField: "MyOtherValue"' }, + enabled: { + kuery: [ + [ + { language: 'kuery', query: 'geo.src: "US"' }, + { language: 'kuery', query: 'geo.src: "IN"' }, + ], + [ + { language: 'kuery', query: 'geo.dest: "IT" AND myField: "MyValue"' }, + { language: 'kuery', query: 'geo.dest: "DE" AND myField: "MyOtherValue"' }, + ], ], - ], - lucene: [], + lucene: [], + }, + disabled: { kuery: [], lucene: [] }, }); }); it('shuold collect top values fields and terms and carefully handle empty string values', () => { @@ -1648,17 +1660,20 @@ describe('IndexPattern Data Source', () => { }, }; expect(publicAPI.getFilters(data)).toEqual({ - kuery: [ - [ - { language: 'kuery', query: 'geo.src: "US"' }, - { language: 'kuery', query: 'geo.src: "IN"' }, - ], - [ - { language: 'kuery', query: `geo.dest: "IT" AND myField: ""` }, - { language: 'kuery', query: `geo.dest: "DE" AND myField: "MyOtherValue"` }, + enabled: { + kuery: [ + [ + { language: 'kuery', query: 'geo.src: "US"' }, + { language: 'kuery', query: 'geo.src: "IN"' }, + ], + [ + { language: 'kuery', query: `geo.dest: "IT" AND myField: ""` }, + { language: 'kuery', query: `geo.dest: "DE" AND myField: "MyOtherValue"` }, + ], ], - ], - lucene: [], + lucene: [], + }, + disabled: { kuery: [], lucene: [] }, }); }); it('should ignore top values fields if other/missing option is enabled', () => { @@ -1702,7 +1717,10 @@ describe('IndexPattern Data Source', () => { }, layerId: 'first', }); - expect(publicAPI.getFilters()).toEqual({ kuery: [], lucene: [] }); + expect(publicAPI.getFilters()).toEqual({ + enabled: { kuery: [], lucene: [] }, + disabled: { kuery: [], lucene: [] }, + }); }); it('should collect custom ranges as kuery filters', () => { publicAPI = indexPatternDatasource.getPublicAPI({ @@ -1745,14 +1763,17 @@ describe('IndexPattern Data Source', () => { layerId: 'first', }); expect(publicAPI.getFilters()).toEqual({ - kuery: [ - [{ language: 'kuery', query: 'bytes >= 100 AND bytes <= 150' }], - [ - { language: 'kuery', query: 'bytes >= 200 AND bytes <= 300' }, - { language: 'kuery', query: 'bytes >= 300 AND bytes <= 400' }, + enabled: { + kuery: [ + [{ language: 'kuery', query: 'bytes >= 100 AND bytes <= 150' }], + [ + { language: 'kuery', query: 'bytes >= 200 AND bytes <= 300' }, + { language: 'kuery', query: 'bytes >= 300 AND bytes <= 400' }, + ], ], - ], - lucene: [], + lucene: [], + }, + disabled: { kuery: [], lucene: [] }, }); }); it('should collect custom ranges as kuery filters as partial', () => { @@ -1804,11 +1825,14 @@ describe('IndexPattern Data Source', () => { layerId: 'first', }); expect(publicAPI.getFilters()).toEqual({ - kuery: [ - [{ language: 'kuery', query: 'bytes >= 100' }], - [{ language: 'kuery', query: 'bytes <= 300' }], - ], - lucene: [], + enabled: { + kuery: [ + [{ language: 'kuery', query: 'bytes >= 100' }], + [{ language: 'kuery', query: 'bytes <= 300' }], + ], + lucene: [], + }, + disabled: { kuery: [], lucene: [] }, }); }); it('should collect filters within filters operation grouped by language', () => { @@ -1862,20 +1886,23 @@ describe('IndexPattern Data Source', () => { layerId: 'first', }); expect(publicAPI.getFilters()).toEqual({ - kuery: [ - [{ language: 'kuery', query: 'bytes > 1000' }], - [ - { language: 'kuery', query: 'bytes > 5000' }, - { language: 'kuery', query: 'memory > 500000' }, + enabled: { + kuery: [ + [{ language: 'kuery', query: 'bytes > 1000' }], + [ + { language: 'kuery', query: 'bytes > 5000' }, + { language: 'kuery', query: 'memory > 500000' }, + ], ], - ], - lucene: [ - [{ language: 'lucene', query: 'memory' }], - [ - { language: 'lucene', query: 'phpmemory' }, - { language: 'lucene', query: 'memory: 5000000' }, + lucene: [ + [{ language: 'lucene', query: 'memory' }], + [ + { language: 'lucene', query: 'phpmemory' }, + { language: 'lucene', query: 'memory: 5000000' }, + ], ], - ], + }, + disabled: { kuery: [], lucene: [] }, }); }); it('should ignore filtered metrics if at least one metric is unfiltered', () => { @@ -1911,8 +1938,8 @@ describe('IndexPattern Data Source', () => { layerId: 'first', }); expect(publicAPI.getFilters()).toEqual({ - kuery: [], - lucene: [], + enabled: { kuery: [], lucene: [] }, + disabled: { kuery: [[{ language: 'kuery', query: 'bytes > 1000' }]], lucene: [] }, }); }); it('should ignore filtered metrics if at least one metric is unfiltered in formula', () => { @@ -1983,8 +2010,8 @@ describe('IndexPattern Data Source', () => { layerId: 'first', }); expect(publicAPI.getFilters()).toEqual({ - kuery: [], - lucene: [], + enabled: { kuery: [], lucene: [] }, + disabled: { kuery: [[{ language: 'kuery', query: 'memory > 5000' }]], lucene: [] }, }); }); it('should support complete scenarios', () => { @@ -2049,24 +2076,27 @@ describe('IndexPattern Data Source', () => { layerId: 'first', }); expect(publicAPI.getFilters()).toEqual({ - kuery: [ - [{ language: 'kuery', query: 'bytes > 1000' }], - [ - { language: 'kuery', query: 'bytes > 5000' }, - { language: 'kuery', query: 'memory > 500000' }, + enabled: { + kuery: [ + [{ language: 'kuery', query: 'bytes > 1000' }], + [ + { language: 'kuery', query: 'bytes > 5000' }, + { language: 'kuery', query: 'memory > 500000' }, + ], + [ + { language: 'kuery', query: 'geo.src: *' }, + { language: 'kuery', query: 'myField: *' }, + ], ], - [ - { language: 'kuery', query: 'geo.src: *' }, - { language: 'kuery', query: 'myField: *' }, + lucene: [ + [{ language: 'lucene', query: 'memory' }], + [ + { language: 'lucene', query: 'phpmemory' }, + { language: 'lucene', query: 'memory: 5000000' }, + ], ], - ], - lucene: [ - [{ language: 'lucene', query: 'memory' }], - [ - { language: 'lucene', query: 'phpmemory' }, - { language: 'lucene', query: 'memory: 5000000' }, - ], - ], + }, + disabled: { kuery: [], lucene: [] }, }); }); @@ -2140,13 +2170,16 @@ describe('IndexPattern Data Source', () => { layerId: 'first', }); expect(publicAPI.getFilters()).toEqual({ - kuery: [ - [ - { language: 'kuery', query: 'bytes > 4000 AND memory > 5000' }, - { language: 'kuery', query: 'bytes > 4000' }, + enabled: { + kuery: [ + [ + { language: 'kuery', query: 'bytes > 4000 AND memory > 5000' }, + { language: 'kuery', query: 'bytes > 4000' }, + ], ], - ], - lucene: [], + lucene: [], + }, + disabled: { kuery: [], lucene: [] }, }); }); }); diff --git a/x-pack/plugins/lens/public/indexpattern_datasource/utils.tsx b/x-pack/plugins/lens/public/indexpattern_datasource/utils.tsx index b5f82653565c8..4062f2b2613fe 100644 --- a/x-pack/plugins/lens/public/indexpattern_datasource/utils.tsx +++ b/x-pack/plugins/lens/public/indexpattern_datasource/utils.tsx @@ -352,16 +352,15 @@ function collectFiltersFromMetrics(layer: IndexPatternLayer, columnIds: string[] layer.columns[colId]?.filter ? 'filtered' : 'unfiltered' ); - // extract filters from filtered metrics - // consider all the columns, included referenced ones to cover also the formula case - return ( - filtered - // if there are metric columns not filtered, then ignore filtered columns completely - .filter(() => !unfiltered.length) - .map((colId) => layer.columns[colId]?.filter) - // filter out empty filters as well - .filter((filter) => filter?.query?.trim()) as Query[] - ); + const filteredMetrics = filtered + .map((colId) => layer.columns[colId]?.filter) + // filter out empty filters as well + .filter((filter) => filter?.query?.trim()) as Query[]; + + return { + enabled: unfiltered.length ? [] : filteredMetrics, + disabled: unfiltered.length ? filteredMetrics : [], + }; } interface GroupedQueries { @@ -385,10 +384,10 @@ export function getFiltersInLayer( columnIds: string[], layerData: NonNullable[string] | undefined ) { - const filtersFromMetricsByLanguage = groupBy( - collectFiltersFromMetrics(layer, columnIds), - 'language' - ) as unknown as GroupedQueries; + const filtersGroupedByState = collectFiltersFromMetrics(layer, columnIds); + const [enabledFiltersFromMetricsByLanguage, disabledFitleredFromMetricsByLanguage] = ( + ['enabled', 'disabled'] as const + ).map((state) => groupBy(filtersGroupedByState[state], 'language') as unknown as GroupedQueries); const filterOperation = columnIds .map((colId) => { @@ -433,7 +432,19 @@ export function getFiltersInLayer( }) .filter(Boolean) as GroupedQueries[]; return { - kuery: collectOnlyValidQueries(filtersFromMetricsByLanguage, filterOperation, 'kuery'), - lucene: collectOnlyValidQueries(filtersFromMetricsByLanguage, filterOperation, 'lucene'), + enabled: { + kuery: collectOnlyValidQueries(enabledFiltersFromMetricsByLanguage, filterOperation, 'kuery'), + lucene: collectOnlyValidQueries( + enabledFiltersFromMetricsByLanguage, + filterOperation, + 'lucene' + ), + }, + disabled: { + kuery: [disabledFitleredFromMetricsByLanguage.kuery || []].filter((filter) => filter.length), + lucene: [disabledFitleredFromMetricsByLanguage.lucene || []].filter( + (filter) => filter.length + ), + }, }; } diff --git a/x-pack/plugins/lens/public/types.ts b/x-pack/plugins/lens/public/types.ts index 107884a849217..058d850dee09c 100644 --- a/x-pack/plugins/lens/public/types.ts +++ b/x-pack/plugins/lens/public/types.ts @@ -368,10 +368,13 @@ export interface DatasourcePublicAPI { /** * Collect all defined filters from all the operations in the layer */ - getFilters: (activeData?: FramePublicAPI['activeData']) => { - kuery: Query[][]; - lucene: Query[][]; - }; + getFilters: (activeData?: FramePublicAPI['activeData']) => Record< + 'enabled' | 'disabled', + { + kuery: Query[][]; + lucene: Query[][]; + } + >; } export interface DatasourceDataPanelProps {