From 7e2ace049ee22447c5bb54061d298276b1511f8b Mon Sep 17 00:00:00 2001 From: Hailong Cui Date: Thu, 26 Oct 2023 10:52:17 +0800 Subject: [PATCH] Add simple string query flags options to find method (#239) * Add simple string query flags options to find method Signed-off-by: Hailong Cui * fix osd boostrap Signed-off-by: Hailong Cui * Add unit test Signed-off-by: Hailong Cui --------- Signed-off-by: Hailong Cui --- .../saved_objects/saved_objects_client.ts | 1 + .../saved_objects/service/lib/repository.ts | 2 ++ .../lib/search_dsl/query_params.test.ts | 19 +++++++++++++++++++ .../service/lib/search_dsl/query_params.ts | 6 ++++++ .../service/lib/search_dsl/search_dsl.ts | 3 +++ src/core/server/saved_objects/types.ts | 2 ++ .../workspace/server/workspace_client.ts | 2 ++ 7 files changed, 35 insertions(+) diff --git a/src/core/public/saved_objects/saved_objects_client.ts b/src/core/public/saved_objects/saved_objects_client.ts index f1f3b87c6dc7..7681117c7977 100644 --- a/src/core/public/saved_objects/saved_objects_client.ts +++ b/src/core/public/saved_objects/saved_objects_client.ts @@ -373,6 +373,7 @@ export class SavedObjectsClient { namespaces: 'namespaces', preference: 'preference', workspaces: 'workspaces', + flags: 'flags', }; const currentWorkspaceId = this._getCurrentWorkspace(); diff --git a/src/core/server/saved_objects/service/lib/repository.ts b/src/core/server/saved_objects/service/lib/repository.ts index a8fd54ef6cfe..3ee7889475d2 100644 --- a/src/core/server/saved_objects/service/lib/repository.ts +++ b/src/core/server/saved_objects/service/lib/repository.ts @@ -888,6 +888,7 @@ export class SavedObjectsRepository { preference, workspaces, ACLSearchParams, + flags, } = options; if (!type && !typeToNamespacesMap) { @@ -963,6 +964,7 @@ export class SavedObjectsRepository { kueryNode, workspaces, ACLSearchParams, + flags, }), }, }; diff --git a/src/core/server/saved_objects/service/lib/search_dsl/query_params.test.ts b/src/core/server/saved_objects/service/lib/search_dsl/query_params.test.ts index 518e2ff56d0e..a4c1e602829c 100644 --- a/src/core/server/saved_objects/service/lib/search_dsl/query_params.test.ts +++ b/src/core/server/saved_objects/service/lib/search_dsl/query_params.test.ts @@ -506,6 +506,25 @@ describe('#getQueryParams', () => { ); }); }); + + describe('`flags` parameter', () => { + it('does not include flags when `flags` is not specified', () => { + const result = getQueryParams({ + registry, + search, + }); + expectResult(result, expect.not.objectContaining({ flags: expect.anything() })); + }); + + it('includes flags when specified', () => { + const result = getQueryParams({ + registry, + search, + flags: 'abc', + }); + expectResult(result, expect.objectContaining({ flags: expect.stringMatching('abc') })); + }); + }); }); describe('when using prefix search (query.bool.should)', () => { diff --git a/src/core/server/saved_objects/service/lib/search_dsl/query_params.ts b/src/core/server/saved_objects/service/lib/search_dsl/query_params.ts index 96da76df5a68..4f2170abad22 100644 --- a/src/core/server/saved_objects/service/lib/search_dsl/query_params.ts +++ b/src/core/server/saved_objects/service/lib/search_dsl/query_params.ts @@ -168,6 +168,7 @@ interface QueryParams { kueryNode?: KueryNode; workspaces?: string[]; ACLSearchParams?: SavedObjectsFindOptions['ACLSearchParams']; + flags?: string; } export function getClauseForReference(reference: HasReferenceQueryParams) { @@ -226,6 +227,7 @@ export function getQueryParams({ kueryNode, workspaces, ACLSearchParams, + flags, }: QueryParams) { const types = getTypes( registry, @@ -269,6 +271,7 @@ export function getQueryParams({ searchFields, rootSearchFields, defaultSearchOperator, + flags, }); if (useMatchPhrasePrefix) { @@ -417,18 +420,21 @@ const getSimpleQueryStringClause = ({ searchFields, rootSearchFields, defaultSearchOperator, + flags, }: { search: string; types: string[]; searchFields?: string[]; rootSearchFields?: string[]; defaultSearchOperator?: string; + flags?: string; }) => { return { simple_query_string: { query: search, ...getSimpleQueryStringTypeFields(types, searchFields, rootSearchFields), ...(defaultSearchOperator ? { default_operator: defaultSearchOperator } : {}), + ...(flags ? { flags } : {}), }, }; }; diff --git a/src/core/server/saved_objects/service/lib/search_dsl/search_dsl.ts b/src/core/server/saved_objects/service/lib/search_dsl/search_dsl.ts index a93e134a9757..ba8caff62401 100644 --- a/src/core/server/saved_objects/service/lib/search_dsl/search_dsl.ts +++ b/src/core/server/saved_objects/service/lib/search_dsl/search_dsl.ts @@ -55,6 +55,7 @@ interface GetSearchDslOptions { kueryNode?: KueryNode; workspaces?: string[]; ACLSearchParams?: SavedObjectsFindOptions['ACLSearchParams']; + flags?: string; } export function getSearchDsl( @@ -76,6 +77,7 @@ export function getSearchDsl( kueryNode, workspaces, ACLSearchParams, + flags, } = options; if (!type) { @@ -100,6 +102,7 @@ export function getSearchDsl( kueryNode, workspaces, ACLSearchParams, + flags, }), ...getSortingParams(mappings, type, sortField, sortOrder), }; diff --git a/src/core/server/saved_objects/types.ts b/src/core/server/saved_objects/types.ts index aa2d1baf85de..90ffcd311fca 100644 --- a/src/core/server/saved_objects/types.ts +++ b/src/core/server/saved_objects/types.ts @@ -92,6 +92,8 @@ export interface SavedObjectsFindOptions { search?: string; /** The fields to perform the parsed query against. See OpenSearch Simple Query String `fields` argument for more information */ searchFields?: string[]; + /** The enabled operators for OpenSearch Simple Query String. See OpenSearch Simple Query String `flags` argument for more information */ + flags?: string; /** * The fields to perform the parsed query against. Unlike the `searchFields` argument, these are expected to be root fields and will not * be modified. If used in conjunction with `searchFields`, both are concatenated together. diff --git a/src/plugins/workspace/server/workspace_client.ts b/src/plugins/workspace/server/workspace_client.ts index 799e055f256c..8ce3f929dd56 100644 --- a/src/plugins/workspace/server/workspace_client.ts +++ b/src/plugins/workspace/server/workspace_client.ts @@ -296,6 +296,7 @@ export class WorkspaceClientWithSavedObject implements IWorkspaceDBImpl { type: WORKSPACE_TYPE, search: attributes.name, searchFields: ['name'], + flags: 'NONE', // disable all operators, treat workspace as literal string } ); if (existingWorkspaceRes && existingWorkspaceRes.total > 0) { @@ -452,6 +453,7 @@ export class WorkspaceClientWithSavedObject implements IWorkspaceDBImpl { search: attributes.name, searchFields: ['name'], fields: ['_id'], + flags: 'NONE', // disable all operators, treat workspace as literal string }); if (existingWorkspaceRes && existingWorkspaceRes.total > 0) { throw new Error(DUPLICATE_WORKSPACE_NAME_ERROR);