Shun Miyazawa 2 سال پیش
والد
کامیت
27890c7c7f

+ 10 - 4
apps/app/src/server/service/search-delegator/elasticsearch-client.ts

@@ -53,9 +53,9 @@ export default class ElasticsearchClient {
   };
 
   cluster = {
-    health: (params: ES7RequestParams.ClusterHealth & estypes.ClusterHealthRequest)
+    health: ()
     : Promise<ES7ApiResponse<ClusterHealthResponse> | estypes.ClusterHealthResponse> =>
-      this.client instanceof ES7Client ? this.client.cluster.health(params) : this.client.cluster.health(params),
+      this.client instanceof ES7Client ? this.client.cluster.health() : this.client.cluster.health(),
   };
 
   indices = {
@@ -107,8 +107,14 @@ export default class ElasticsearchClient {
     return this.client instanceof ES7Client ? this.client.ping() : this.client.ping();
   }
 
-  reindex(params: ES7RequestParams.Reindex & estypes.ReindexRequest): Promise<ES7ApiResponse<ReindexResponse> | estypes.ReindexResponse> {
-    return this.client instanceof ES7Client ? this.client.reindex(params) : this.client.reindex(params);
+  // reindex(params: ES7RequestParams.Reindex & estypes.ReindexRequest): Promise<ES7ApiResponse<ReindexResponse> | estypes.ReindexResponse> {
+  //   return this.client instanceof ES7Client ? this.client.reindex(params) : this.client.reindex(params);
+  // }
+
+  reindex(indexName: string, tmpIndexName: string): Promise<ES7ApiResponse<ReindexResponse> | estypes.ReindexResponse> {
+    return this.client instanceof ES7Client
+      ? this.client.reindex({ wait_for_completion: false, body: { source: { index: indexName }, dest: { index: tmpIndexName } } })
+      : this.client.reindex({ wait_for_completion: false, source: { index: indexName }, dest: { index: tmpIndexName } });
   }
 
   async search(params: ES7RequestParams.Search & estypes.SearchRequest): Promise<SearchResponse | estypes.SearchResponse> {

+ 26 - 20
apps/app/src/server/service/search-delegator/elasticsearch.ts

@@ -58,7 +58,7 @@ class ElasticsearchDelegator implements SearchDelegator<Data, ESTermsKey, ESQuer
 
   elasticsearch: any;
 
-  client: any;
+  client: ElasticsearchClient;
 
   queries: any;
 
@@ -80,7 +80,7 @@ class ElasticsearchDelegator implements SearchDelegator<Data, ESTermsKey, ESQuer
     this.isElasticsearchV7 = elasticsearchVersion === 7;
 
     this.isElasticsearchReindexOnBoot = this.configManager.getConfig('crowi', 'app:elasticsearchReindexOnBoot');
-    this.client = null;
+    // this.client = null;
 
     // In Elasticsearch RegExp, we don't need to used ^ and $.
     // Ref: https://www.elastic.co/guide/en/elasticsearch/reference/5.6/query-dsl-regexp-query.html#_standard_operators
@@ -184,14 +184,14 @@ class ElasticsearchDelegator implements SearchDelegator<Data, ESTermsKey, ESQuer
    */
   async getInfo() {
     const info = await this.client.nodes.info();
-    if (!info._nodes || !info.nodes) {
+    if (!info != null) {
       throw new Error('There is no nodes');
     }
 
     let esVersion = 'unknown';
     const esNodeInfos = {};
 
-    for (const [nodeName, nodeInfo] of Object.entries<any>(info.nodes)) {
+    for (const [nodeName, nodeInfo] of Object.entries<any>(info)) {
       esVersion = nodeInfo.version;
 
       const filteredInfo = {
@@ -275,24 +275,24 @@ class ElasticsearchDelegator implements SearchDelegator<Data, ESTermsKey, ESQuer
 
     const tmpIndexName = `${indexName}-tmp`;
 
-    const reindexRequest = this.isElasticsearchV7
-      ? {
-        waitForCompletion: false,
-        body: {
-          source: { index: indexName },
-          dest: { index: tmpIndexName },
-        },
-      }
-      : {
-        wait_for_completion: false,
-        source: { index: indexName },
-        dest: { index: tmpIndexName },
-      };
+    // const reindexRequest = this.isElasticsearchV7
+    //   ? {
+    //     waitForCompletion: false,
+    //     body: {
+    //       source: { index: indexName },
+    //       dest: { index: tmpIndexName },
+    //     },
+    //   }
+    //   : {
+    //     wait_for_completion: false,
+    //     source: { index: indexName },
+    //     dest: { index: tmpIndexName },
+    //   };
 
     try {
       // reindex to tmp index
       await this.createIndex(tmpIndexName);
-      await client.reindex(reindexRequest);
+      await client.reindex(indexName, tmpIndexName);
 
       // update alias
       await client.indices.updateAliases({
@@ -345,7 +345,7 @@ class ElasticsearchDelegator implements SearchDelegator<Data, ESTermsKey, ESQuer
     }
 
     // create alias
-    const { body: isExistsAlias } = await client.indices.existsAlias({ name: aliasName, index: indexName });
+    const isExistsAlias = await client.indices.existsAlias({ name: aliasName, index: indexName });
     if (!isExistsAlias) {
       await client.indices.putAlias({
         name: aliasName,
@@ -674,10 +674,16 @@ class ElasticsearchDelegator implements SearchDelegator<Data, ESTermsKey, ESQuer
 
     const searchResponse = await this.client.search(query);
 
+    const total = searchResponse?.hits?.total;
+    let totalCount = 0;
+    if (typeof total === 'object') {
+      totalCount = total.value;
+    }
+
     return {
       meta: {
         took: searchResponse.took,
-        total: searchResponse.hits.total.value,
+        total: totalCount,
         hitsCount: searchResponse.hits.hits.length,
       },
       data: searchResponse.hits.hits.map((elm) => {