Skip to content

Commit

Permalink
[Cleanup] utilize the same helper function
Browse files Browse the repository at this point in the history
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 <kavilla414@gmail.com>
  • Loading branch information
kavilla committed Dec 7, 2023
1 parent 8551434 commit fd418fc
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 78 deletions.

This file was deleted.

This file was deleted.

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;

Check warning on line 40 in src/plugins/data/common/opensearch_query/opensearch_query/filter_matches_index.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/common/opensearch_query/opensearch_query/filter_matches_index.ts#L40

Added line #L40 was not covered by tests
}

return indexPattern.fields.some((field: IFieldType) => field.name === filter.meta.key);
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ 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 @@ -77,10 +76,7 @@ export const buildQueryFromFilters = (
return filters
.filter(filterNegate(negate))
.filter(
(filter) =>
!ignoreFilterIfFieldNotInIndex ||
filterMatchesIndex(filter, indexPattern) ||
customFilterMatchesIndex(filter, indexPattern)
(filter) => !ignoreFilterIfFieldNotInIndex || filterMatchesIndex(filter, indexPattern)
)
.map((filter) => {
return migrateFilter(filter, indexPattern);
Expand Down

0 comments on commit fd418fc

Please sign in to comment.