Просмотр исходного кода

Initialize in elasticsearch-client

Shun Miyazawa 2 лет назад
Родитель
Сommit
4855813413

+ 11 - 3
apps/app/src/server/service/search-delegator/elasticsearch-client.ts

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

+ 6 - 8
apps/app/src/server/service/search-delegator/elasticsearch.ts

@@ -2,7 +2,7 @@ import { Writable, Transform } from 'stream';
 import { URL } from 'url';
 
 import elasticsearch7 from '@elastic/elasticsearch7';
-import elasticsearch8 from '@elastic/elasticsearch8';
+import elasticsearch8, { Client } from '@elastic/elasticsearch8';
 import gc from 'expose-gc/function';
 import mongoose from 'mongoose';
 import streamToPromise from 'stream-to-promise';
@@ -119,17 +119,15 @@ class ElasticsearchDelegator implements SearchDelegator<Data, ESTermsKey, ESQuer
   initClient() {
     const { host, auth, indexName } = this.getConnectionInfo();
 
-    const elasticsearchRejectUnauthorized = this.configManager.getConfig('crowi', 'app:elasticsearchRejectUnauthorized');
-    const encryptionOption = this.isElasticsearchV7
-      ? { ssl: { rejectUnauthorized: elasticsearchRejectUnauthorized } }
-      : { tls: { rejectUnauthorized: elasticsearchRejectUnauthorized } };
+    const rejectUnauthorized = this.configManager.getConfig('crowi', 'app:elasticsearchRejectUnauthorized');
 
-    this.client = new ElasticsearchClient(new this.elasticsearch.Client({
+    const options = {
       node: host,
       auth,
       requestTimeout: this.configManager.getConfig('crowi', 'app:elasticsearchRequestTimeout'),
-      ...encryptionOption,
-    }));
+    };
+
+    this.client = new ElasticsearchClient(this.elasticsearch, options, rejectUnauthorized);
     this.indexName = indexName;
   }