文档库--删除es和搜索中心全部查询

This commit is contained in:
zhn
2022-03-23 15:52:20 +08:00
parent 4f1779048c
commit 7dc544f313
2 changed files with 90 additions and 197 deletions
@@ -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<BussDocumentLi
}
}
}
List<OSSFile> files = iOSSFileService.getFileInfosByConnectId(StringUtils.join(connectIdList, ","));
List<String> fileIdList = files.stream().map(OSSFile::getId).collect(Collectors.toList());
//删除文件
iOSSFileService.deleteByConnectIdList(connectIdList);
//删除文档库数据
@@ -240,7 +243,9 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl<BussDocumentLi
try {
for (String id : ids) {
jeroElasticsearchTemplate.delete(SearchEnum.INDEX_NAME_DOCUMENT.getValue(), SearchEnum.TYPE_NAME_DOCUMENT.getValue(), id);
jeroElasticsearchTemplate.delete(SearchEnum.FULL_TEXT_SEARCH.getValue(), SearchEnum.FULL_TEXT_SEARCH.getValue(), id);
for (String fileId : fileIdList) {
jeroElasticsearchTemplate.delete(SearchEnum.FULL_TEXT_SEARCH.getValue(), SearchEnum.FULL_TEXT_SEARCH.getValue(), id+fileId);
}
}
} catch (Exception e) {
log.error("文档库删除es失败");
@@ -980,14 +985,9 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl<BussDocumentLi
StringBuilder fieldsBuilder = new StringBuilder();
//数据值
StringBuilder valuesBuilder = new StringBuilder();
Map<String,Object> 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<List<String>> fileTextList = new ArrayList<>();
List<OSSFile> oSSFileAll = new ArrayList<>();
try {
for (Map.Entry<String, Object> entry : map.entrySet()) {
String key = entry.getKey();
@@ -1022,6 +1022,7 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl<BussDocumentLi
oSSFileList.add(ossFile);
}
if(oSSFileList.size() != 0){
oSSFileAll.addAll(oSSFileList);
//读取word或PDF文件内容
List<String> list = readFileContent(oSSFileList);
if(list.size() != 0){
@@ -1039,8 +1040,50 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl<BussDocumentLi
} catch (Exception e) {
e.printStackTrace();
}
mapFullText.put("file_text", fileTextList);
mapFullText.put("content", sb.toString());
// Map<String,Object> 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<String> fileIdList = oSSFileAll.stream().map(OSSFile::getId).collect(Collectors.toList());
List<OSSFile> fileInfos = iOSSFileService.getFileInfos(StringUtils.join(fileIdList, ","));
List<Map<String,Object>> mapListTemp = new ArrayList<>();
try {
for (OSSFile ossFile : fileInfos) {
Map<String,Object> 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<BussDocumentLi
map.put("id", idTemp);
map.put("module_type", "文档库");
JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(map));
JSONObject jsonObjectAll = JSON.parseObject(JSON.toJSONString(mapFullText));
// JSONObject jsonObjectAll = JSON.parseObject(JSON.toJSONString(mapFullText));
JSONArray array= JSONArray.parseArray(JSON.toJSONString(mapListTemp));
try {
jeroElasticsearchTemplate.saveOrUpdate(SearchEnum.INDEX_NAME_DOCUMENT.getValue(), SearchEnum.TYPE_NAME_DOCUMENT.getValue(), idTemp, jsonObject);
//全部
jeroElasticsearchTemplate.saveOrUpdate(SearchEnum.FULL_TEXT_SEARCH.getValue(), SearchEnum.FULL_TEXT_SEARCH.getValue(), idTemp, jsonObjectAll);
jeroElasticsearchTemplate.saveBatch(SearchEnum.FULL_TEXT_SEARCH.getValue(), SearchEnum.FULL_TEXT_SEARCH.getValue(), array);
} catch (Exception e) {
log.error("文档库添加es失败");
}
@@ -219,66 +219,6 @@ public class DocumentSearchServiceImpl implements IDocumentSearchService {
@Override
public IPage getFullTextInfoList(SearchVO searchVO) {
//多字段查询
// List<String> 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<String, Object> highlightMap = new HashMap<>();
// List<Map<String,Object>> highlightList = new ArrayList<>();
//// Map<String, Object> queryMap = new HashMap<>();
// JSONArray queryMapJson = new JSONArray();
// if("fullTextSearch".equals(indexName) && StringUtils.isNotBlank(selectValue)){
// //封装高亮查询条件
// Map<String, Object> map1 = new HashMap<>();
//// Map<String, Object> map2 = new HashMap<>();
//// map2.put("content",selectValue);
// Map<String,Object> map3 = new HashMap<>();
// map3.put("pre_tags","<text class='highlight-class'>");
// map3.put("post_tags","</text>");
// for (String s : listTemp) {
// Map<String, Object> map4 = new HashMap<>();
// Map<String, Object> queryMap = new HashMap<>();
// Map<String, Object> 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<String, Object> highlightMap = new HashMap<>();
// Map<String, Object> queryMap = new HashMap<>();
// JSONArray queryMapJson = new JSONArray();
// if("fullTextSearch".equals(indexName) && StringUtils.isNotBlank(selectValue)){
// //封装高亮查询条件
// Map<String, Object> map1 = new HashMap<>();
// Map<String,Object> map3 = new HashMap<>();
// map3.put("pre_tags","<text class='highlight-class'>");
// map3.put("post_tags","</text>");
// map1.put("content", map3);
// highlightMap.put("fields", map1);
//
// //处理首次搜索和在结果中搜索
// Map<String, Object> 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<String, Object> 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<String, Object> mapHighlight = (Map<String, Object>) stringObjectMap.get("highlight");
//content高亮处理()
// String content = "";
// for (Map.Entry<String, Object> entry : mapHighlight.entrySet()) {
// List<String> valueList = (ArrayList)entry.getValue();
// for (String s : valueList) {
// content += s;
// }
// }
//-----------------------------------------------------------------------------
//处理高亮module_type,title,serial_number,file_text(输入查询条件后,处理高亮字段)
if(StringUtils.isNotBlank(selectValue)){
for (Map.Entry<String, Object> 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<Map<String, Object>> result = new ArrayList<>();
List<String> idList = new ArrayList<>();
for (Map<String, Object> map : listNew) {
// idList.add((String) map.get("_id"));
Map<String, Object> mapSource = (Map<String, Object>) 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<String, Object> entry : mapSource.entrySet()) {
String key = entry.getKey();
if("file_text".equals(key)){
for (List<String> valueList : (List<List<String>>) 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 = "<text class='highlight-class'>" + chStr + "</text>";
count ++;
}
sb.append(String.valueOf(chStr));
}
}
}
if(count > 0){
Map<String,Object> 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<String, Object> entry : mapSource.entrySet()) {
String key = entry.getKey();
if("file_text".equals(key)){
for (List<String> valueList : (List<List<String>>) 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 = "<text class='highlight-class'>" + chStr + "</text>";
count ++;
}
sb.append(String.valueOf(chStr));
}
}
if(count > 0){
Map<String,Object> 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());