From 9d6f9cfc0917c215ef9e8065cf9d84515c1168c1 Mon Sep 17 00:00:00 2001 From: Michael Bromley Date: Mon, 2 Sep 2024 14:47:21 +0200 Subject: [PATCH] fix(elasticsearch-plugin): Fix search multichannel indexing issue Relates to #3012 --- .../src/indexing/indexer.controller.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/elasticsearch-plugin/src/indexing/indexer.controller.ts b/packages/elasticsearch-plugin/src/indexing/indexer.controller.ts index 328a3bdcdc..7eb65c62c4 100644 --- a/packages/elasticsearch-plugin/src/indexing/indexer.controller.ts +++ b/packages/elasticsearch-plugin/src/indexing/indexer.controller.ts @@ -511,7 +511,6 @@ export class ElasticsearchIndexerController implements OnModuleInit, OnModuleDes if (!product) { return; } - let updatedProductVariants: ProductVariant[] = []; try { updatedProductVariants = await this.connection.rawConnection.getRepository(ProductVariant).find({ @@ -541,6 +540,7 @@ export class ElasticsearchIndexerController implements OnModuleInit, OnModuleDes languageVariants.push(...variant.translations.map(t => t.languageCode)); const uniqueLanguageVariants = unique(languageVariants); + const originalChannel = ctx.channel; for (const channel of product.channels) { ctx.setChannel(channel); const variantsInChannel = updatedProductVariants.filter(v => @@ -623,6 +623,7 @@ export class ElasticsearchIndexerController implements OnModuleInit, OnModuleDes } } } + ctx.setChannel(originalChannel); // Because we can have a huge amount of variant for 1 product, we also chunk update operations await this.executeBulkOperationsByChunks( @@ -944,7 +945,13 @@ export class ElasticsearchIndexerController implements OnModuleInit, OnModuleDes const productCustomMappings = Object.entries(this.options.customProductMappings); for (const [name, def] of productCustomMappings) { - item[`product-${name}`] = await def.valueFn(v.product, variants, languageCode, this.injector, ctx); + item[`product-${name}`] = await def.valueFn( + v.product, + variants, + languageCode, + this.injector, + ctx, + ); } return item; } catch (err: any) {