Browse Source

- Implement rebuild based on env condition on elasticsearch.ts instead of search.ts
- Adjust elasticsearch.init()

LuqmanHakim-Grune 4 years ago
parent
commit
33c72f4e10

+ 7 - 1
packages/app/src/server/service/search-delegator/elasticsearch.ts

@@ -47,6 +47,8 @@ class ElasticsearchDelegator implements SearchDelegator<Data> {
 
   isElasticsearchV6: boolean
 
+  isElasticsearchReindexOnBoot: boolean
+
   elasticsearch: any
 
   client: any
@@ -65,6 +67,7 @@ class ElasticsearchDelegator implements SearchDelegator<Data> {
     this.isElasticsearchV6 = this.configManager.getConfig('crowi', 'app:useElasticsearchV6');
 
     this.elasticsearch = this.isElasticsearchV6 ? elasticsearch6 : elasticsearch7;
+    this.isElasticsearchReindexOnBoot = this.configManager.getConfig('crowi', 'app:elasticsearchReindexOnBoot');
     this.client = null;
 
     // In Elasticsearch RegExp, we don't need to used ^ and $.
@@ -144,7 +147,10 @@ class ElasticsearchDelegator implements SearchDelegator<Data> {
   }
 
   async init() {
-    return this.normalizeIndices();
+    if (!this.isElasticsearchReindexOnBoot) {
+      return this.normalizeIndices(); // call normalizeIndices() only
+    }
+    return this.rebuildIndex(); // rebuildIndex() will call normalizeIndeces() in the end of rebuild process
   }
 
   /**

+ 1 - 15
packages/app/src/server/service/search.ts

@@ -53,8 +53,6 @@ class SearchService implements SearchQueryParser, SearchResolver {
 
   isErrorOccuredOnSearching: boolean | null
 
-  isElasticsearchReindexOnBoot: boolean
-
   fullTextSearchDelegator: any & SearchDelegator
 
   nqDelegators: {[key in SearchDelegatorName]: SearchDelegator}
@@ -75,15 +73,9 @@ class SearchService implements SearchQueryParser, SearchResolver {
       logger.error(err);
     }
 
-    this.isElasticsearchReindexOnBoot = this.configManager.getConfig('crowi', 'app:elasticsearchReindexOnBoot');
-
     if (this.isConfigured) {
       this.fullTextSearchDelegator.init();
       this.registerUpdateEvent();
-      if (this.isElasticsearchReindexOnBoot) {
-        logger.info('Reindex elasticsearch is running');
-        this.rebuildIndex();
-      }
     }
   }
 
@@ -201,13 +193,7 @@ class SearchService implements SearchQueryParser, SearchResolver {
   }
 
   async rebuildIndex() {
-    try {
-      await this.fullTextSearchDelegator.rebuildIndex();
-      logger.info('Reindex elasticsearch done');
-    }
-    catch (err) {
-      logger.warn(`Reindex elasticsearch fail, ${err}`);
-    }
+    return this.fullTextSearchDelegator.rebuildIndex();
   }
 
   async parseSearchQuery(_queryString: string): Promise<ParsedQuery> {