-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[UnifiedFieldList] Allow wildcards in field search (#155540)
Closes #97459 ## Summary This PR allows to search by field names with wildcard. <img width="318" alt="Screenshot 2023-04-24 at 10 47 18" src="https://user-images.githubusercontent.com/1415710/233946401-5588b757-9ba7-4e4f-adda-4ad4a936f528.png"> ### Checklist - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios
- Loading branch information
Showing
7 changed files
with
158 additions
and
6 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
...ld_list/public/components/field_item_button/__snapshots__/field_item_button.test.tsx.snap
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/plugins/unified_field_list/public/utils/field_name_wildcard_matcher.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { type DataViewField } from '@kbn/data-views-plugin/common'; | ||
import { fieldNameWildcardMatcher } from './field_name_wildcard_matcher'; | ||
|
||
describe('UnifiedFieldList fieldNameWildcardMatcher()', () => { | ||
it('should work correctly', async () => { | ||
expect(fieldNameWildcardMatcher({ displayName: 'test' } as DataViewField, 'no')).toBe(false); | ||
expect( | ||
fieldNameWildcardMatcher({ displayName: 'test', name: 'yes' } as DataViewField, 'yes') | ||
).toBe(true); | ||
|
||
const search = 'test*ue'; | ||
expect(fieldNameWildcardMatcher({ displayName: 'test' } as DataViewField, search)).toBe(false); | ||
expect(fieldNameWildcardMatcher({ displayName: 'test.value' } as DataViewField, search)).toBe( | ||
true | ||
); | ||
expect(fieldNameWildcardMatcher({ name: 'test.this_value' } as DataViewField, search)).toBe( | ||
true | ||
); | ||
expect(fieldNameWildcardMatcher({ name: 'message.test' } as DataViewField, search)).toBe(false); | ||
expect( | ||
fieldNameWildcardMatcher({ name: 'test.this_value.maybe' } as DataViewField, search) | ||
).toBe(false); | ||
expect( | ||
fieldNameWildcardMatcher({ name: 'test.this_value.maybe' } as DataViewField, `${search}*`) | ||
).toBe(true); | ||
expect( | ||
fieldNameWildcardMatcher({ name: 'test.this_value.maybe' } as DataViewField, '*value*') | ||
).toBe(true); | ||
}); | ||
}); |
34 changes: 34 additions & 0 deletions
34
src/plugins/unified_field_list/public/utils/field_name_wildcard_matcher.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { escapeRegExp, memoize } from 'lodash'; | ||
|
||
const makeRegEx = memoize(function makeRegEx(glob: string) { | ||
const globRegex = glob.split('*').map(escapeRegExp).join('.*'); | ||
return new RegExp(globRegex.includes('*') ? `^${globRegex}$` : globRegex, 'i'); | ||
}); | ||
|
||
/** | ||
* Checks if field displayName or name matches the provided search string. | ||
* The search string can have wildcard. | ||
* @param field | ||
* @param fieldSearchHighlight | ||
*/ | ||
export const fieldNameWildcardMatcher = ( | ||
field: { name: string; displayName?: string }, | ||
fieldSearchHighlight: string | ||
): boolean => { | ||
if (!fieldSearchHighlight) { | ||
return false; | ||
} | ||
|
||
return ( | ||
(!!field.displayName && makeRegEx(fieldSearchHighlight).test(field.displayName)) || | ||
makeRegEx(fieldSearchHighlight).test(field.name) | ||
); | ||
}; |