search-backend-module-elasticsearch: switch translator customization to extension point
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
@@ -4,4 +4,4 @@
|
||||
|
||||
Index templates can now be configured through configuration under the `search.elasticsearch.indexTemplates`. In addition, the `ElasticSearchSearchEngine.fromConfig` now also accepts a `LoggerService` as the `logger` option as well as a new `translator` option.
|
||||
|
||||
The alpha `searchModuleElasticsearchEngine` export no longer accepts options.
|
||||
The alpha `searchModuleElasticsearchEngine` export no longer accepts options and a new alpha `elasticsearchTranslatorExtensionPoint` export has been added which lets you customize the query translator.
|
||||
|
||||
@@ -4,6 +4,17 @@
|
||||
|
||||
```ts
|
||||
import { BackendFeature } from '@backstage/backend-plugin-api';
|
||||
import { ElasticSearchQueryTranslator } from '@backstage/plugin-search-backend-module-elasticsearch';
|
||||
import { ExtensionPoint } from '@backstage/backend-plugin-api';
|
||||
|
||||
// @alpha (undocumented)
|
||||
export interface ElasticSearchQueryTranslatorExtensionPoint {
|
||||
// (undocumented)
|
||||
setTranslator(translator: ElasticSearchQueryTranslator): void;
|
||||
}
|
||||
|
||||
// @alpha
|
||||
export const elasticsearchTranslatorExtensionPoint: ExtensionPoint<ElasticSearchQueryTranslatorExtensionPoint>;
|
||||
|
||||
// @alpha
|
||||
export const searchModuleElasticsearchEngine: () => BackendFeature;
|
||||
|
||||
@@ -16,52 +16,51 @@
|
||||
import {
|
||||
coreServices,
|
||||
createBackendModule,
|
||||
createExtensionPoint,
|
||||
} from '@backstage/backend-plugin-api';
|
||||
import { searchEngineRegistryExtensionPoint } from '@backstage/plugin-search-backend-node/alpha';
|
||||
import { ElasticSearchSearchEngine } from '@backstage/plugin-search-backend-module-elasticsearch';
|
||||
import {
|
||||
ElasticSearchQueryTranslator,
|
||||
ElasticSearchSearchEngine,
|
||||
} from '@backstage/plugin-search-backend-module-elasticsearch';
|
||||
|
||||
/** @alpha */
|
||||
export interface ElasticSearchQueryTranslatorExtensionPoint {
|
||||
setTranslator(translator: ElasticSearchQueryTranslator): void;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extension point used to customize the ElasticSearch query translator.
|
||||
*
|
||||
* @alpha
|
||||
*/
|
||||
export const elasticsearchTranslatorExtensionPoint =
|
||||
createExtensionPoint<ElasticSearchQueryTranslatorExtensionPoint>({
|
||||
id: 'search.elasticsearchEngine.translator',
|
||||
});
|
||||
|
||||
/**
|
||||
* Search backend module for the Elasticsearch engine.
|
||||
*
|
||||
* @alpha
|
||||
* @remarks
|
||||
*
|
||||
* If you need to customize the search engine beyond what is supported by the
|
||||
* static configuration, you can create a custom module using the `ElasticSearchSearchEngine`.
|
||||
*
|
||||
* For example, this lets you configure your own query translator:
|
||||
*
|
||||
* ```ts
|
||||
* export const searchModuleElasticsearchEngine = createBackendModule({
|
||||
* moduleId: 'elasticsearchEngine',
|
||||
* pluginId: 'search',
|
||||
* register(env) {
|
||||
* env.registerInit({
|
||||
* deps: {
|
||||
* searchEngineRegistry: searchEngineRegistryExtensionPoint,
|
||||
* logger: coreServices.logger,
|
||||
* config: coreServices.rootConfig,
|
||||
* },
|
||||
* async init({ searchEngineRegistry, logger, config }) {
|
||||
* searchEngineRegistry.setSearchEngine(
|
||||
* await ElasticSearchSearchEngine.fromConfig({
|
||||
* logger,
|
||||
* config,
|
||||
* translator(query) {
|
||||
* return ... // translated query
|
||||
* }
|
||||
* }),
|
||||
* );
|
||||
* },
|
||||
* });
|
||||
* },
|
||||
* });
|
||||
* ```
|
||||
*/
|
||||
export const searchModuleElasticsearchEngine = createBackendModule({
|
||||
moduleId: 'elasticsearchEngine',
|
||||
pluginId: 'search',
|
||||
register(env) {
|
||||
let translator: ElasticSearchQueryTranslator | undefined;
|
||||
|
||||
env.registerExtensionPoint(elasticsearchTranslatorExtensionPoint, {
|
||||
setTranslator(newTranslator) {
|
||||
if (translator) {
|
||||
throw new Error(
|
||||
'ElasticSearch query translator may only be set once',
|
||||
);
|
||||
}
|
||||
translator = newTranslator;
|
||||
},
|
||||
});
|
||||
|
||||
env.registerInit({
|
||||
deps: {
|
||||
searchEngineRegistry: searchEngineRegistryExtensionPoint,
|
||||
@@ -73,6 +72,7 @@ export const searchModuleElasticsearchEngine = createBackendModule({
|
||||
await ElasticSearchSearchEngine.fromConfig({
|
||||
logger,
|
||||
config,
|
||||
translator,
|
||||
}),
|
||||
);
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user