From a96f8fe2e3147e3cd0ed8a645e69539c77520dd1 Mon Sep 17 00:00:00 2001 From: Kawika Avilla Date: Wed, 6 Dec 2023 10:21:43 +0000 Subject: [PATCH] [BUG][Data] Support for custom filters with heterogeneous data fields When enabling the advanced setting `courier:ignoreFilterIfFieldNotInIndex` Custom OpenSearch Query DSL filters could technically be applied to index patterns that map to indices that are not exactly the same. Since the custom query filter is a user input then users can really type anything that they need. Or any field that they know is present but we do not know for sure. Therefore, we can check if the id which is the index pattern title to check if we should apply the filter or not. Issue resolved: https://github.com/opensearch-project/dashboards-visualizations/issues/281 I believe issue: https://github.com/opensearch-project/OpenSearch-Dashboards/issues/5423 Should closed as that is expected functionality. Signed-off-by: Kawika Avilla --- .../custom_filter_matches_index.test.ts | 51 +++++++++++++++++++ .../custom_filter_matches_index.ts | 17 +++++++ .../opensearch_query/from_filters.ts | 6 ++- 3 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 src/plugins/data/common/opensearch_query/opensearch_query/custom_filter_matches_index.test.ts create mode 100644 src/plugins/data/common/opensearch_query/opensearch_query/custom_filter_matches_index.ts 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);