Skip to content

Commit

Permalink
[BUG][Data] Support for custom filters with heterogeneous data fields (
Browse files Browse the repository at this point in the history
…#5577) (#5585)

* [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:
#5423

Should closed as that is expected functionality.

Signed-off-by: Kawika Avilla <[email protected]>

* [Cleanup] utilize the same helper function

Originally when implementing the fix the historical comment caused concern about
potential breaking changes.

But after discussion, we decided it is more clear to consolidate the helper functions.

Signed-off-by: Kawika Avilla <[email protected]>
(cherry picked from commit 805400d)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
1 parent 592b27d commit 12af400
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,18 @@ describe('filterMatchesIndex', () => {

expect(filterMatchesIndex(filter, indexPattern)).toBe(true);
});

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(filterMatchesIndex(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(filterMatchesIndex(filter, indexPattern)).toBe(true);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@
import { IIndexPattern, IFieldType } from '../../index_patterns';
import { Filter } from '../filters';

/*
* TODO: We should base this on something better than `filter.meta.key`. We should probably modify
* this to check if `filter.meta.index` matches `indexPattern.id` instead, but that's a breaking
* change.
*/
export function filterMatchesIndex(filter: Filter, indexPattern?: IIndexPattern | null) {
if (!filter.meta?.key || !indexPattern) {
return true;
}

if (filter.meta?.type === 'custom') {
return filter.meta.index === indexPattern.id;
}

return indexPattern.fields.some((field: IFieldType) => field.name === filter.meta.key);
}

0 comments on commit 12af400

Please sign in to comment.