diff --git a/plugins/search-backend-node/src/alpha.ts b/plugins/search-backend-node/src/alpha.ts index 331f0ae304..20b0f22089 100644 --- a/plugins/search-backend-node/src/alpha.ts +++ b/plugins/search-backend-node/src/alpha.ts @@ -34,7 +34,7 @@ import { * @alpha * Options for build method on {@link SearchIndexService}. */ -export type SearchIndexServiceStartOptions = { +export type SearchIndexServiceBuildOptions = { searchEngine: SearchEngine; collators: RegisterCollatorParameters[]; decorators: RegisterDecoratorParameters[]; @@ -45,15 +45,21 @@ export type SearchIndexServiceStartOptions = { * Interface for implementation of index service. */ export interface SearchIndexService { + /** + * Initializes state in preparation for starting the search index service + */ + build(options: SearchIndexServiceBuildOptions): void; + /** * Starts indexing process */ - start(options: SearchIndexServiceStartOptions): Promise; + start(): Promise; /** * Stops indexing process */ stop(): Promise; + /** * Returns an index types list. */ @@ -83,7 +89,7 @@ type DefaultSearchIndexServiceOptions = { /** * @alpha - * Reponsible for register the indexing task and start the schedule. + * Responsible for register the indexing task and start the schedule. */ class DefaultSearchIndexService implements SearchIndexService { private readonly logger: LoggerService; @@ -98,7 +104,7 @@ class DefaultSearchIndexService implements SearchIndexService { return new DefaultSearchIndexService(options); } - async start(options: SearchIndexServiceStartOptions): Promise { + build(options: SearchIndexServiceBuildOptions): void { this.indexBuilder = new IndexBuilder({ logger: this.logger, searchEngine: options.searchEngine, @@ -111,8 +117,13 @@ class DefaultSearchIndexService implements SearchIndexService { options.decorators.forEach(decorator => this.indexBuilder?.addDecorator(decorator), ); + } - const { scheduler } = await this.indexBuilder?.build(); + async start(): Promise { + if (!this.indexBuilder) { + throw new Error('IndexBuilder is not initialized, call build first'); + } + const { scheduler } = await this.indexBuilder.build(); this.scheduler = scheduler; this.scheduler!.start(); } diff --git a/plugins/search-backend/src/alpha.ts b/plugins/search-backend/src/alpha.ts index a17b542903..a7c2c6c872 100644 --- a/plugins/search-backend/src/alpha.ts +++ b/plugins/search-backend/src/alpha.ts @@ -121,13 +121,14 @@ export default createBackendPlugin({ const collators = searchIndexRegistry.getCollators(); const decorators = searchIndexRegistry.getDecorators(); + searchIndexService.build({ + searchEngine: searchEngine!, + collators, + decorators, + }); lifecycle.addStartupHook(async () => { - await searchIndexService.start({ - searchEngine: searchEngine!, - collators, - decorators, - }); + await searchIndexService.start(); }); lifecycle.addShutdownHook(async () => {