From f4fa7677d3913d0e992cae43e4d59c482f4c87ef Mon Sep 17 00:00:00 2001 From: Amardeepsingh Siglani Date: Fri, 20 Sep 2024 18:13:45 -0700 Subject: [PATCH] [Backport 2.x] Expose method to register search strategy routes in query enhancement (#8282) * exposed method to register search strategy routes Signed-off-by: Amardeepsingh Siglani * Changeset file for PR #8245 created/updated * addressed comment Signed-off-by: Amardeepsingh Siglani * fix linter error Signed-off-by: Amardeepsingh Siglani --------- Signed-off-by: Amardeepsingh Siglani Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com> --- changelogs/fragments/8245.yml | 2 + .../query_enhancements/server/plugin.ts | 6 +- .../query_enhancements/server/routes/index.ts | 133 +++++++++--------- .../query_enhancements/server/types.ts | 11 +- 4 files changed, 81 insertions(+), 71 deletions(-) create mode 100644 changelogs/fragments/8245.yml diff --git a/changelogs/fragments/8245.yml b/changelogs/fragments/8245.yml new file mode 100644 index 00000000000..33a9f71bbd9 --- /dev/null +++ b/changelogs/fragments/8245.yml @@ -0,0 +1,2 @@ +fix: +- Expose method to register search strategy routes in query enhancement ([#8245](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/8245)) \ No newline at end of file diff --git a/src/plugins/query_enhancements/server/plugin.ts b/src/plugins/query_enhancements/server/plugin.ts index 78989696155..4a624bfccb1 100644 --- a/src/plugins/query_enhancements/server/plugin.ts +++ b/src/plugins/query_enhancements/server/plugin.ts @@ -15,7 +15,7 @@ import { } from '../../../core/server'; import { SEARCH_STRATEGY } from '../common'; import { ConfigSchema } from '../common/config'; -import { defineRoutes } from './routes'; +import { defineRoutes, defineSearchStrategyRouteProvider } from './routes'; import { pplSearchStrategyProvider, pplRawSearchStrategyProvider, @@ -89,7 +89,9 @@ export class QueryEnhancementsPlugin }); this.logger.info('queryEnhancements: Setup complete'); - return {}; + return { + defineSearchStrategyRoute: defineSearchStrategyRouteProvider(this.logger, router), + }; } public start(core: CoreStart) { diff --git a/src/plugins/query_enhancements/server/routes/index.ts b/src/plugins/query_enhancements/server/routes/index.ts index 7d64e2c12b1..a766bea85fa 100644 --- a/src/plugins/query_enhancements/server/routes/index.ts +++ b/src/plugins/query_enhancements/server/routes/index.ts @@ -12,81 +12,81 @@ import { } from '../../../../core/server'; import { IDataFrameResponse, IOpenSearchDashboardsSearchRequest } from '../../../data/common'; import { ISearchStrategy } from '../../../data/server'; -import { API, SEARCH_STRATEGY } from '../../common'; +import { API } from '../../common'; import { registerQueryAssistRoutes } from './query_assist'; import { registerDataSourceConnectionsRoutes } from './data_source_connection'; /** - * Defines a route for a specific search strategy. + * @experimental * - * @experimental This function is experimental and might change in future releases. + * This method creates a function that will setup the routes for a search strategy by encapsulating the + * logger and router instances. * * @param logger - The logger instance. * @param router - The router instance. - * @param searchStrategies - The available search strategies. - * @param searchStrategyId - The ID of the search strategy to use. - * - * @example - * API Request Body: - * ```json - * { - * "query": { - * "query": "SELECT * FROM my_index", - * "language": "sql", - * "dataset": { - * "id": "my_dataset_id", - * "title": "My Dataset" - * }, - * "format": "json" - * }, - * @experimental - * "aggConfig": { - * // Optional aggregation configuration - * }, - * } - * ``` */ -function defineRoute( - logger: Logger, - router: IRouter, - searchStrategies: Record< - string, - ISearchStrategy - >, - searchStrategyId: string -) { - const path = `${API.SEARCH}/${searchStrategyId}`; - router.post( - { - path, - validate: { - body: schema.object({ - query: schema.object({ - query: schema.string(), - language: schema.string(), - dataset: schema.nullable(schema.object({}, { unknowns: 'allow' })), - format: schema.string(), +export function defineSearchStrategyRouteProvider(logger: Logger, router: IRouter) { + /** + * @param id - The ID of the search strategy to use. + * @param searchStrategy + * + * @example + * API Request Body: + * ```json + * { + * "query": { + * "query": "SELECT * FROM my_index", + * "language": "sql", + * "dataset": { + * "id": "my_dataset_id", + * "title": "My Dataset" + * }, + * "format": "json" + * }, + * @experimental + * "aggConfig": { + * // Optional aggregation configuration + * }, + * @deprecated + * "df": { + * // Optional data frame configuration + * } + * } + * ``` + */ + return function ( + id: string, + searchStrategy: ISearchStrategy + ) { + const path = `${API.SEARCH}/${id}`; + router.post( + { + path, + validate: { + body: schema.object({ + query: schema.object({ + query: schema.string(), + language: schema.string(), + dataset: schema.nullable(schema.object({}, { unknowns: 'allow' })), + format: schema.string(), + }), + aggConfig: schema.nullable(schema.object({}, { unknowns: 'allow' })), }), - aggConfig: schema.nullable(schema.object({}, { unknowns: 'allow' })), - }), + }, }, - }, - async (context, req, res): Promise> => { - try { - const queryRes: IDataFrameResponse = await searchStrategies[searchStrategyId].search( - context, - req as any, - {} - ); - return res.ok({ body: { ...queryRes } }); - } catch (err) { - return res.custom({ - statusCode: err.name, - body: err.message, - }); + async (context, req, res): Promise> => { + try { + const queryRes: IDataFrameResponse = await searchStrategy.search(context, req as any, {}); + return res.ok({ body: { ...queryRes } }); + } catch (err) { + return res.custom({ + statusCode: err.name, + body: err.message, + }); + } } - } - ); + ); + }; } /** @@ -108,9 +108,10 @@ export function defineRoutes( ISearchStrategy > ) { - defineRoute(logger, router, searchStrategies, SEARCH_STRATEGY.PPL); - defineRoute(logger, router, searchStrategies, SEARCH_STRATEGY.SQL); - defineRoute(logger, router, searchStrategies, SEARCH_STRATEGY.SQL_ASYNC); + const defineRoute = defineSearchStrategyRouteProvider(logger, router); + Object.entries(searchStrategies).forEach(([id, strategy]) => { + defineRoute(id, strategy); + }); registerDataSourceConnectionsRoutes(router, client); registerQueryAssistRoutes(router); } diff --git a/src/plugins/query_enhancements/server/types.ts b/src/plugins/query_enhancements/server/types.ts index 5e8ab7987de..b8694cccab8 100644 --- a/src/plugins/query_enhancements/server/types.ts +++ b/src/plugins/query_enhancements/server/types.ts @@ -3,11 +3,16 @@ * SPDX-License-Identifier: Apache-2.0 */ -import { PluginSetup } from 'src/plugins/data/server'; +import { ISearchStrategy, PluginSetup } from 'src/plugins/data/server'; import { DataSourcePluginSetup } from 'src/plugins/data_source/server'; +import { IDataFrameResponse, IOpenSearchDashboardsSearchRequest } from '../../data/common'; -// eslint-disable-next-line @typescript-eslint/no-empty-interface -export interface QueryEnhancementsPluginSetup {} +export interface QueryEnhancementsPluginSetup { + defineSearchStrategyRoute: ( + id: string, + searchStrategy: ISearchStrategy + ) => void; +} // eslint-disable-next-line @typescript-eslint/no-empty-interface export interface QueryEnhancementsPluginStart {}