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

[Query Parsing] Use unknownFilters as part of the search term #186432

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ describe('getQueryText', () => {
});
});

it('uses unknown fields as part of queryText', () => {
const query = Query.parse('tag:tag-1 unknown:worlds some search');

expect(parseQuery(query, [])).toEqual({
queryText: 'some search unknown:worlds',
Copy link
Member

Choose a reason for hiding this comment

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

is it OK to invert the search?

Copy link
Member Author

Choose a reason for hiding this comment

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

@afharo It looks like the order of terms doesn't matter

image

Copy link
Member

Choose a reason for hiding this comment

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

FMI, do we support double-quote searches here?

selectedTags: ['tag-1'],
});
});

it('parses the types when provided types are empty', () => {
const query = Query.parse('type:(index-pattern or dashboard) kibana');

Expand Down
52 changes: 38 additions & 14 deletions src/plugins/saved_objects_management/public/lib/parse_query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,55 @@ interface ParsedQuery {
selectedTags?: string[];
}

export function parseQuery(query: Query, types: SavedObjectManagementTypeInfo[]): ParsedQuery {
enum FieldClause {
TYPE = 'type',
TAG = 'tag',
}
const fieldClauseValues: string[] = Object.values(FieldClause);

export function parseQuery(query: Query, typeInfos: SavedObjectManagementTypeInfo[]): ParsedQuery {
let queryText: string | undefined;
let visibleTypes: string[] | undefined;
let selectedTags: string[] | undefined;

if (query) {
if (query.ast.getTermClauses().length) {
queryText = query.ast
.getTermClauses()
.map((clause: any) => clause.value)
.join(' ');
const termClauses = query.ast.getTermClauses();
if (termClauses.length > 0) {
queryText = termClauses.map(({ value }) => value).join(' ');
}
if (query.ast.getFieldClauses('type')) {
const displayedTypes = query.ast.getFieldClauses('type')[0].value as string[];
const displayNameToNameMap = types.reduce((map, type) => {
map.set(type.displayName, type.name);

const typeFieldClauses = query.ast.getFieldClauses(FieldClause.TYPE);
if (typeFieldClauses && typeFieldClauses.length > 0) {
const displayNameToNameMap = typeInfos.reduce((map, typeInfo) => {
map.set(typeInfo.displayName, typeInfo.name);
return map;
}, new Map<string, string>());
visibleTypes = displayedTypes.map((type) => {
return displayNameToNameMap.get(type) ?? type;
const values = typeFieldClauses[0].value;
const typeFieldClausesValues = Array.isArray(values) ? values : [values];
visibleTypes = typeFieldClausesValues.map((typeInfo) => {
typeInfo = typeInfo.toString();
return displayNameToNameMap.get(typeInfo) ?? typeInfo;
});
}
if (query.ast.getFieldClauses('tag')) {
selectedTags = query.ast.getFieldClauses('tag')[0].value as string[];

const tagFieldClauses = query.ast.getFieldClauses(FieldClause.TAG);
if (tagFieldClauses && tagFieldClauses.length > 0) {
const values = tagFieldClauses[0].value;
const tagFieldClausesValues = Array.isArray(values) ? values : [values];
selectedTags = tagFieldClausesValues.map((t) => {
return t.toString();
});
}

// check for unknown filters
query.ast.getFieldClauses().forEach((clause) => {
const { type: clauseType, field, value } = clause;
if (clauseType === 'field' && !Array.isArray(value) && !fieldClauseValues.includes(field)) {
// Unknown filters must be used as part of the search term.
// Example: "remote:logs" is not a filter, it is a valid search term.
queryText = `${queryText ?? ''} ${field}:${value}`;
}
});
}

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ describe('parseSearchParams', () => {
});
});

it('handles unknowns field clauses', () => {
it('adds unknowns field clauses to the search term', () => {
const searchParams = parseSearchParams('tag:foo unknown:bar hello');
expect(searchParams).toEqual({
term: 'hello',
term: 'hello unknown:bar',
filters: {
tags: ['foo'],
unknowns: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,21 @@ export const parseSearchParams = (term: string): ParsedSearchParams => {
};
}

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

const unknownFilters = [...filterValues.entries()]
.filter(([key]) => !knownFilters.includes(key))
.reduce((unknowns, [key, value]) => {
.reduce((unknowns, [key, values]) => {
// Unknown filters must be used as part of the search term.
// Example: "remote:logs" is not a filter, it is a valid search term.
if (Array.isArray(values) && values.length === 1) {
searchTerm = `${searchTerm} ${key}:${values[0]}`;
}

return {
...unknowns,
[key]: value,
[key]: values,
};
}, {} as Record<string, FilterValues>);

Expand Down