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
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:
opensearch-project#5423

Should closed as that is expected functionality.

Signed-off-by: Kawika Avilla <[email protected]>
  • Loading branch information
kavilla committed Dec 6, 2023
1 parent 2e7a2ba commit 036b6f6
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- [BUG] Add platform "darwin-arm64" to unit test ([#5290](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5290))
- [BUG][Dev Tool] Add dev tool documentation link to dev tool's help menu [#5166](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5166)
- Fix missing border for header navigation control on right ([#5450](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5450))
- [BUG] Fix filtering issue in data source selector ([5484](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5484))
- [BUG][Data] Support for custom filters with heterogeneous data fields ([5577](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5577))

### 🚞 Infrastructure

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
});
});
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 036b6f6

Please sign in to comment.