Skip to content

Commit

Permalink
Add simple string query flags options to find method (opensearch-proj…
Browse files Browse the repository at this point in the history
…ect#239)

* Add simple string query flags options to find method

Signed-off-by: Hailong Cui <ihailong@amazon.com>

* fix osd boostrap

Signed-off-by: Hailong Cui <ihailong@amazon.com>

* Add unit test

Signed-off-by: Hailong Cui <ihailong@amazon.com>

---------

Signed-off-by: Hailong Cui <ihailong@amazon.com>
  • Loading branch information
Hailong-am authored Oct 26, 2023
1 parent 1d679f8 commit 7e2ace0
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/core/public/saved_objects/saved_objects_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ export class SavedObjectsClient {
namespaces: 'namespaces',
preference: 'preference',
workspaces: 'workspaces',
flags: 'flags',
};

const currentWorkspaceId = this._getCurrentWorkspace();
Expand Down
2 changes: 2 additions & 0 deletions src/core/server/saved_objects/service/lib/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,7 @@ export class SavedObjectsRepository {
preference,
workspaces,
ACLSearchParams,
flags,
} = options;

if (!type && !typeToNamespacesMap) {
Expand Down Expand Up @@ -963,6 +964,7 @@ export class SavedObjectsRepository {
kueryNode,
workspaces,
ACLSearchParams,
flags,
}),
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ interface QueryParams {
kueryNode?: KueryNode;
workspaces?: string[];
ACLSearchParams?: SavedObjectsFindOptions['ACLSearchParams'];
flags?: string;
}

export function getClauseForReference(reference: HasReferenceQueryParams) {
Expand Down Expand Up @@ -226,6 +227,7 @@ export function getQueryParams({
kueryNode,
workspaces,
ACLSearchParams,
flags,
}: QueryParams) {
const types = getTypes(
registry,
Expand Down Expand Up @@ -269,6 +271,7 @@ export function getQueryParams({
searchFields,
rootSearchFields,
defaultSearchOperator,
flags,
});

if (useMatchPhrasePrefix) {
Expand Down Expand Up @@ -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 } : {}),
},
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ interface GetSearchDslOptions {
kueryNode?: KueryNode;
workspaces?: string[];
ACLSearchParams?: SavedObjectsFindOptions['ACLSearchParams'];
flags?: string;
}

export function getSearchDsl(
Expand All @@ -76,6 +77,7 @@ export function getSearchDsl(
kueryNode,
workspaces,
ACLSearchParams,
flags,
} = options;

if (!type) {
Expand All @@ -100,6 +102,7 @@ export function getSearchDsl(
kueryNode,
workspaces,
ACLSearchParams,
flags,
}),
...getSortingParams(mappings, type, sortField, sortOrder),
};
Expand Down
2 changes: 2 additions & 0 deletions src/core/server/saved_objects/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/workspace/server/workspace_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 7e2ace0

Please sign in to comment.