From 7dc544f313f36389eb08eb485cf4903aac69e6d4 Mon Sep 17 00:00:00 2001 From: zhn <947514737@qq.com> Date: Wed, 23 Mar 2022 15:52:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E6=A1=A3=E5=BA=93--=E5=88=A0=E9=99=A4?= =?UTF-8?q?es=E5=92=8C=E6=90=9C=E7=B4=A2=E4=B8=AD=E5=BF=83=E5=85=A8?= =?UTF-8?q?=E9=83=A8=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BussDocumentLibraryEOServiceImpl.java | 66 +++++- .../impl/DocumentSearchServiceImpl.java | 221 +++--------------- 2 files changed, 90 insertions(+), 197 deletions(-) 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 63660c860..3deec4dae 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 @@ -2,6 +2,7 @@ package com.jero.modules.document.service.impl; import cn.hutool.core.util.ZipUtil; import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.aliyuncs.utils.IOUtils; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; @@ -190,6 +191,8 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl files = iOSSFileService.getFileInfosByConnectId(StringUtils.join(connectIdList, ",")); + List fileIdList = files.stream().map(OSSFile::getId).collect(Collectors.toList()); //删除文件 iOSSFileService.deleteByConnectIdList(connectIdList); //删除文档库数据 @@ -240,7 +243,9 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl mapFullText = new HashMap<>(); - mapFullText.put("id", idTemp); - mapFullText.put("module_type", "文档库"); - mapFullText.put("serial_number", map.get("serial_number")); - mapFullText.put("title", map.get("title")); - StringBuilder sb = new StringBuilder(); List> fileTextList = new ArrayList<>(); + List oSSFileAll = new ArrayList<>(); try { for (Map.Entry entry : map.entrySet()) { String key = entry.getKey(); @@ -1022,6 +1022,7 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl list = readFileContent(oSSFileList); if(list.size() != 0){ @@ -1039,8 +1040,50 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl mapFullText = new HashMap<>(); +// mapFullText.put("id", idTemp); +// mapFullText.put("module_type", "文档库"); +// mapFullText.put("serial_number", map.get("serial_number")); +// mapFullText.put("title", map.get("title")); +// mapFullText.put("file_text", fileTextList); +// mapFullText.put("content", sb.toString()); + //多文件es iOSSFileService.getFileInfos(StringUtils.join(fileIdList, ",")); + List fileIdList = oSSFileAll.stream().map(OSSFile::getId).collect(Collectors.toList()); + + List fileInfos = iOSSFileService.getFileInfos(StringUtils.join(fileIdList, ",")); + List> mapListTemp = new ArrayList<>(); + try { + for (OSSFile ossFile : fileInfos) { + Map stringMap = new HashMap<>(); + String fileText = ""; + 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)){ + fileText = wordContent; + } + }else if(ossFile.getFileName().endsWith(".pdf")||ossFile.getFileName().endsWith(".PDF")){ + String pdfContent = ReadPdfUtil.readPdf(ossFile.getUrl()); + if(StringUtils.isNotBlank(pdfContent)){ + fileText = pdfContent; + } + } + stringMap.put("id",idTemp+ossFile.getId()); + stringMap.put("module_type", "文档库"); + stringMap.put("serial_number", map.get("serial_number")); + stringMap.put("title", map.get("title")); + stringMap.put("content", sb.toString()); + stringMap.put("file_text",fileText); + stringMap.put("file_name",ossFile.getFileName()); + mapListTemp.add(stringMap); + } + } catch (Exception e) { + log.error("文档库新增es错误"); + } + + String insertField = mustFields + fieldsBuilder.toString(); String insertValue = mustValues + valuesBuilder.toString(); bussDocumentLibraryEOMapper.insertInfo(insertField, insertValue); @@ -1056,12 +1099,13 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl listTemp = new ArrayList<>(); -// listTemp.add("module_type"); -// listTemp.add("title"); -// listTemp.add("serial_number"); -// listTemp.add("file_text"); -// listTemp.add("content"); -// String indexName = "fullTextSearch"; -// String typeName = "fullTextSearch"; -// String selectValue = searchVO.getSelectValue(); -// selectValue = "废止"; -// Map highlightMap = new HashMap<>(); -// List> highlightList = new ArrayList<>(); -//// Map queryMap = new HashMap<>(); -// JSONArray queryMapJson = new JSONArray(); -// if("fullTextSearch".equals(indexName) && StringUtils.isNotBlank(selectValue)){ -// //封装高亮查询条件 -// Map map1 = new HashMap<>(); -//// Map map2 = new HashMap<>(); -//// map2.put("content",selectValue); -// Map map3 = new HashMap<>(); -// map3.put("pre_tags",""); -// map3.put("post_tags",""); -// for (String s : listTemp) { -// Map map4 = new HashMap<>(); -// Map queryMap = new HashMap<>(); -// Map map2 = new HashMap<>(); -// map1.put(s, map3); -// map4.put(s, map3); -// map2.put(s,selectValue); -// queryMap.put("match", map2); -// queryMapJson.add(queryMap); -// highlightList.add(map4); -// } -//// highlightMap.put("fields", highlightList); -// highlightMap.put("fields", map1); -// -// } - //以下是一个字段查询 -// String indexName = "fullTextSearch"; -// String typeName = "fullTextSearch"; -// String selectValue = searchVO.getSelectValue(); -// selectValue = "啊"; -// Map highlightMap = new HashMap<>(); -// Map queryMap = new HashMap<>(); -// JSONArray queryMapJson = new JSONArray(); -// if("fullTextSearch".equals(indexName) && StringUtils.isNotBlank(selectValue)){ -// //封装高亮查询条件 -// Map map1 = new HashMap<>(); -// Map map3 = new HashMap<>(); -// map3.put("pre_tags",""); -// map3.put("post_tags",""); -// map1.put("content", map3); -// highlightMap.put("fields", map1); -// -// //处理首次搜索和在结果中搜索 -// Map map2 = new HashMap<>(); -// map2.put("content",selectValue); -// queryMap.put("match", map2); -// } //判断索引是否存在 boolean flagTemp = jeroElasticsearchTemplate.indexExists(SearchEnum.FULL_TEXT_SEARCH.getValue()); if(!flagTemp){ @@ -298,6 +238,7 @@ public class DocumentSearchServiceImpl implements IDocumentSearchService { fieldList.add("serial_number"); fieldList.add("file_text"); fieldList.add("content"); + fieldList.add("file_name"); JSONArray queryMapJson = new JSONArray(); if(StringUtils.isNotBlank(selectValue)){ @@ -353,15 +294,21 @@ public class DocumentSearchServiceImpl implements IDocumentSearchService { int count = 0; for (Map.Entry entry : mapSource.entrySet()) { String key = entry.getKey(); - String value = null; - try { - value = (String) entry.getValue(); - } catch (Exception e) { - listNew.add(stringObjectMap); + String value = (String) entry.getValue(); + //二次搜索的file_text是在一次搜索的基础上 + if("file_text".equals(key)){ + //判断value中是否包含首次搜索中的字符 + boolean flag = judgeContains(value, selectValue); + if(flag){ + //截取第一处高亮的前后100个字符 + value = getFileText(value, selectValue); + } } //判断value中是否包含二次搜索中的字符 boolean flag = judgeContains(value, selectValueTwo); if (flag) { + //截取第一处高亮的前后100个字符 + value = getFileText(value, selectValue); count++; StringBuilder sb = new StringBuilder(); if (StringUtils.isNotBlank(value)) { @@ -382,34 +329,35 @@ public class DocumentSearchServiceImpl implements IDocumentSearchService { } else { //首次查询(没有输入查询条件) mapList.add(mapSource); - - //目前只能处理单字段高亮(多字段高亮问题解决后可以用此方法) - //----------------------------------------------------------------- -// Map mapHighlight = (Map) stringObjectMap.get("highlight"); - //content高亮处理() -// String content = ""; -// for (Map.Entry entry : mapHighlight.entrySet()) { -// List valueList = (ArrayList)entry.getValue(); -// for (String s : valueList) { -// content += s; -// } -// } - //----------------------------------------------------------------------------- //处理高亮module_type,title,serial_number,file_text(输入查询条件后,处理高亮字段) if(StringUtils.isNotBlank(selectValue)){ for (Map.Entry entry : mapSource.entrySet()) { String key = entry.getKey(); - String value = null; - try { - //文件内容为字符串格式 - value = (String) entry.getValue(); - } catch (Exception e) { - //文件内容为集合格式 - listNew.add(stringObjectMap); - e.printStackTrace(); - } + String value = (String) entry.getValue(); StringBuilder sb = new StringBuilder(); if (StringUtils.isNotBlank(value)) { + //file_text需要单独处理(截取第一处高亮的前后100个字符) + if("file_text".equals(key)){ + //判断value中是否包含搜索中的字符 + boolean flag = judgeContains(value, selectValue); + if(flag){ +// mapSource.put("file_text_flag","true"); + //截取第一处高亮的前后100个字符 + value = getFileText(value, selectValue); + } +// else{ +// mapSource.put("file_text_flag","flag"); +// } + } + if("content".equals(key)){ + //判断value中是否包含搜索中的字符 + boolean flag = judgeContains(value, selectValue); +// if(flag){ +// mapSource.put("content_flag","true"); +// }else{ +// mapSource.put("content_flag","false"); +// } + } for (int i = 0; i < value.length(); i++) { String chStr = String.valueOf(value.charAt(i)); if (selectValue.contains(String.valueOf(chStr))) { @@ -427,105 +375,6 @@ public class DocumentSearchServiceImpl implements IDocumentSearchService { } } - //若一条标准含有多条发布稿或报批稿等文件,需要对每个附件进行搜索并多行展示, - //例如: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) { - int count = 0; - StringBuilder sb = new StringBuilder(); - if (StringUtils.isNotBlank(s)) { - //判断value中是否包含二次搜索中的字符 - boolean flag = judgeContains(s, selectValueTwo); - if(flag){ - for (int i = 0; i < s.length(); i++) { - s = getFileText(s, selectValue); - 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); - idList.add(id); - } - } - } - } - } - }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) { - s = s.replace("\n",""); - int count = 0; - StringBuilder sb = new StringBuilder(); - if (StringUtils.isNotBlank(s)) { - //判断file_text中是否有搜索内容,如果有 - s = getFileText(s, selectValue); - 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); - idList.add(id); - } - } - } - } - } - } - } - 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());