Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG][Data] Support for custom filters with heterogeneous data fields #5577

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- [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
Loading