From 7bb6e0117739fc9541152f49460a39ac6b6d0fd0 Mon Sep 17 00:00:00 2001 From: "huang.jusheng" Date: Fri, 20 Sep 2024 14:01:29 +0800 Subject: [PATCH 1/3] [AdvancedSettings] Fix search does not work for terms with ":" --- .../settings/application/hooks/use_fields.ts | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/packages/kbn-management/settings/application/hooks/use_fields.ts b/packages/kbn-management/settings/application/hooks/use_fields.ts index 829e368812512..d18d1e480f38e 100644 --- a/packages/kbn-management/settings/application/hooks/use_fields.ts +++ b/packages/kbn-management/settings/application/hooks/use_fields.ts @@ -7,7 +7,7 @@ * License v3.0 only", or the "Server Side Public License, v 1". */ -import { Query } from '@elastic/eui'; +import { Ast, Query } from '@elastic/eui'; import { getFieldDefinitions } from '@kbn/management-settings-field-definition'; import { FieldDefinition } from '@kbn/management-settings-types'; import { UiSettingsScope } from '@kbn/core-ui-settings-common'; @@ -28,8 +28,22 @@ export const useFields = (scope: UiSettingsScope, query?: Query): FieldDefinitio isCustom: (key) => isCustomSetting(key, scope), isOverridden: (key) => isOverriddenSetting(key, scope), }); - if (query) { - return Query.execute(query, fields); + if (query && fields.length) { + let ast = Ast.create([]); + + query.ast.clauses.forEach((clause) => { + if (clause.type !== 'field' || Object.keys(fields[0]).includes(clause.field)) { + ast = ast.addClause(clause); + } else { + ast = ast.addClause({ + type: 'term', + match: 'must', + value: `${clause.field}:${clause.value}`, + }); + } + }); + + return Query.execute(new Query(ast, undefined, query.text), fields); } return fields; }; From efc4981f8227fe981df923a16c2d6ebab408cbf5 Mon Sep 17 00:00:00 2001 From: "huang.jusheng" Date: Tue, 24 Sep 2024 17:54:27 +0800 Subject: [PATCH 2/3] fix: check categories field & optimize code --- .../settings/application/hooks/use_fields.ts | 29 +++++++++---------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/packages/kbn-management/settings/application/hooks/use_fields.ts b/packages/kbn-management/settings/application/hooks/use_fields.ts index d18d1e480f38e..34edcf4431bc0 100644 --- a/packages/kbn-management/settings/application/hooks/use_fields.ts +++ b/packages/kbn-management/settings/application/hooks/use_fields.ts @@ -11,6 +11,7 @@ import { Ast, Query } from '@elastic/eui'; import { getFieldDefinitions } from '@kbn/management-settings-field-definition'; import { FieldDefinition } from '@kbn/management-settings-types'; import { UiSettingsScope } from '@kbn/core-ui-settings-common'; +import { Clause } from '@elastic/eui/src/components/search_bar/query/ast'; import { useServices } from '../services'; import { useSettings } from './use_settings'; @@ -28,22 +29,20 @@ export const useFields = (scope: UiSettingsScope, query?: Query): FieldDefinitio isCustom: (key) => isCustomSetting(key, scope), isOverridden: (key) => isOverriddenSetting(key, scope), }); - if (query && fields.length) { - let ast = Ast.create([]); + if (query) { + const clauses: Clause[] = query.ast.clauses.map((clause) => + // If the clause value contains `:` and is not a category filter, add it as a term clause + // This allows searching for settings that include `:` in their names + clause.type === 'field' && clause.field !== 'categories' + ? { + type: 'term', + match: 'must', + value: `${clause.field}:${clause.value}`, + } + : clause + ); - query.ast.clauses.forEach((clause) => { - if (clause.type !== 'field' || Object.keys(fields[0]).includes(clause.field)) { - ast = ast.addClause(clause); - } else { - ast = ast.addClause({ - type: 'term', - match: 'must', - value: `${clause.field}:${clause.value}`, - }); - } - }); - - return Query.execute(new Query(ast, undefined, query.text), fields); + return Query.execute(new Query(Ast.create(clauses), undefined, query.text), fields); } return fields; }; From 2a7f0b4dd444bbc371a04d7e6f35d6436f8dfbdf Mon Sep 17 00:00:00 2001 From: "huang.jusheng" Date: Tue, 24 Sep 2024 18:39:53 +0800 Subject: [PATCH 3/3] fix: use constant of category field --- .../kbn-management/settings/application/hooks/use_fields.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/kbn-management/settings/application/hooks/use_fields.ts b/packages/kbn-management/settings/application/hooks/use_fields.ts index 34edcf4431bc0..f0dc538f54189 100644 --- a/packages/kbn-management/settings/application/hooks/use_fields.ts +++ b/packages/kbn-management/settings/application/hooks/use_fields.ts @@ -13,6 +13,7 @@ import { FieldDefinition } from '@kbn/management-settings-types'; import { UiSettingsScope } from '@kbn/core-ui-settings-common'; import { Clause } from '@elastic/eui/src/components/search_bar/query/ast'; import { useServices } from '../services'; +import { CATEGORY_FIELD } from '../query_input'; import { useSettings } from './use_settings'; /** @@ -33,7 +34,7 @@ export const useFields = (scope: UiSettingsScope, query?: Query): FieldDefinitio const clauses: Clause[] = query.ast.clauses.map((clause) => // If the clause value contains `:` and is not a category filter, add it as a term clause // This allows searching for settings that include `:` in their names - clause.type === 'field' && clause.field !== 'categories' + clause.type === 'field' && clause.field !== CATEGORY_FIELD ? { type: 'term', match: 'must',