Browse Source

停用词

lrf 7 months ago
parent
commit
e1ba1bd68a
2 changed files with 36 additions and 1 deletions
  1. 19 1
      src/controller/home.controller.ts
  2. 17 0
      src/service/esDict.service.ts

+ 19 - 1
src/controller/home.controller.ts

@@ -53,7 +53,7 @@ export class HomeController {
    */
   @Get('/es_dict')
   async getEsDict() {
-    console.log(`es dict sync - ${new Date()}`)
+    console.log(`es dict sync - ${new Date()}`);
     const result = await this.esDict.getDictContent();
     const ranStr = this.esDict.randomStr();
     try {
@@ -65,6 +65,24 @@ export class HomeController {
 
     return result;
   }
+  /**
+   * es请求远程字典内容, 约1分钟来同步一次
+   * @returns 返回停用词字典内容
+   */
+  @Get('/es_stop_dict')
+  async getEsStopDict() {
+    console.log(`es stop dict sync - ${new Date()}`);
+    const result = await this.esDict.getStopDictContent();
+    const ranStr = this.esDict.randomStr();
+    try {
+      this.ctx.response.etag = ranStr;
+      this.ctx.response.lastModified = new Date();
+    } catch (error) {
+      console.log(error);
+    }
+
+    return result;
+  }
 
   @All('/**')
   async proxy() {

+ 17 - 0
src/service/esDict.service.ts

@@ -9,6 +9,7 @@ export class EsDictService {
   baseUrl: object;
   @InjectClient(HttpServiceFactory, 'default')
   serviceAxios: HttpService;
+
   async getDictContent() {
     const reqConfig: any = {
       url: `${this.baseUrl}/cxyy/es/dict/remote`,
@@ -23,6 +24,22 @@ export class EsDictService {
       console.log(error);
     }
   }
+  async getStopDictContent() {
+    const reqConfig: any = {
+      url: `${this.baseUrl}/cxyy/es/dict/stop/remote`,
+      method: 'Get',
+    };
+    try {
+      const res = await this.serviceAxios.request(reqConfig);
+      if (res.status !== 200) throw new ServiceError(ErrorCode.REQUSET_ERROR);
+      const data = get(res, 'data.data');
+      return data;
+    } catch (error) {
+      console.log(error);
+    }
+  }
+
+
   randomStr(len = 6) {
     return Math.random().toString(36).slice(-len);
   }