搜索中心-法规月报

This commit is contained in:
liyawei
2022-08-18 10:52:16 +08:00
parent c5682ef175
commit 7335445c40
5 changed files with 468 additions and 2 deletions
@@ -8,6 +8,8 @@ package com.jero.modules.document.enums;
public enum SearchEnum {
INDEX_NAME_DOCUMENT("文档库索引名称","documentLibrary"),
TYPE_NAME_DOCUMENT("文档库类型","document"),
INDEX_NAME_LAWS_MONTHLY_REPORT("法规月报索引名称","lawsMonthlyReportManage"),
TYPE_NAME_LAWS_MONTHLY_REPORT("法规月报类型名称","lawsMonthlyReport"),
FULL_TEXT_SEARCH("全部中文","fulltextsearchcn"),
FULL_TEXT_SEARCH_CN("全部中文","fulltextsearchcn"),
FULL_TEXT_SEARCH_EN("全部英文","fulltextsearchen");
@@ -1,20 +1,32 @@
package com.jero.modules.report.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jero.common.constant.enums.CutEnum;
import com.jero.common.es.JeroElasticsearchTemplate;
import com.jero.common.system.vo.LoginUser;
import com.jero.modules.document.enums.SearchEnum;
import com.jero.modules.document.utils.ReadPdfUtil;
import com.jero.modules.document.utils.ReadWordUtil;
import com.jero.modules.dummy.enums.InventoryStateEnum;
import com.jero.modules.oss.entity.OSSFile;
import com.jero.modules.oss.service.IOSSFileService;
import com.jero.modules.report.entity.LawsMonthlyReportManageEO;
import com.jero.modules.report.mapper.LawsMonthlyReportManageEOMapper;
import com.jero.modules.report.service.ILawsMonthlyReportManageEOService;
import com.jero.modules.system.service.ISysDictItemService;
import com.jero.modules.system.util.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
import java.util.*;
/**
* @Description: 法规月报管理
@@ -25,6 +37,21 @@ import java.util.List;
@Service
public class LawsMonthlyReportManageEOServiceImpl extends ServiceImpl<LawsMonthlyReportManageEOMapper, LawsMonthlyReportManageEO> implements ILawsMonthlyReportManageEOService {
@Autowired
private ISysDictItemService sysDictItemService;
@Autowired
private JeroElasticsearchTemplate jeroElasticsearchTemplate;
@Autowired
private IOSSFileService iOSSFileService;
@Value(value = "${jero.path.upload}")
private String uploadpath;
public static final String SEARCH_FLAG = "";//标识(es数据带此标识的代表全文和段落的数据,不带此标识的代表列表数据)
/**
* 保存
*
@@ -110,6 +137,126 @@ public class LawsMonthlyReportManageEOServiceImpl extends ServiceImpl<LawsMonthl
@Override
public void issue(LawsMonthlyReportManageEO lawsMonthlyReportManageEO) {
String id = lawsMonthlyReportManageEO.getId();
String issueStatus = lawsMonthlyReportManageEO.getIssueStatus();
LawsMonthlyReportManageEO monthlyReportManageEO = getById(id);
Date issueTime = new Date();
lawsMonthlyReportManageEO.setUpdateTime(issueTime);
this.updateById(lawsMonthlyReportManageEO);
if ("1".equals(issueStatus)) { // 已发布---发布操作
// 添加月报到es
OSSFile ossFile = iOSSFileService.getById(monthlyReportManageEO.getFileId());
String fileText = "";
// 读取月报文件内容
if (ossFile.getFileName().endsWith(".doc")
|| ossFile.getFileName().endsWith(".docx")
|| ossFile.getFileName().endsWith(".DOC")
|| ossFile.getFileName().endsWith(".DOCX")) {
String wordContent = null;
try {
wordContent = ReadWordUtil.readWord(ossFile.getUrl(),uploadpath);
} catch (Exception e) {
log.error("文档库: 读取word内容失败");
}
if (StringUtils.isNotBlank(wordContent)) {
fileText = wordContent;
}
} else if (ossFile.getFileName().endsWith(".pdf") || ossFile.getFileName().endsWith(".PDF")) {
String pdfContent = null;
try {
pdfContent = ReadPdfUtil.readPdf(ossFile.getUrl());
} catch (Exception e) {
log.error("文档库: 读取PDF文件内容失败");
}
if (StringUtils.isNotBlank(pdfContent)) {
fileText = pdfContent;
}
}
Map<String, Object> stringMapCn = new HashMap<>();
Map<String, Object> stringMapEn = new HashMap<>();
Map<String, Object> stringMapCnForSearch = new HashMap<>();
Map<String, Object> stringMapEnForSearch = new HashMap<>();
List<Map<String, Object>> mapListTempCn = new ArrayList<>();
List<Map<String, Object>> mapListTempEn = new ArrayList<>();
//文件相关封装中文的全文内容(es) 非搜索条件下的:flag---SEARCH_FLAG
putMap(id, "法规月报",
fileText, monthlyReportManageEO.getName(), fileText,
ossFile.getFileName(), ossFile.getId(), issueTime, CutEnum.CN.getValue(), stringMapCn, SEARCH_FLAG);
//文件相关封装英文的全文内容(es) 搜索条件下的: flag---null
putMap(id, "monthly report",
fileText, monthlyReportManageEO.getName(), fileText,
ossFile.getFileName(), ossFile.getId(), issueTime, CutEnum.EN.getValue(), stringMapEn, SEARCH_FLAG);
//文件相关封装中文的全文内容(es)
putMap(id + monthlyReportManageEO.getFileId(), "法规月报",
fileText, monthlyReportManageEO.getName(), fileText,
ossFile.getFileName(), ossFile.getId(), issueTime, CutEnum.CN.getValue(), stringMapCnForSearch, null);
//文件相关封装英文的全文内容(es)
putMap(id + monthlyReportManageEO.getFileId(), "monthly report",
fileText, monthlyReportManageEO.getName(), fileText,
ossFile.getFileName(), ossFile.getId(), issueTime, CutEnum.EN.getValue(), stringMapEnForSearch, null);
mapListTempCn.add(stringMapCn);
mapListTempCn.add(stringMapCnForSearch);
mapListTempEn.add(stringMapEn);
mapListTempEn.add(stringMapEnForSearch);
//添加es
JSONObject jsonObjectCn = JSON.parseObject(JSON.toJSONString(stringMapCn));
JSONObject jsonObjectEn = JSON.parseObject(JSON.toJSONString(stringMapEn));
JSONArray arrayCn = JSONArray.parseArray(JSON.toJSONString(mapListTempCn));
JSONArray arrayEn = JSONArray.parseArray(JSON.toJSONString(mapListTempEn));
try {
// 法规月报
jeroElasticsearchTemplate.saveOrUpdate(SearchEnum.INDEX_NAME_LAWS_MONTHLY_REPORT.getValue(), SearchEnum.TYPE_NAME_LAWS_MONTHLY_REPORT.getValue(), id, jsonObjectCn);
//全部
jeroElasticsearchTemplate.saveBatch(SearchEnum.FULL_TEXT_SEARCH_CN.getValue(), SearchEnum.FULL_TEXT_SEARCH_CN.getValue(), arrayCn);
jeroElasticsearchTemplate.saveBatch(SearchEnum.FULL_TEXT_SEARCH_EN.getValue(), SearchEnum.FULL_TEXT_SEARCH_EN.getValue(), arrayEn);
} catch (Exception e) {
log.error("文档库添加es失败");
}
} else if ("2".equals(issueStatus)) { // 待发布---撤回操作
// 删除es中的月报
try {
jeroElasticsearchTemplate.delete(SearchEnum.INDEX_NAME_LAWS_MONTHLY_REPORT.getValue(), SearchEnum.TYPE_NAME_LAWS_MONTHLY_REPORT.getValue(), id);
//删除全部中文的
jeroElasticsearchTemplate.delete(SearchEnum.FULL_TEXT_SEARCH.getValue(), SearchEnum.FULL_TEXT_SEARCH.getValue(), id);
jeroElasticsearchTemplate.delete(SearchEnum.FULL_TEXT_SEARCH_CN.getValue(), SearchEnum.FULL_TEXT_SEARCH_CN.getValue(), id);
jeroElasticsearchTemplate.delete(SearchEnum.FULL_TEXT_SEARCH_CN.getValue(), SearchEnum.FULL_TEXT_SEARCH_CN.getValue(), id + monthlyReportManageEO.getFileId());
//删除全部英文的
jeroElasticsearchTemplate.delete(SearchEnum.FULL_TEXT_SEARCH_EN.getValue(), SearchEnum.FULL_TEXT_SEARCH_EN.getValue(), id);
jeroElasticsearchTemplate.delete(SearchEnum.FULL_TEXT_SEARCH_EN.getValue(), SearchEnum.FULL_TEXT_SEARCH_EN.getValue(), id + monthlyReportManageEO.getFileId());
} catch (Exception e) {
e.printStackTrace();
log.error("文档库删除es失败");
}
}
}
private void putMap(String id, String moduleType,
String content, String title , String fileText,
String fileName, String fileId, Date issueTime, String cut,
Map<String, Object> stringMap,
String flag){
stringMap.put("id", id);
stringMap.put("module_type", moduleType);
stringMap.put("content", content);
stringMap.put("title", title);
stringMap.put("file_text", fileText);
stringMap.put("file_name", fileName);
stringMap.put("file_id", fileId);
stringMap.put("issue_time", issueTime);
stringMap.put("cut",cut);
stringMap.put("flag",flag);
}
}
@@ -0,0 +1,37 @@
package com.jero.modules.searchcenter.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jero.common.api.vo.Result;
import com.jero.modules.searchcenter.service.ILawsMonthlyReportSearchService;
import com.jero.modules.searchcenter.vo.SearchVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @description 搜索中心-法规月报 控制层
* @date 2022/8/17 15:03
* @auth liyawei
*/
@Api(tags="搜索中心")
@RestController
@RequestMapping("/search/lawsMonthlyReport")
@Slf4j
public class LawsMonthlyReportSearchController {
@Autowired
private ILawsMonthlyReportSearchService lawsMonthlyReportSearchService;
@ApiOperation(value="全部查询", notes="全部查询")
@PostMapping(value = "/page")
// @RequiresPermissions("lawsMonthlyReport:page")
public Result<?> queryPageInfo(@RequestBody SearchVO searchVO) {
IPage pageInfo = lawsMonthlyReportSearchService.pageInfo(searchVO);
return Result.OK(pageInfo);
}
}
@@ -0,0 +1,19 @@
package com.jero.modules.searchcenter.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jero.modules.searchcenter.vo.SearchVO;
/**
* @description 搜索中心-法规月报 接口
* @date 2022/8/17 15:03
* @auth liyawei
*/
public interface ILawsMonthlyReportSearchService {
/**
* 分页查询
* @param searchVO
* @return
*/
IPage pageInfo(SearchVO searchVO);
}
@@ -0,0 +1,261 @@
package com.jero.modules.searchcenter.service.impl;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jero.common.es.JeroElasticsearchTemplate;
import com.jero.modules.document.enums.SearchEnum;
import com.jero.modules.searchcenter.service.ILawsMonthlyReportSearchService;
import com.jero.modules.searchcenter.vo.SearchVO;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @description 搜索中心-法规月报 服务层
* @date 2022/8/17 15:03
* @auth liyawei
*/
@Component
public class LawsMonthlyReportSearchServiceImpl implements ILawsMonthlyReportSearchService {
@Autowired
private JeroElasticsearchTemplate jeroElasticsearchTemplate;
public static final String SEARCH_FLAG = "";//标识(es数据带此标识的代表全文和段落的数据,不带此标识的代表列表数据)
/**
* 全文
* @param searchVO
* @return
*/
@Override
public IPage pageInfo(SearchVO searchVO) {
JSONArray queryJsonMustNot = new JSONArray();
// if(StringUtils.isNotBlank(searchVO.getSelectValue())){
// Map<String,Object> map = new HashMap<>();
// Map<String,Object> map1 = new HashMap<>();
// Map<String,Object> map2 = new HashMap<>();
// map.put("query",SEARCH_FLAG);
// map1.put("flag",map);
// map2.put("match",map1);
// queryJsonMustNot.add(map2);
// }
if(StringUtils.isBlank(searchVO.getSelectValue())){
searchVO.setSelectValue(SEARCH_FLAG);
}
//判断索引是否存在
boolean flagTemp = true;
flagTemp = jeroElasticsearchTemplate.indexExists(SearchEnum.INDEX_NAME_LAWS_MONTHLY_REPORT.getValue());
if (!flagTemp) {
return new Page(searchVO.getPageNo(), searchVO.getPageSize());
}
String selectValue = searchVO.getSelectValue();
String selectValueTwo = searchVO.getSelectValueTwo();
//1. 需要查询的字段
List<String> fieldList = new ArrayList<>();
fieldList.add("title");//权重5
fieldList.add("file_text");//权重4
fieldList.add("content");//权重3
fieldList.add("file_name"); //权重2
fieldList.add("module_type");
fieldList.add("id");
fieldList.add("flag");
JSONArray queryMapJson = new JSONArray();
JSONArray queryMapJsonTwo = new JSONArray();
JSONArray queryMapJsonThree = new JSONArray();
JSONArray queryMapJsonAll = new JSONArray();
Map<String,Object> mapHighlight = new HashMap<>();
Map<String,Object> mapHighlight1 = new HashMap<>();
//封装首次的条件
if (StringUtils.isNotBlank(selectValue)) {
for (String field : fieldList) {
Map<String, Object> map2 = new HashMap<>();
Map<String, Object> map3 = new HashMap<>();
Map<String, Object> map4 = new HashMap<>();
if("title".equals(field)){
map3.put("boost",1);
}else if("file_text".equals(field)){
map3.put("boost",0.01);
}else if("content".equals(field)){
map3.put("boost",0.01);
}else if("file_name".equals(field)){
map3.put("boost",1);
}
map3.put("query",selectValue);
map2.put(field, map3);
map4.put("match",map2);
queryMapJson.add(map4);
//高亮
if(!"flag".equals(field)){
highlight(mapHighlight, field);
}
}
mapHighlight1.put("fields",mapHighlight);
}
if(CollectionUtils.isNotEmpty(queryMapJson)){
JSONObject jsonObject = jeroElasticsearchTemplate.buildBoolQuery(null, null, queryMapJson);
queryMapJsonAll.add(jsonObject);
}
//封装二次的条件
if (StringUtils.isNotBlank(selectValueTwo)) {
for (String field : fieldList) {
Map<String, Object> queryMap = new HashMap<>();
Map<String, Object> map2 = new HashMap<>();
Map<String, Object> map3 = new HashMap<>();
if("title".equals(field)){
map3.put("boost",1);
}else if("file_text".equals(field)){
map3.put("boost",0.01);
}else if("content".equals(field)){
map3.put("boost",0.01);
}else if("file_name".equals(field)){
map3.put("boost",1);
}
//特殊处理编号字段
map3.put("query",selectValueTwo);
// map3.put("minimum_should_match",2);
map2.put(field, map3);
queryMap.put("match", map2);
queryMapJsonTwo.add(queryMap);
//高亮
if(!"flag".equals(field)){
highlight(mapHighlight, field);
}
}
mapHighlight1.put("fields",mapHighlight);
}
if(CollectionUtils.isNotEmpty(queryMapJsonTwo)){
JSONObject jsonObjectTwo = jeroElasticsearchTemplate.buildBoolQuery(null, null, queryMapJsonTwo);
queryMapJsonAll.add(jsonObjectTwo);
}
//ids进行三次封装
if (StringUtils.isNotBlank(searchVO.getIds())) {
for (String s : searchVO.getIds().split(",")) {
Map<String, Object> queryMap = new HashMap<>();
Map<String, Object> queryMapTemp = new HashMap<>();
queryMapTemp.put("id", s);
queryMap.put("match_phrase", queryMapTemp);
queryMapJsonThree.add(queryMap);
}
}
if(CollectionUtils.isNotEmpty(queryMapJsonThree)){
JSONObject jsonObjectThree = jeroElasticsearchTemplate.buildBoolQuery(null, null, queryMapJsonThree);
queryMapJsonAll.add(jsonObjectThree);
}
JSONObject sort = new JSONObject();
Map<String,Object> issueTime = new HashMap<>();
Map<String,Object> issueTime1 = new HashMap<>();
issueTime.put("order","desc");
issueTime1.put("issue_time",issueTime);
sort.putAll(issueTime1);
// Map<String,Object> mapSort = new HashMap<>();
// Map<String,Object> mapSortTemp = new HashMap<>();
// mapSort.put("order","asc");
// mapSortTemp.put("file_sort.keyword",mapSort);
// sort.putAll(mapSortTemp);
Integer pageNo = searchVO.getPageNo();
Integer pageSize = searchVO.getPageSize();
IPage page = getiPage(selectValue, selectValueTwo, queryMapJsonAll, mapHighlight1,null,
pageNo, pageSize, null,searchVO.getCut(),sort,queryJsonMustNot);
return page;
}
@NotNull
private IPage getiPage(String selectValue, String selectValueTwo, JSONArray queryMapJson,Map<String,Object> highlightMap,
JSONArray should, Integer pageNo, Integer pageSize, String paragraphFlag,
String cut,JSONObject querySort,JSONArray queryMustNot) {
JSONObject jsonObject = new JSONObject();
//基础添加封装
if ("paragraphFlag".equals(paragraphFlag)) {
jsonObject = jeroElasticsearchTemplate.buildBoolQuery(queryMapJson, queryMustNot, null);
} else {
jsonObject = jeroElasticsearchTemplate.buildBoolQuery(queryMapJson, queryMustNot, null);
}
JSONArray jsonArraySort = new JSONArray();
jsonArraySort.add(querySort);
//1. 条件,分页
JSONObject queryObject = jeroElasticsearchTemplate.buildQuery(null,
jsonObject,
highlightMap,
jsonArraySort,
pageNo-1,
pageSize);
//2. 数据查询
JSONObject search = new JSONObject();
search = jeroElasticsearchTemplate.search(SearchEnum.INDEX_NAME_LAWS_MONTHLY_REPORT.getValue(),
SearchEnum.TYPE_NAME_LAWS_MONTHLY_REPORT.getValue(),
queryObject);
List<Map<String, Object>> list = (List<Map<String, Object>>) (((Map) search.get("hits")).get("hits"));
List<Map<String, Object>> mapList = new ArrayList<>();
for (Map<String, Object> map : list) {
Map<String, Object> mapSource = (Map<String, Object>) map.get("_source");
Map<String, Object> mapHighlight = (Map<String, Object>) map.get("highlight");
if(ObjectUtils.isNotEmpty(mapHighlight)){
for (Map.Entry<String, Object> entry : mapHighlight.entrySet()) {
String key = entry.getKey();
List<String> value = (List<String>) entry.getValue();
String fieldConyent= "";
for (String s : value) {
fieldConyent += s;
}
mapSource.put(key,fieldConyent);
}
//如果这一条数据中有高亮字段值,但是file_text中没有高亮值, 则赋空值,否则列表中会展示所有的文件内容, 赋空后,则展示基础数据
String fileText = (String) mapSource.get("file_text");
if(StringUtils.isNotBlank(fileText) && !fileText.contains("<text class='highlight-class'>")){
mapSource.put("file_text","");
}
}
mapList.add(mapSource);
}
//处理分页
IPage page = new Page(pageNo, pageSize);
if (StringUtils.isNotBlank(selectValueTwo)) {
//二次搜索的结果
page.setTotal(Long.parseLong(String.valueOf(((Map) search.get("hits")).get("total"))));
page.setRecords(mapList);
} else {
//首次搜索的结果
page.setTotal(Long.parseLong(String.valueOf(((Map) search.get("hits")).get("total"))));
page.setRecords(mapList);
}
return page;
}
private void highlight(Map<String, Object> mapHighlight, String key) {
Map<String, Object> mapTemp = new HashMap<>();
List<String> list = new ArrayList<>();
list.add("<text class='highlight-class'>");
List<String> list1 = new ArrayList<>();
list1.add("</text>");
mapTemp.put("pre_tags", list);
mapTemp.put("post_tags", list1);
mapTemp.put("fragment_size", 200);//高亮字段内容长度,去除html样式标签,统计字数,设置为200
mapTemp.put("number_of_fragments", 1);//高亮内容默认分为5段, 此处设置为一段
mapTemp.put("type", "plain");
mapHighlight.put(key, mapTemp);
}
}