Browse Source

refactor(elasticsearch-plugin): Tidy up some duplicated values

Michael Bromley 4 years ago
parent
commit
64a3380a0d

+ 36 - 59
packages/elasticsearch-plugin/src/indexer.controller.ts

@@ -23,7 +23,7 @@ import {
 import { Observable } from 'rxjs';
 import { Observable } from 'rxjs';
 
 
 import { ELASTIC_SEARCH_OPTIONS, loggerCtx, PRODUCT_INDEX_NAME, VARIANT_INDEX_NAME } from './constants';
 import { ELASTIC_SEARCH_OPTIONS, loggerCtx, PRODUCT_INDEX_NAME, VARIANT_INDEX_NAME } from './constants';
-import { createIndices } from './indexing-utils';
+import { createIndices, getIndexNameByAlias } from './indexing-utils';
 import { ElasticsearchOptions } from './options';
 import { ElasticsearchOptions } from './options';
 import {
 import {
     BulkOperation,
     BulkOperation,
@@ -214,19 +214,11 @@ export class ElasticsearchIndexerController implements OnModuleInit, OnModuleDes
                 const operations: Array<BulkProductOperation | BulkVariantOperation> = [];
                 const operations: Array<BulkProductOperation | BulkVariantOperation> = [];
 
 
                 const reindexTempName = new Date().getTime();
                 const reindexTempName = new Date().getTime();
+                const productIndexName = this.options.indexPrefix + PRODUCT_INDEX_NAME;
+                const variantIndexName = this.options.indexPrefix + VARIANT_INDEX_NAME;
+                const reindexProductAliasName = productIndexName + `-reindex-${reindexTempName}`;
+                const reindexVariantAliasName = variantIndexName + `-reindex-${reindexTempName}`;
                 try {
                 try {
-                    const getIndexNameByAlias = async (aliasName: string) => {
-                        const aliasExist = await this.client.indices.existsAlias({ name: aliasName });
-                        if (aliasExist.body) {
-                            const alias = await this.client.indices.getAlias({
-                                name: aliasName,
-                            });
-                            return Object.keys(alias.body)[0];
-                        } else {
-                            return aliasName;
-                        }
-                    };
-
                     await createIndices(
                     await createIndices(
                         this.client,
                         this.client,
                         this.options.indexPrefix,
                         this.options.indexPrefix,
@@ -236,45 +228,41 @@ export class ElasticsearchIndexerController implements OnModuleInit, OnModuleDes
                         true,
                         true,
                         `-reindex-${reindexTempName}`,
                         `-reindex-${reindexTempName}`,
                     );
                     );
-                    const reindexProductAliasName =
-                        this.options.indexPrefix + PRODUCT_INDEX_NAME + `-reindex-${reindexTempName}`;
-                    const reindexVariantAliasName =
-                        this.options.indexPrefix + VARIANT_INDEX_NAME + `-reindex-${reindexTempName}`;
-                    const reindexProductIndexName = await getIndexNameByAlias(reindexProductAliasName);
-                    const reindexVariantIndexName = await getIndexNameByAlias(reindexVariantAliasName);
+
+                    const reindexProductIndexName = await getIndexNameByAlias(
+                        this.client,
+                        reindexProductAliasName,
+                    );
+                    const reindexVariantIndexName = await getIndexNameByAlias(
+                        this.client,
+                        reindexVariantAliasName,
+                    );
 
 
                     const originalProductAliasExist = await this.client.indices.existsAlias({
                     const originalProductAliasExist = await this.client.indices.existsAlias({
-                        name: this.options.indexPrefix + PRODUCT_INDEX_NAME,
+                        name: productIndexName,
                     });
                     });
                     const originalVariantAliasExist = await this.client.indices.existsAlias({
                     const originalVariantAliasExist = await this.client.indices.existsAlias({
-                        name: this.options.indexPrefix + VARIANT_INDEX_NAME,
+                        name: variantIndexName,
                     });
                     });
                     const originalProductIndexExist = await this.client.indices.exists({
                     const originalProductIndexExist = await this.client.indices.exists({
-                        index: this.options.indexPrefix + PRODUCT_INDEX_NAME,
+                        index: productIndexName,
                     });
                     });
                     const originalVariantIndexExist = await this.client.indices.exists({
                     const originalVariantIndexExist = await this.client.indices.exists({
-                        index: this.options.indexPrefix + PRODUCT_INDEX_NAME,
+                        index: variantIndexName,
                     });
                     });
 
 
-                    const originalProductIndexName = await getIndexNameByAlias(
-                        this.options.indexPrefix + PRODUCT_INDEX_NAME,
-                    );
-                    const originalVariantIndexName = await getIndexNameByAlias(
-                        this.options.indexPrefix + VARIANT_INDEX_NAME,
-                    );
+                    const originalProductIndexName = await getIndexNameByAlias(this.client, productIndexName);
+                    const originalVariantIndexName = await getIndexNameByAlias(this.client, variantIndexName);
 
 
                     if (originalVariantAliasExist.body || originalVariantIndexExist.body) {
                     if (originalVariantAliasExist.body || originalVariantIndexExist.body) {
                         await this.client.reindex({
                         await this.client.reindex({
                             refresh: true,
                             refresh: true,
                             body: {
                             body: {
                                 source: {
                                 source: {
-                                    index: this.options.indexPrefix + VARIANT_INDEX_NAME,
+                                    index: variantIndexName,
                                 },
                                 },
                                 dest: {
                                 dest: {
-                                    index:
-                                        this.options.indexPrefix +
-                                        VARIANT_INDEX_NAME +
-                                        `-reindex-${reindexTempName}`,
+                                    index: reindexVariantAliasName,
                                 },
                                 },
                             },
                             },
                         });
                         });
@@ -284,13 +272,10 @@ export class ElasticsearchIndexerController implements OnModuleInit, OnModuleDes
                             refresh: true,
                             refresh: true,
                             body: {
                             body: {
                                 source: {
                                 source: {
-                                    index: this.options.indexPrefix + PRODUCT_INDEX_NAME,
+                                    index: productIndexName,
                                 },
                                 },
                                 dest: {
                                 dest: {
-                                    index:
-                                        this.options.indexPrefix +
-                                        PRODUCT_INDEX_NAME +
-                                        `-reindex-${reindexTempName}`,
+                                    index: reindexProductAliasName,
                                 },
                                 },
                             },
                             },
                         });
                         });
@@ -300,31 +285,25 @@ export class ElasticsearchIndexerController implements OnModuleInit, OnModuleDes
                         {
                         {
                             remove: {
                             remove: {
                                 index: reindexVariantIndexName,
                                 index: reindexVariantIndexName,
-                                alias:
-                                    this.options.indexPrefix +
-                                    VARIANT_INDEX_NAME +
-                                    `-reindex-${reindexTempName}`,
+                                alias: reindexVariantAliasName,
                             },
                             },
                         },
                         },
                         {
                         {
                             remove: {
                             remove: {
                                 index: reindexProductIndexName,
                                 index: reindexProductIndexName,
-                                alias:
-                                    this.options.indexPrefix +
-                                    PRODUCT_INDEX_NAME +
-                                    `-reindex-${reindexTempName}`,
+                                alias: reindexProductAliasName,
                             },
                             },
                         },
                         },
                         {
                         {
                             add: {
                             add: {
                                 index: reindexVariantIndexName,
                                 index: reindexVariantIndexName,
-                                alias: this.options.indexPrefix + VARIANT_INDEX_NAME,
+                                alias: variantIndexName,
                             },
                             },
                         },
                         },
                         {
                         {
                             add: {
                             add: {
                                 index: reindexProductIndexName,
                                 index: reindexProductIndexName,
-                                alias: this.options.indexPrefix + PRODUCT_INDEX_NAME,
+                                alias: productIndexName,
                             },
                             },
                         },
                         },
                     ];
                     ];
@@ -333,12 +312,12 @@ export class ElasticsearchIndexerController implements OnModuleInit, OnModuleDes
                         actions.push({
                         actions.push({
                             remove: {
                             remove: {
                                 index: originalProductIndexName,
                                 index: originalProductIndexName,
-                                alias: this.options.indexPrefix + PRODUCT_INDEX_NAME,
+                                alias: productIndexName,
                             },
                             },
                         });
                         });
                     } else if (originalProductIndexExist.body) {
                     } else if (originalProductIndexExist.body) {
                         await this.client.indices.delete({
                         await this.client.indices.delete({
-                            index: [this.options.indexPrefix + PRODUCT_INDEX_NAME],
+                            index: [productIndexName],
                         });
                         });
                     }
                     }
 
 
@@ -346,12 +325,12 @@ export class ElasticsearchIndexerController implements OnModuleInit, OnModuleDes
                         actions.push({
                         actions.push({
                             remove: {
                             remove: {
                                 index: originalVariantIndexName,
                                 index: originalVariantIndexName,
-                                alias: this.options.indexPrefix + VARIANT_INDEX_NAME,
+                                alias: variantIndexName,
                             },
                             },
                         });
                         });
                     } else if (originalVariantIndexExist.body) {
                     } else if (originalVariantIndexExist.body) {
                         await this.client.indices.delete({
                         await this.client.indices.delete({
-                            index: [this.options.indexPrefix + VARIANT_INDEX_NAME],
+                            index: [variantIndexName],
                         });
                         });
                     }
                     }
 
 
@@ -379,12 +358,11 @@ export class ElasticsearchIndexerController implements OnModuleInit, OnModuleDes
                     Logger.warn(JSON.stringify(e), loggerCtx);
                     Logger.warn(JSON.stringify(e), loggerCtx);
                 } finally {
                 } finally {
                     const reindexVariantAliasExist = await this.client.indices.existsAlias({
                     const reindexVariantAliasExist = await this.client.indices.existsAlias({
-                        name: this.options.indexPrefix + VARIANT_INDEX_NAME + `-reindex-${reindexTempName}`,
+                        name: reindexVariantAliasName,
                     });
                     });
                     if (reindexVariantAliasExist.body) {
                     if (reindexVariantAliasExist.body) {
                         const reindexVariantAliasResult = await this.client.indices.getAlias({
                         const reindexVariantAliasResult = await this.client.indices.getAlias({
-                            name:
-                                this.options.indexPrefix + VARIANT_INDEX_NAME + `-reindex-${reindexTempName}`,
+                            name: reindexVariantAliasName,
                         });
                         });
                         const reindexVariantIndexName = Object.keys(reindexVariantAliasResult.body)[0];
                         const reindexVariantIndexName = Object.keys(reindexVariantAliasResult.body)[0];
                         await this.client.indices.delete({
                         await this.client.indices.delete({
@@ -392,12 +370,11 @@ export class ElasticsearchIndexerController implements OnModuleInit, OnModuleDes
                         });
                         });
                     }
                     }
                     const reindexProductAliasExist = await this.client.indices.existsAlias({
                     const reindexProductAliasExist = await this.client.indices.existsAlias({
-                        name: this.options.indexPrefix + PRODUCT_INDEX_NAME + `-reindex-${reindexTempName}`,
+                        name: reindexProductAliasName,
                     });
                     });
                     if (reindexProductAliasExist.body) {
                     if (reindexProductAliasExist.body) {
                         const reindexProductAliasResult = await this.client.indices.getAlias({
                         const reindexProductAliasResult = await this.client.indices.getAlias({
-                            name:
-                                this.options.indexPrefix + PRODUCT_INDEX_NAME + `-reindex-${reindexTempName}`,
+                            name: reindexProductAliasName,
                         });
                         });
                         const reindexProductIndexName = Object.keys(reindexProductAliasResult.body)[0];
                         const reindexProductIndexName = Object.keys(reindexProductAliasResult.body)[0];
                         await this.client.indices.delete({
                         await this.client.indices.delete({

+ 14 - 2
packages/elasticsearch-plugin/src/indexing-utils.ts

@@ -117,14 +117,14 @@ export async function createIndices(
 
 
 export async function deleteIndices(client: Client, prefix: string) {
 export async function deleteIndices(client: Client, prefix: string) {
     try {
     try {
-        const index = prefix + VARIANT_INDEX_NAME;
+        const index = await getIndexNameByAlias(client, prefix + VARIANT_INDEX_NAME);
         await client.indices.delete({ index });
         await client.indices.delete({ index });
         Logger.verbose(`Deleted index "${index}"`, loggerCtx);
         Logger.verbose(`Deleted index "${index}"`, loggerCtx);
     } catch (e) {
     } catch (e) {
         Logger.error(e, loggerCtx);
         Logger.error(e, loggerCtx);
     }
     }
     try {
     try {
-        const index = prefix + PRODUCT_INDEX_NAME;
+        const index = await getIndexNameByAlias(client, prefix + PRODUCT_INDEX_NAME);
         await client.indices.delete({ index });
         await client.indices.delete({ index });
         Logger.verbose(`Deleted index "${index}"`, loggerCtx);
         Logger.verbose(`Deleted index "${index}"`, loggerCtx);
     } catch (e) {
     } catch (e) {
@@ -162,3 +162,15 @@ export async function deleteByChannel(client: Client, prefix: string, channelId:
         Logger.error(e, loggerCtx);
         Logger.error(e, loggerCtx);
     }
     }
 }
 }
+
+export async function getIndexNameByAlias(client: Client, aliasName: string) {
+    const aliasExist = await client.indices.existsAlias({ name: aliasName });
+    if (aliasExist.body) {
+        const alias = await client.indices.getAlias({
+            name: aliasName,
+        });
+        return Object.keys(alias.body)[0];
+    } else {
+        return aliasName;
+    }
+}