Skip to content

Commit

Permalink
[Search Source] Fix field filtering (#97189)
Browse files Browse the repository at this point in the history
* [Search Source] Fix field filtering

* Add more use-cases for source filtering

* Add more use-cases

* Change filtering to use fieldWildcardFilter

Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
Maja Grubic and kibanamachine authored Apr 20, 2021
1 parent e6b0f80 commit ec4b20e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -418,10 +418,16 @@ describe('SearchSource', () => {
docvalueFields: [],
}),
} as unknown) as IndexPattern);
searchSource.setField('fields', ['hello', 'foo']);

searchSource.setField('fields', [
'hello',
'foo-bar',
'foo--bar',
'fooo',
'somethingfoo',
'xxfxxoxxo',
]);
const request = searchSource.getSearchRequestBody();
expect(request.fields).toEqual(['hello']);
expect(request.fields).toEqual(['hello', 'fooo', 'somethingfoo', 'xxfxxoxxo']);
});

test('request all fields from index pattern except the ones specified with source filters', async () => {
Expand Down
13 changes: 5 additions & 8 deletions src/plugins/data/common/search/search_source/search_source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -635,18 +635,15 @@ export class SearchSource {
if (!sourceFilters || sourceFilters.excludes?.length === 0 || bodyFields.length === 0) {
return bodyFields;
}
const metaFields = this.dependencies.getConfig(UI_SETTINGS.META_FIELDS);
const sourceFiltersValues = sourceFilters.excludes;
const wildcardField = bodyFields.find(
(el: SearchFieldValue) => el === '*' || (el as Record<string, string>).field === '*'
);
const filterSourceFields = (fieldName: string) => {
return (
fieldName &&
!sourceFiltersValues.some((sourceFilter) => fieldName.match(sourceFilter)) &&
!metaFields.includes(fieldName)
);
};
const filter = fieldWildcardFilter(
sourceFiltersValues,
this.dependencies.getConfig(UI_SETTINGS.META_FIELDS)
);
const filterSourceFields = (fieldName: string) => fieldName && filter(fieldName);
if (!wildcardField) {
// we already have an explicit list of fields, so we just remove source filters from that list
return bodyFields.filter((fld: SearchFieldValue) =>
Expand Down

0 comments on commit ec4b20e

Please sign in to comment.