lrf 6 tháng trước cách đây
mục cha
commit
397041690e
2 tập tin đã thay đổi với 23 bổ sung1 xóa
  1. 7 1
      src/controller/init.controller.ts
  2. 16 0
      src/service/elasticsearch/es.service.ts

+ 7 - 1
src/controller/init.controller.ts

@@ -1,4 +1,4 @@
-import { Body, Controller, Inject, Post } from '@midwayjs/core';
+import { Body, Controller, Inject, Param, Post } from '@midwayjs/core';
 import { ESService } from '../service/elasticsearch/es.service';
 
 @Controller('/init')
@@ -17,6 +17,12 @@ export class InitController {
     return data;
   }
 
+  @Post('/indices/:index')
+  async initIndex(@Param('index') index:string) {
+    const data = await this.esService.initOneIndex(index)
+    return data;
+  }
+
   @Post('/data')
   async data(@Body('index') index: string, @Body('data') data: Array<any>) {
     console.log(`in init data: ${index} - ${data.length}`)

+ 16 - 0
src/service/elasticsearch/es.service.ts

@@ -42,6 +42,22 @@ export class ESService {
     }
     return tables;
   }
+  async initOneIndex(indexName) {
+    const index = toLower(indexName);
+    const hasIndex = await this.esClient.indices.exists({ index });
+    const mappings = indices[indexName];
+    if (!mappings) return;
+    if (hasIndex) {
+      await this.esClient.indices.delete({ index });
+    }
+    const properties = {};
+    for (const key in mappings) {
+      let pk = key;
+      const val = mappings[key];
+      properties[pk] = getType(val);
+    }
+    await this.esClient.indices.create({ index, mappings: { properties } });
+  }
   /**
    * 按表,初始化es数据
    * 1.获取数据总量