瀏覽代碼

encapsulation of elasticsearch instances

Shun Miyazawa 2 年之前
父節點
當前提交
bbac79ea9b

+ 5 - 7
apps/app/src/server/service/search-delegator/elasticsearch-client.ts

@@ -1,7 +1,7 @@
 /* eslint-disable implicit-arrow-linebreak */
 /* eslint-disable no-confusing-arrow */
 import elasticsearch7, { Client as ES7Client, ApiResponse as ES7ApiResponse, RequestParams as ES7RequestParams } from '@elastic/elasticsearch7';
-import { Client as ES8Client, estypes } from '@elastic/elasticsearch8';
+import elasticsearch8, { Client as ES8Client, estypes } from '@elastic/elasticsearch8';
 
 import {
   BulkResponse,
@@ -21,16 +21,14 @@ export default class ElasticsearchClient {
 
   client: ES7Client | ES8Client;
 
-  constructor(elasticserch, options, rejectUnauthorized: boolean) {
+  constructor(isElasticsearch7: boolean, options, rejectUnauthorized: boolean) {
+    const elasticsearch = isElasticsearch7 ? elasticsearch7 : elasticsearch8;
 
-    const encryptionOption = elasticserch === elasticsearch7
+    const encryptionOption = isElasticsearch7
       ? { ssl: { rejectUnauthorized } }
       : { tls: { rejectUnauthorized } };
 
-    this.client = new elasticserch.Client({
-      ...options,
-      ...encryptionOption,
-    });
+    this.client = new elasticsearch.Client({ ...options, ...encryptionOption });
   }
 
   async bulk(params: ES7RequestParams.Bulk & estypes.BulkRequest): Promise<BulkResponse | estypes.BulkResponse> {

+ 1 - 2
apps/app/src/server/service/search-delegator/elasticsearch.ts

@@ -81,7 +81,6 @@ class ElasticsearchDelegator implements SearchDelegator<Data, ESTermsKey, ESQuer
 
     this.isElasticsearchV7 = elasticsearchVersion === 7;
 
-    this.elasticsearch = this.isElasticsearchV7 ? elasticsearch7 : elasticsearch8;
     this.isElasticsearchReindexOnBoot = this.configManager.getConfig('crowi', 'app:elasticsearchReindexOnBoot');
     this.client = null;
 
@@ -127,7 +126,7 @@ class ElasticsearchDelegator implements SearchDelegator<Data, ESTermsKey, ESQuer
       requestTimeout: this.configManager.getConfig('crowi', 'app:elasticsearchRequestTimeout'),
     };
 
-    this.client = new ElasticsearchClient(this.elasticsearch, options, rejectUnauthorized);
+    this.client = new ElasticsearchClient(this.isElasticsearchV7, options, rejectUnauthorized);
     this.indexName = indexName;
   }