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

[Global Search, Saved Objects Management] Use new parse option to specify recognized fields #190464

Merged
merged 7 commits into from
Sep 19, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,9 @@ export class SavedObjectFinderUi extends React.Component<
box: {
incremental: true,
'data-test-subj': 'savedObjectFinderSearchInput',
schema: {
recognizedFields: ['type', 'tag'],
},
},
filters: this.props.showFilter
? [
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,12 @@ export class Table extends PureComponent<TableProps, TableState> {
<Fragment>
{activeActionContents}
<EuiSearchBar
box={{ 'data-test-subj': 'savedObjectSearchBar' }}
box={{
'data-test-subj': 'savedObjectSearchBar',
schema: {
recognizedFields: ['type', 'tag'],
},
}}
filters={filters as any}
onChange={this.onChange}
defaultQuery={this.props.initialQuery}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ describe('parseSearchParams', () => {
const searchParams = parseSearchParams('tag:((()^invalid');
expect(searchParams).toEqual({
term: 'tag:((()^invalid',
filters: {
unknowns: {},
},
filters: {},
});
});

Expand All @@ -33,7 +31,6 @@ describe('parseSearchParams', () => {
expect(searchParams.filters).toEqual({
tags: undefined,
types: undefined,
unknowns: {},
});
});

Expand All @@ -44,20 +41,16 @@ describe('parseSearchParams', () => {
filters: {
tags: ['foo', 'dolly'],
types: ['bar'],
unknowns: {},
},
});
});

it('handles unknowns field clauses', () => {
Copy link
Member Author

@tsullivan tsullivan Sep 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my comment below for explanation

it('considers unknown field clauses to be part of the raw search term', () => {
const searchParams = parseSearchParams('tag:foo unknown:bar hello');
expect(searchParams).toEqual({
term: 'hello',
term: 'unknown:bar hello',
filters: {
tags: ['foo'],
unknowns: {
unknown: ['bar'],
},
},
});
});
Expand All @@ -69,7 +62,6 @@ describe('parseSearchParams', () => {
filters: {
tags: ['foo', 'bar'],
types: ['dash', 'board'],
unknowns: {},
},
});
});
Expand All @@ -81,7 +73,6 @@ describe('parseSearchParams', () => {
filters: {
tags: ['42', 'true'],
types: ['69', 'false'],
unknowns: {},
},
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,24 @@ const aliasMap = {
};

export const parseSearchParams = (term: string): ParsedSearchParams => {
const recognizedFields = knownFilters.concat(...Object.values(aliasMap));
let query: Query;

try {
query = Query.parse(term);
query = Query.parse(term, {
schema: { recognizedFields },
});
} catch (e) {
// if the query fails to parse, we just perform the search against the raw search term.
return {
term,
filters: {
unknowns: {},
},
filters: {},
};
}

const searchTerm = getSearchTerm(query);
const filterValues = applyAliases(getFieldValueMap(query), aliasMap);

const unknownFilters = [...filterValues.entries()]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the previous code, nothing was handling the unknowns field from the result. This PR takes the opinion that pulling out unknown field clauses and making them unusable is not the user's intention.

.filter(([key]) => !knownFilters.includes(key))
.reduce((unknowns, [key, value]) => {
return {
...unknowns,
[key]: value,
};
}, {} as Record<string, FilterValues>);

const tags = filterValues.get('tag');
const types = filterValues.get('type');

Expand All @@ -51,7 +43,6 @@ export const parseSearchParams = (term: string): ParsedSearchParams => {
filters: {
tags: tags ? valuesToString(tags) : undefined,
types: types ? valuesToString(types) : undefined,
unknowns: unknownFilters,
},
};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,5 @@ export interface ParsedSearchParams {
* Aggregation of `type` and `types` field clauses
*/
types?: FilterValues<string>;
/**
* All unknown field clauses
*/
unknowns: Record<string, FilterValues>;
};
}