diff --git a/src/plugins/saved_objects_management/public/lib/parse_query.test.ts b/src/plugins/saved_objects_management/public/lib/parse_query.test.ts index 3ab2d14ba491d..76335aa1febc7 100644 --- a/src/plugins/saved_objects_management/public/lib/parse_query.test.ts +++ b/src/plugins/saved_objects_management/public/lib/parse_query.test.ts @@ -7,30 +7,29 @@ */ import { Query } from '@elastic/eui'; +import type { SavedObjectManagementTypeInfo } from '../../common'; import { parseQuery } from './parse_query'; +const createType = (name: string, displayName?: string): SavedObjectManagementTypeInfo => ({ + name, + displayName: displayName ?? name, + hidden: false, + namespaceType: 'multiple', +}); + describe('getQueryText', () => { it('parses the query text', () => { const query = Query.parse('some search'); - expect(parseQuery(query)).toEqual({ + expect(parseQuery(query, [])).toEqual({ queryText: 'some search', }); }); - it('parses the types', () => { - const query = Query.parse('type:(index-pattern or dashboard) kibana'); - - expect(parseQuery(query)).toEqual({ - queryText: 'kibana', - visibleTypes: ['index-pattern', 'dashboard'], - }); - }); - it('parses the tags', () => { const query = Query.parse('tag:(tag-1 or tag-2) kibana'); - expect(parseQuery(query)).toEqual({ + expect(parseQuery(query, [])).toEqual({ queryText: 'kibana', selectedTags: ['tag-1', 'tag-2'], }); @@ -39,7 +38,7 @@ describe('getQueryText', () => { it('parses all the fields', () => { const query = Query.parse('tag:(tag-1 or tag-2) type:(index-pattern) kibana'); - expect(parseQuery(query)).toEqual({ + expect(parseQuery(query, [])).toEqual({ queryText: 'kibana', visibleTypes: ['index-pattern'], selectedTags: ['tag-1', 'tag-2'], @@ -49,8 +48,37 @@ describe('getQueryText', () => { it('does not fail on unknown fields', () => { const query = Query.parse('unknown:(hello or dolly) some search'); - expect(parseQuery(query)).toEqual({ + expect(parseQuery(query, [])).toEqual({ queryText: 'some search', }); }); + + it('parses the types when provided types are empty', () => { + const query = Query.parse('type:(index-pattern or dashboard) kibana'); + + expect(parseQuery(query, [])).toEqual({ + queryText: 'kibana', + visibleTypes: ['index-pattern', 'dashboard'], + }); + }); + + it('maps displayName to name when parsing the types', () => { + const query = Query.parse('type:(i-p or dash) kibana'); + const types = [createType('index-pattern', 'i-p'), createType('dashboard', 'dash')]; + + expect(parseQuery(query, types)).toEqual({ + queryText: 'kibana', + visibleTypes: ['index-pattern', 'dashboard'], + }); + }); + + it('maps displayName to name even when some types are missing', () => { + const query = Query.parse('type:(i-p or dashboard) kibana'); + const types = [createType('index-pattern', 'i-p')]; + + expect(parseQuery(query, types)).toEqual({ + queryText: 'kibana', + visibleTypes: ['index-pattern', 'dashboard'], + }); + }); }); diff --git a/src/plugins/saved_objects_management/public/lib/parse_query.ts b/src/plugins/saved_objects_management/public/lib/parse_query.ts index bef60a038353e..9676ad22a93be 100644 --- a/src/plugins/saved_objects_management/public/lib/parse_query.ts +++ b/src/plugins/saved_objects_management/public/lib/parse_query.ts @@ -7,6 +7,7 @@ */ import { Query } from '@elastic/eui'; +import type { SavedObjectManagementTypeInfo } from '../../common'; interface ParsedQuery { queryText?: string; @@ -14,7 +15,7 @@ interface ParsedQuery { selectedTags?: string[]; } -export function parseQuery(query: Query): ParsedQuery { +export function parseQuery(query: Query, types: SavedObjectManagementTypeInfo[]): ParsedQuery { let queryText: string | undefined; let visibleTypes: string[] | undefined; let selectedTags: string[] | undefined; @@ -27,7 +28,14 @@ export function parseQuery(query: Query): ParsedQuery { .join(' '); } if (query.ast.getFieldClauses('type')) { - visibleTypes = query.ast.getFieldClauses('type')[0].value as string[]; + const displayedTypes = query.ast.getFieldClauses('type')[0].value as string[]; + const displayNameToNameMap = types.reduce((map, type) => { + map.set(type.displayName, type.name); + return map; + }, new Map()); + visibleTypes = displayedTypes.map((type) => { + return displayNameToNameMap.get(type) ?? type; + }); } if (query.ast.getFieldClauses('tag')) { selectedTags = query.ast.getFieldClauses('tag')[0].value as string[]; diff --git a/src/plugins/saved_objects_management/public/management_section/objects_table/saved_objects_table.tsx b/src/plugins/saved_objects_management/public/management_section/objects_table/saved_objects_table.tsx index a73a695c5b39d..d4b3ecac5b8db 100644 --- a/src/plugins/saved_objects_management/public/management_section/objects_table/saved_objects_table.tsx +++ b/src/plugins/saved_objects_management/public/management_section/objects_table/saved_objects_table.tsx @@ -149,7 +149,10 @@ export class SavedObjectsTable extends Component { const { taggingApi } = this.props; - const { queryText, visibleTypes, selectedTags } = parseQuery(this.state.activeQuery); + const { queryText, visibleTypes, selectedTags } = parseQuery( + this.state.activeQuery, + this.props.allowedTypes + ); const allowedTypes = this.props.allowedTypes.map((type) => type.name); @@ -210,7 +213,7 @@ export class SavedObjectsTable extends Component { const { activeQuery: query, page, perPage } = this.state; const { notifications, http, allowedTypes, taggingApi } = this.props; - const { queryText, visibleTypes, selectedTags } = parseQuery(query); + const { queryText, visibleTypes, selectedTags } = parseQuery(query, allowedTypes); const searchTypes = allowedTypes .map((type) => type.name) @@ -404,8 +407,8 @@ export class SavedObjectsTable extends Component { const { exportAllSelectedOptions, isIncludeReferencesDeepChecked, activeQuery } = this.state; - const { notifications, http, taggingApi } = this.props; - const { queryText, selectedTags } = parseQuery(activeQuery); + const { notifications, http, taggingApi, allowedTypes } = this.props; + const { queryText, selectedTags } = parseQuery(activeQuery, allowedTypes); const exportTypes = Object.entries(exportAllSelectedOptions).reduce((accum, [id, selected]) => { if (selected) { accum.push(id); @@ -658,8 +661,8 @@ export class SavedObjectsTable extends Component ({ - value: type.name, - name: type.name, + value: type.displayName, + name: type.displayName, view: `${type.displayName} (${savedObjectCounts[type.name] || 0})`, }));