Ver Fonte

新增通过docId 查询ES内容,根据docID删除ES。

wukai há 2 anos atrás
pai
commit
209451a063

+ 4 - 23
doc-biz/src/main/java/com/doc/biz/controller/ElasticSearchController.java

@@ -10,19 +10,20 @@ import com.doc.common.config.EsConfig;
 import com.doc.common.core.domain.AjaxResult;
 import com.doc.common.enums.SpaceType;
 import com.doc.common.utils.SecurityUtils;
-import io.swagger.annotations.*;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiImplicitParam;
+import io.swagger.annotations.ApiImplicitParams;
+import io.swagger.annotations.ApiOperation;
 import org.springframework.data.domain.PageRequest;
 import org.springframework.data.elasticsearch.NoSuchIndexException;
 import org.springframework.data.elasticsearch.core.SearchHit;
 import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
 import javax.annotation.Resource;
 import java.util.ArrayList;
 import java.util.List;
-import java.util.Optional;
 
 /**
  * es查询控制器
@@ -89,24 +90,4 @@ public class ElasticSearchController {
             return AjaxResult.success("无结果");
         }
     }
-
-    /**
-     * 模糊查询ik分词器分词的content字段
-     */
-    @GetMapping("/query/{docId}")
-    @ApiOperation("根据ID查询文本内容")
-    public AjaxResult queryByDocId(@ApiParam(value = "文件ID", required = true) @PathVariable Long docId) {
-        DocInfo docInfo = infoService.selectDocInfoByDocId(docId);
-        String indexName = "docs_" + docInfo.getSpaceId();
-        esConfig.setIndexName(indexName);
-        Optional<EsDocInfo> optional = esDocInfoService.findById(docId);
-        if (optional.isPresent()) {
-            EsDocInfo info = optional.get();
-            info.setDocInfo(docInfo);
-            return AjaxResult.success(optional.get());
-        } else {
-            return AjaxResult.success(null);
-        }
-    }
-
 }

+ 15 - 0
doc-biz/src/main/java/com/doc/biz/service/IElasticSearchService.java

@@ -1,6 +1,7 @@
 package com.doc.biz.service;
 
 import com.doc.biz.domain.DocInfo;
+import com.doc.biz.domain.EsDocInfo;
 
 /**
  * ES操作接口
@@ -22,4 +23,18 @@ public interface IElasticSearchService {
      * @param info 文档信息
      */
     void delete(DocInfo info);
+    /**
+     * 通过ID删除ES内容
+     *
+     * @param docId id
+     */
+    void delete(Long docId);
+
+    /**
+     * 根据docId 查询文本内容
+     *
+     * @param docId id
+     * @return 文本
+     */
+    EsDocInfo getEsDocInfo(Long docId);
 }

+ 41 - 5
doc-biz/src/main/java/com/doc/biz/service/impl/ElasticSearchServiceImpl.java

@@ -2,10 +2,7 @@ package com.doc.biz.service.impl;
 
 import com.doc.biz.domain.DocInfo;
 import com.doc.biz.domain.EsDocInfo;
-import com.doc.biz.service.IElasticSearchService;
-import com.doc.biz.service.IEsDocInfoService;
-import com.doc.biz.service.IMongoService;
-import com.doc.biz.service.IOcrService;
+import com.doc.biz.service.*;
 import com.doc.biz.vo.DocumentVO;
 import com.doc.common.config.EsConfig;
 import com.doc.common.constant.Constants;
@@ -14,6 +11,7 @@ import com.doc.common.utils.StringUtils;
 import com.doc.common.utils.file.FileUtils;
 import com.doc.common.utils.file.PdfUtils;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.data.elasticsearch.NoSuchIndexException;
 import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Service;
 import org.springframework.web.multipart.MultipartFile;
@@ -23,6 +21,7 @@ import java.io.File;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Optional;
 import java.util.function.Function;
 
 /**
@@ -42,6 +41,8 @@ public class ElasticSearchServiceImpl implements IElasticSearchService {
     private IMongoService mongoService;
     @Resource
     private IOcrService ocrService;
+    @Resource
+    private IDocInfoService docInfoService;
 
     /**
      * 文件内容入es库
@@ -76,7 +77,42 @@ public class ElasticSearchServiceImpl implements IElasticSearchService {
     @Override
     public void delete(DocInfo info) {
         comboIndex(info.getSpaceId());
-        esDocInfoService.deleteById(info.getDocId());
+        //删除ES记录
+        try {
+            esDocInfoService.deleteById(info.getDocId());
+        } catch (NoSuchIndexException e) {
+            //不用管,表示没这个索引
+        }
+    }
+
+    /**
+     * 通过ID删除ES内容
+     *
+     * @param docId id
+     */
+    @Override
+    public void delete(Long docId) {
+        DocInfo docInfo = docInfoService.selectDocInfoByDocId(docId);
+        delete(docInfo);
+    }
+
+    /**
+     * 根据docId 查询文本内容
+     *
+     * @param docId id
+     * @return 文本
+     */
+    @Override
+    public EsDocInfo getEsDocInfo(Long docId) {
+        DocInfo docInfo = docInfoService.selectDocInfoByDocId(docId);
+        comboIndex(docInfo.getSpaceId());
+        Optional<EsDocInfo> optional = esDocInfoService.findById(docId);
+        if (optional.isPresent()) {
+            EsDocInfo info = optional.get();
+            info.setDocInfo(docInfo);
+            return info;
+        }
+        return null;
     }
 
     /**