Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport 2.x] Expose method to register search strategy routes in query enhancement #8282

Merged
merged 4 commits into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/8245.yml
Original file line number Diff line number Diff line change
@@ -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))
6 changes: 4 additions & 2 deletions src/plugins/query_enhancements/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -89,7 +89,9 @@ export class QueryEnhancementsPlugin
});

this.logger.info('queryEnhancements: Setup complete');
return {};
return {
defineSearchStrategyRoute: defineSearchStrategyRouteProvider(this.logger, router),
};
}

public start(core: CoreStart) {
Expand Down
133 changes: 67 additions & 66 deletions src/plugins/query_enhancements/server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<IOpenSearchDashboardsSearchRequest, IDataFrameResponse>
>,
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<IOpenSearchDashboardsSearchRequest, IDataFrameResponse>
) {
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<IOpenSearchDashboardsResponse<any | ResponseError>> => {
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<IOpenSearchDashboardsResponse<any | ResponseError>> => {
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,
});
}
}
}
);
);
};
}

/**
Expand All @@ -108,9 +108,10 @@ export function defineRoutes(
ISearchStrategy<IOpenSearchDashboardsSearchRequest, IDataFrameResponse>
>
) {
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);
}
11 changes: 8 additions & 3 deletions src/plugins/query_enhancements/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<IOpenSearchDashboardsSearchRequest, IDataFrameResponse>
) => void;
}
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface QueryEnhancementsPluginStart {}

Expand Down
Loading