From 9befc4e57e6faa00d1863b37aa7a3dcc002b09b5 Mon Sep 17 00:00:00 2001 From: zhn <947514737@qq.com> Date: Tue, 22 Mar 2022 10:21:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E6=A1=A3=E5=BA=93--=E6=90=9C=E7=B4=A2?= =?UTF-8?q?(=E6=96=87=E4=BB=B6=E5=86=85=E5=AE=B9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jero-boot/jero-boot-modules/pom.xml | 5 + .../BussDocumentLibraryEOServiceImpl.java | 61 +++++++- .../modules/document/utils/ReadPdfUtil.java | 40 ++++++ .../modules/document/utils/ReadWordUtil.java | 45 ++++++ .../impl/DocumentSearchServiceImpl.java | 135 +++++++++++++++++- 5 files changed, 279 insertions(+), 7 deletions(-) create mode 100644 jero-boot/jero-boot-modules/src/main/java/com/jero/modules/document/utils/ReadPdfUtil.java create mode 100644 jero-boot/jero-boot-modules/src/main/java/com/jero/modules/document/utils/ReadWordUtil.java diff --git a/jero-boot/jero-boot-modules/pom.xml b/jero-boot/jero-boot-modules/pom.xml index fd4cbc16d..b0fe1c28b 100644 --- a/jero-boot/jero-boot-modules/pom.xml +++ b/jero-boot/jero-boot-modules/pom.xml @@ -32,6 +32,11 @@ + + org.apache.pdfbox + pdfbox + 2.0.6 + com.jero.boot jero-boot-module-system diff --git a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/document/service/impl/BussDocumentLibraryEOServiceImpl.java b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/document/service/impl/BussDocumentLibraryEOServiceImpl.java index 92fe9b1cf..42491c88f 100644 --- a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/document/service/impl/BussDocumentLibraryEOServiceImpl.java +++ b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/document/service/impl/BussDocumentLibraryEOServiceImpl.java @@ -29,6 +29,8 @@ import com.jero.modules.document.enums.FieldTypeEnum; import com.jero.modules.document.enums.SearchEnum; import com.jero.modules.document.mapper.BussDocumentLibraryEOMapper; import com.jero.modules.document.service.IBussDocumentLibraryEOService; +import com.jero.modules.document.utils.ReadPdfUtil; +import com.jero.modules.document.utils.ReadWordUtil; import com.jero.modules.domain.service.DomainUserRelService; import com.jero.modules.feishu.service.IFeishuService; import com.jero.modules.log.entity.BussLogEO; @@ -926,8 +928,9 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl> fileTextList = new ArrayList<>(); try { for (Map.Entry entry : map.entrySet()) { String key = entry.getKey(); @@ -961,6 +964,13 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl list = readFileContent(oSSFileList); + if(list.size() != 0){ + fileTextList.add(list) ; + } + } iOSSFileService.updateFileInfo(oSSFileList); valuesBuilder.append("," + "'" + valueTemp + "'"); } else { @@ -972,6 +982,7 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl readFileContent(List oSSFileList){ + //查询文件详细信息 + List fileIdList = oSSFileList.stream().map(OSSFile::getId).collect(Collectors.toList()); + oSSFileList = iOSSFileService.getFileInfos(StringUtils.join(fileIdList, ",")); + List result = new ArrayList<>(); + for (OSSFile ossFile : oSSFileList) { + if(ossFile.getFileName().endsWith(".doc") + || ossFile.getFileName().endsWith(".docx") + || ossFile.getFileName().endsWith(".DOC") + || ossFile.getFileName().endsWith(".DOCX")){ + String wordContent = ReadWordUtil.readWord(ossFile.getUrl()); + if(StringUtils.isNotBlank(wordContent)){ + result.add(wordContent); + } + }else if(ossFile.getFileName().endsWith(".pdf")||ossFile.getFileName().endsWith(".PDF")){ + String pdfContent = ReadPdfUtil.readPdf(ossFile.getUrl()); + if(StringUtils.isNotBlank(pdfContent)){ + result.add(pdfContent); + } + } + } + return result; + } private void esFullText(String cut, List fieldList, List categoryList, List dictItemList, List> mapList, StringBuilder sb, Map.Entry entry) { for (OnlCgformField onlCgformField : fieldList) { @@ -1215,10 +1253,11 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl mapFullText = new HashMap<>(); + mapFullText.put("id", id); mapFullText.put("module_type", "文档库"); mapFullText.put("serial_number", map.get("serial_number")); mapFullText.put("title", map.get("title")); - mapFullText.put("file_text", "文件内容-->废止"); + List> fileTextList = new ArrayList<>(); for (Map.Entry entry : map.entrySet()) { String key = entry.getKey(); Object value = entry.getValue(); @@ -1255,9 +1294,25 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl list = readFileContent(oSSFileList); + if(list.size() != 0){ + fileTextList.add(list) ; + } + } iOSSFileService.updateFileInfo(oSSFileList); valuesBuilder.append("'" + valueTemp + "'" + ","); } else { + List oSSFileList = new ArrayList<>(); + OSSFile ossFile = new OSSFile(); + ossFile.setId((String) value); + oSSFileList.add(ossFile); + //读取word或PDF文件内容 + List list = readFileContent(oSSFileList); + if (list.size() != 0) { + fileTextList.add(list); + } valuesBuilder.append("'" + value + "'" + ","); } // //判断文件是否修改 @@ -1289,7 +1344,9 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl map) { + boolean flagTemp = jeroElasticsearchTemplate.indexExists(SearchEnum.INDEX_NAME_DOCUMENT.getValue()); + if(!flagTemp){ + return new Page(Integer.parseInt(map.get("pageNo").toString()), Integer.parseInt(map.get("pageSize").toString())); + } //文档库所有的字段 List fieldList = onlCgformFieldService.getFieldList(ModuleEnum.DOCUMENT_LIBRARY.getValue()); //需要查询的列表字段 @@ -275,6 +279,12 @@ public class DocumentSearchServiceImpl implements IDocumentSearchService { // map2.put("content",selectValue); // queryMap.put("match", map2); // } + //判断索引是否存在 + boolean flagTemp = jeroElasticsearchTemplate.indexExists(SearchEnum.FULL_TEXT_SEARCH.getValue()); + if(!flagTemp){ + return new Page(searchVO.getPage(), searchVO.getPageSize()); + } + String selectValue = searchVO.getSelectValue(); String selectValueTwo = searchVO.getSelectValueTwo(); //测试数据 @@ -313,10 +323,14 @@ public class DocumentSearchServiceImpl implements IDocumentSearchService { searchVO.getPageSize()); // JSONObject queryObject11 = jeroElasticsearchTemplate.buildQuery(null, jsonObject11,highlightMap,null, Integer.parseInt(map.get("pageNo").toString()), Integer.parseInt(map.get("pageSize").toString())); //2. 数据查询 - JSONObject search = jeroElasticsearchTemplate.search("fulltextsearch", "fulltextsearch", queryObject); + JSONObject search = jeroElasticsearchTemplate.search(SearchEnum.FULL_TEXT_SEARCH.getValue(), + SearchEnum.FULL_TEXT_SEARCH.getValue(), + queryObject); //全部的数据(调试接口用--最后删除即可) - JSONObject searchAll = jeroElasticsearchTemplate.search("fulltextsearch", "fulltextsearch", new JSONObject()); + JSONObject searchAll = jeroElasticsearchTemplate.search(SearchEnum.FULL_TEXT_SEARCH.getValue(), + SearchEnum.FULL_TEXT_SEARCH.getValue(), + new JSONObject()); List> list = (List>) (((Map) search.get("hits")).get("hits")); @@ -331,6 +345,7 @@ public class DocumentSearchServiceImpl implements IDocumentSearchService { list = list.stream().filter(e -> searchVO.getIds().contains((String) e.get("id"))).collect(Collectors.toList()); } + List> listNew = new ArrayList<>(); for (Map stringObjectMap : list) { Map mapSource = (Map) stringObjectMap.get("_source"); if (StringUtils.isNotBlank(selectValueTwo)) { @@ -338,7 +353,12 @@ public class DocumentSearchServiceImpl implements IDocumentSearchService { int count = 0; for (Map.Entry entry : mapSource.entrySet()) { String key = entry.getKey(); - String value = (String) entry.getValue(); + String value = null; + try { + value = (String) entry.getValue(); + } catch (Exception e) { + listNew.add(stringObjectMap); + } //判断value中是否包含二次搜索中的字符 boolean flag = judgeContains(value, selectValueTwo); if (flag) { @@ -376,9 +396,18 @@ public class DocumentSearchServiceImpl implements IDocumentSearchService { // } //----------------------------------------------------------------------------- //处理高亮module_type,title,serial_number,file_text + for (Map.Entry entry : mapSource.entrySet()) { String key = entry.getKey(); - String value = (String) entry.getValue(); + String value = null; + try { + //文件内容为字符串格式 + value = (String) entry.getValue(); + } catch (Exception e) { + //文件内容为集合格式 + listNew.add(stringObjectMap); + e.printStackTrace(); + } StringBuilder sb = new StringBuilder(); if (StringUtils.isNotBlank(value)) { for (int i = 0; i < value.length(); i++) { @@ -390,10 +419,105 @@ public class DocumentSearchServiceImpl implements IDocumentSearchService { sb.append(String.valueOf(chStr)); } } - entry.setValue(sb.toString()); + if(ObjectUtils.isNotEmpty(sb)){ + entry.setValue(sb.toString()); + } } } } + + //若一条标准含有多条发布稿或报批稿等文件,需要对每个附件进行搜索并多行展示, + //例如:GB 7258中含有两个发布稿,并且两个发布稿都有搜索内容,则GB 7258在列表中应该展示两行, + //展示不同发布稿的正文内容 + List> result = new ArrayList<>(); + List idList = new ArrayList<>(); + for (Map map : listNew) { + idList.add((String) map.get("_id")); + Map mapSource = (Map) map.get("_source"); + String moduleType = (String) mapSource.get("module_type"); + String serialNumber = (String) mapSource.get("serial_number"); + String title = (String) mapSource.get("title"); + String content = (String) mapSource.get("content"); + String id = (String) mapSource.get("id"); + int count = 0; + if(StringUtils.isNotBlank(selectValueTwo)){ + //二次查询 + for (Map.Entry entry : mapSource.entrySet()) { + String key = entry.getKey(); + if("file_text".equals(key)){ + for (List valueList : (List>) entry.getValue()) { + for (String s : valueList) { + StringBuilder sb = new StringBuilder(); + if (StringUtils.isNotBlank(s)) { + //判断value中是否包含二次搜索中的字符 + boolean flag = judgeContains(s, selectValueTwo); + if(flag){ + for (int i = 0; i < s.length(); i++) { + String chStr = String.valueOf(s.charAt(i)); + if (selectValue.contains(String.valueOf(chStr))) { + chStr = "" + chStr + ""; + count ++; + } + sb.append(String.valueOf(chStr)); + } + } + } + if(count > 0){ + Map mapTemp = new HashMap<>(); + mapTemp.put("id",id); + mapTemp.put("module_type",moduleType); + mapTemp.put("serial_number",serialNumber); + mapTemp.put("title",title); + mapTemp.put("content",content); + mapTemp.put("file_text",sb.toString()); + result.add(mapTemp); + } + } + } + } + } + }else{ + //首次查询 + for (Map.Entry entry : mapSource.entrySet()) { + String key = entry.getKey(); + if("file_text".equals(key)){ + for (List valueList : (List>) entry.getValue()) { + for (String s : valueList) { + StringBuilder sb = new StringBuilder(); + if (StringUtils.isNotBlank(s)) { + for (int i = 0; i < s.length(); i++) { + String chStr = String.valueOf(s.charAt(i)); + if (selectValue.contains(String.valueOf(chStr))) { + chStr = "" + chStr + ""; + count ++; + } + sb.append(String.valueOf(chStr)); + } + } + if(count > 0){ + Map mapTemp = new HashMap<>(); + mapTemp.put("id",id); + mapTemp.put("module_type",moduleType); + mapTemp.put("serial_number",serialNumber); + mapTemp.put("title",title); + mapTemp.put("content",content); + mapTemp.put("file_text",sb.toString()); + result.add(mapTemp); + } + } + } + } + } + } + } + int a=0; + //在原始数据中删除idList,再将result填入到原始数据中 + if(result.size() != 0){ + mapList.removeIf(e->idList.contains(e.get("id"))); + mapList.addAll(result); + } + + //处理分页 IPage page = new Page(searchVO.getPage(), searchVO.getPageSize()); if (StringUtils.isNotBlank(selectValueTwo)) { @@ -449,6 +573,7 @@ public class DocumentSearchServiceImpl implements IDocumentSearchService { @Override public void deleteAll() { jeroElasticsearchTemplate.removeIndex(SearchEnum.INDEX_NAME_DOCUMENT.getValue()); + jeroElasticsearchTemplate.removeIndex(SearchEnum.FULL_TEXT_SEARCH.getValue()); }