Skip to content

Commit

Permalink
[Backport 2.x] Expose method to register search strategy routes in qu…
Browse files Browse the repository at this point in the history
…ery enhancement (#8282)

* exposed method to register search strategy routes

Signed-off-by: Amardeepsingh Siglani <amardeep7194@gmail.com>

* Changeset file for PR #8245 created/updated

* addressed comment

Signed-off-by: Amardeepsingh Siglani <amardeep7194@gmail.com>

* fix linter error

Signed-off-by: Amardeepsingh Siglani <amardeep7194@gmail.com>

---------

Signed-off-by: Amardeepsingh Siglani <amardeep7194@gmail.com>
Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com>
  • Loading branch information
amsiglan and opensearch-changeset-bot[bot] committed Sep 21, 2024
1 parent 91e4fe5 commit f4fa767
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 71 deletions.
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

0 comments on commit f4fa767

Please sign in to comment.