Skip to content

Commit

Permalink
use type.displayName for search filters and search query (#119025) (#…
Browse files Browse the repository at this point in the history
…119145)

Co-authored-by: Kibana Machine <[email protected]>

Co-authored-by: Pierre Gayvallet <[email protected]>
  • Loading branch information
kibanamachine and pgayvallet authored Nov 19, 2021
1 parent 2d6c10a commit 06d9ffb
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 21 deletions.
54 changes: 41 additions & 13 deletions src/plugins/saved_objects_management/public/lib/parse_query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
});
Expand All @@ -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'],
Expand All @@ -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'],
});
});
});
12 changes: 10 additions & 2 deletions src/plugins/saved_objects_management/public/lib/parse_query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
*/

import { Query } from '@elastic/eui';
import type { SavedObjectManagementTypeInfo } from '../../common';

interface ParsedQuery {
queryText?: string;
visibleTypes?: string[];
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;
Expand All @@ -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<string, string>());
visibleTypes = displayedTypes.map((type) => {
return displayNameToNameMap.get(type) ?? type;
});
}
if (query.ast.getFieldClauses('tag')) {
selectedTags = query.ast.getFieldClauses('tag')[0].value as string[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,10 @@ export class SavedObjectsTable extends Component<SavedObjectsTableProps, SavedOb

fetchCounts = async () => {
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);

Expand Down Expand Up @@ -210,7 +213,7 @@ export class SavedObjectsTable extends Component<SavedObjectsTableProps, SavedOb
debouncedFindObjects = debounce(async () => {
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)
Expand Down Expand Up @@ -404,8 +407,8 @@ export class SavedObjectsTable extends Component<SavedObjectsTableProps, SavedOb

onExportAll = async () => {
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);
Expand Down Expand Up @@ -658,8 +661,8 @@ export class SavedObjectsTable extends Component<SavedObjectsTableProps, SavedOb
};

const filterOptions = allowedTypes.map((type) => ({
value: type.name,
name: type.name,
value: type.displayName,
name: type.displayName,
view: `${type.displayName} (${savedObjectCounts[type.name] || 0})`,
}));

Expand Down

0 comments on commit 06d9ffb

Please sign in to comment.