diff --git a/src/plugins/data/common/opensearch_query/opensearch_query/custom_filter_matches_index.test.ts b/src/plugins/data/common/opensearch_query/opensearch_query/custom_filter_matches_index.test.ts new file mode 100644 index 000000000000..50a6084dafa6 --- /dev/null +++ b/src/plugins/data/common/opensearch_query/opensearch_query/custom_filter_matches_index.test.ts @@ -0,0 +1,51 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +import { Filter } from '../filters'; +import { customFilterMatchesIndex } from './custom_filter_matches_index'; +import { IIndexPattern } from '../../index_patterns'; + +describe('customFilterMatchesIndex', () => { + it('should return true if the custom filter has no meta', () => { + const filter = {} as Filter; + const indexPattern = { id: 'foo', fields: [{ name: 'bar' }] } as IIndexPattern; + + expect(customFilterMatchesIndex(filter, indexPattern)).toBe(true); + }); + + it('should return true if no index pattern is passed', () => { + const filter = { meta: { index: 'foo', key: 'bar', type: 'custom' } } as Filter; + + expect(customFilterMatchesIndex(filter, undefined)).toBe(true); + }); + + it('should return true if the custom filter has meta without a key', () => { + const filter = { meta: { index: 'foo', type: 'custom' } } as Filter; + const indexPattern = { id: 'foo', fields: [{ name: 'bar' }] } as IIndexPattern; + + expect(customFilterMatchesIndex(filter, indexPattern)).toBe(true); + }); + + it('should return false if the filter is not custom', () => { + const filter = { meta: { index: 'foo', key: 'bar', type: 'match_all' } } as Filter; + const indexPattern = { id: 'foo', fields: [{ name: 'bar' }] } as IIndexPattern; + + expect(customFilterMatchesIndex(filter, indexPattern)).toBe(false); + }); + + it('should return false if the custom filter is a different index id', () => { + const filter = { meta: { index: 'foo', key: 'bar', type: 'custom' } } as Filter; + const indexPattern = { id: 'bar', fields: [{ name: 'foo' }] } as IIndexPattern; + + expect(customFilterMatchesIndex(filter, indexPattern)).toBe(false); + }); + + it('should return true if the custom filter is the same index id', () => { + const filter = { meta: { index: 'foo', key: 'bar', type: 'custom' } } as Filter; + const indexPattern = { id: 'foo', fields: [{ name: 'barf' }] } as IIndexPattern; + + expect(customFilterMatchesIndex(filter, indexPattern)).toBe(true); + }); +}); diff --git a/src/plugins/data/common/opensearch_query/opensearch_query/custom_filter_matches_index.ts b/src/plugins/data/common/opensearch_query/opensearch_query/custom_filter_matches_index.ts new file mode 100644 index 000000000000..c98f6d1575a1 --- /dev/null +++ b/src/plugins/data/common/opensearch_query/opensearch_query/custom_filter_matches_index.ts @@ -0,0 +1,17 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +import { IIndexPattern } from '../../index_patterns'; +import { Filter } from '../filters'; + +export function customFilterMatchesIndex(filter: Filter, indexPattern?: IIndexPattern | null) { + if (!filter.meta?.key || !indexPattern) { + return true; + } + if (filter.meta?.type !== 'custom') { + return false; + } + return filter.meta.index === indexPattern.id; +} diff --git a/src/plugins/data/common/opensearch_query/opensearch_query/from_filters.ts b/src/plugins/data/common/opensearch_query/opensearch_query/from_filters.ts index 990dca144abf..e7b13f5a10d6 100644 --- a/src/plugins/data/common/opensearch_query/opensearch_query/from_filters.ts +++ b/src/plugins/data/common/opensearch_query/opensearch_query/from_filters.ts @@ -34,6 +34,7 @@ import { filterMatchesIndex } from './filter_matches_index'; import { Filter, cleanFilter, isFilterDisabled } from '../filters'; import { IIndexPattern } from '../../index_patterns'; import { handleNestedFilter } from './handle_nested_filter'; +import { customFilterMatchesIndex } from './custom_filter_matches_index'; /** * Create a filter that can be reversed for filters with negate set @@ -76,7 +77,10 @@ export const buildQueryFromFilters = ( return filters .filter(filterNegate(negate)) .filter( - (filter) => !ignoreFilterIfFieldNotInIndex || filterMatchesIndex(filter, indexPattern) + (filter) => + !ignoreFilterIfFieldNotInIndex || + filterMatchesIndex(filter, indexPattern) || + customFilterMatchesIndex(filter, indexPattern) ) .map((filter) => { return migrateFilter(filter, indexPattern);