一般检索(全文)添加

This commit is contained in:
梁琦涛
2023-11-17 16:52:46 +08:00
parent bfac88b257
commit 8091cf9527
8 changed files with 741 additions and 65 deletions
@@ -25,4 +25,19 @@ public class LawsHomeSearchCommon {
* 动态消息
*/
public static final String LAWS_NEWS_FEED = "4";
/**
* 操作类型,1add 2update 3delete
*/
public static final String LAWS_ES_ADD = "1";
/**
* 操作类型,1add 2update 3delete
*/
public static final String LAWS_ES_UPDATE = "2";
/**
* 操作类型,1add 2update 3delete
*/
public static final String LAWS_ES_DELETE = "3";
}
@@ -10,12 +10,10 @@ import com.jero.modules.laws.home.vo.LawsHomeNormalSearchVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Map;
/**
* @author lqt
@@ -55,7 +53,6 @@ public class LawsHomeSearchController {
public Result<IPage<LawsHomeNormalSearchVO>> getNormalSearchAllPage(LawsHomeNormalSearchDTO homeNormalSearchDTO,
@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
// todo 待开发
return Result.OK(lawsHomeSearchService.getNormalSearchAllPage(homeNormalSearchDTO,pageNo,pageSize));
}
@@ -67,13 +64,11 @@ public class LawsHomeSearchController {
return Result.OK(lawsHomeSearchService.getNormalSearchAllCount(homeNormalSearchDTO));
}
@AutoLog(value = "首页-高级检索查询",operateType = 1)
@ApiOperation(value="首页-高级检索查询", notes="首页-高级检索查询")
@GetMapping(value = "/getAdvancedSearchPage")
public Result<IPage<LawsHomeNormalSearchVO>> getAdvancedSearchPage(LawsHomeNormalSearchDTO homeNormalSearchDTO,
@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
// todo 待开发
return Result.OK(lawsHomeSearchService.getAdvancedSearchPage(homeNormalSearchDTO,pageNo,pageSize));
@AutoLog(value = "首页-一般检索(全文)测试",operateType = 1)
@ApiOperation(value="首页-一般检索(全文)测试", notes="首页-一般检索(全文)测试")
@PostMapping(value = "/getNormalSearchAllUpdate")
public Result<String> getNormalSearchAllUpdate(@RequestBody Map<String,String> map) {
lawsHomeSearchService.updateEsData(map.get("resourceType"),map.get("ids") ,map.get("opType"));
return Result.OK("操作成功");
}
}
@@ -0,0 +1,28 @@
package com.jero.modules.laws.home.dto;
import lombok.Data;
import java.io.Serializable;
/**
* @author lqt
* @version 1.0
* @date 2023/11/17 14:42
*/
@Data
public class SearchVO implements Serializable {
/**
* 记录首页搜索框中对应的数据
*/
private String selectValue;
/**
* 在结果中搜索(ids)
*/
private String ids;
/**
* 记录从哪个索引表中查数据
*/
private String selectIndex;
private Integer pageNo;
private Integer pageSize;
}
@@ -0,0 +1,27 @@
package com.jero.modules.laws.home.enums;
/**
* @author lqt
* @version 1.0
* @date 2023/11/17 9:15
*/
public enum HomeSearchEnum {
INDEX_NAME_DOCUMENT_ZQ("全文标准名称","documentLibraryZq"),
TYPE_NAME_DOCUMENT_ZQ("全文标准类型","documentZq");
String name;
String value;
HomeSearchEnum(String name, String value) {
this.name = name;
this.value = value;
}
public String getName() {
return name;
}
public String getValue() {
return value;
}
}
@@ -32,10 +32,34 @@ public interface ILawsHomeSearchService {
*/
LawsHomeNormalSearchStatisticsVO getNormalSearchTitleCount(LawsHomeNormalSearchDTO lawsHomeNormalSearchDTO);
/**
* 首页-一般检索(全文)查询
* @author LQT
* @date 2023/11/17 8:45
* @param homeNormalSearchDTO
* @param pageNo
* @param pageSize
* @return com.baomidou.mybatisplus.core.metadata.IPage<com.jero.modules.laws.home.vo.LawsHomeNormalSearchVO>
*/
IPage<LawsHomeNormalSearchVO> getNormalSearchAllPage(LawsHomeNormalSearchDTO homeNormalSearchDTO,Integer pageNo,Integer pageSize);
/**
* 首页-一般检索(全文)(数据统计)
* @author LQT
* @date 2023/11/17 8:45
* @param homeNormalSearchDTO
* @return com.jero.modules.laws.home.vo.LawsHomeNormalSearchStatisticsVO
*/
LawsHomeNormalSearchStatisticsVO getNormalSearchAllCount(LawsHomeNormalSearchDTO homeNormalSearchDTO);
IPage<LawsHomeNormalSearchVO> getAdvancedSearchPage(LawsHomeNormalSearchDTO homeNormalSearchDTO,Integer pageNo,Integer pageSize);
/**
* 同步Elasticsearch数据
* @author LQT
* @date 2023/11/17 8:48
* @param resourceType 资源类型 (数据字典 resource_type 可以取LawsHomeSearchCommon类中的常量值
* @param ids 多数据逗号分隔
* @param opType 操作类型,1add 2update 3delete 可以取LawsHomeSearchCommon类中的常量值
* @return void
*/
void updateEsData(String resourceType,String ids,String opType);
}
@@ -1,16 +1,25 @@
package com.jero.modules.laws.home.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.jero.common.es.JeroElasticsearchTemplate;
import com.jero.common.exception.JeroBootException;
import com.jero.common.system.api.ISysBaseAPI;
import com.jero.common.system.vo.DictModel;
import com.jero.common.system.vo.LoginUser;
import com.jero.common.util.DateUtils;
import com.jero.modules.document.utils.ReadPdfUtil;
import com.jero.modules.document.utils.ReadWordUtil;
import com.jero.modules.laws.home.common.LawsHomeSearchCommon;
import com.jero.modules.laws.home.dto.LawsHomeNormalSearchDTO;
import com.jero.modules.laws.home.dto.SearchVO;
import com.jero.modules.laws.home.entity.LawsHomeNormalSearchHistory;
import com.jero.modules.laws.home.enums.HomeSearchEnum;
import com.jero.modules.laws.home.mapper.LawsHomeSearchMapper;
import com.jero.modules.laws.home.service.ILawsHomeNormalSearchHistoryService;
import com.jero.modules.laws.home.service.ILawsHomeSearchService;
@@ -25,19 +34,26 @@ import com.jero.modules.laws.standard.service.ILawsDomesticStandardService;
import com.jero.modules.laws.standard.service.ILawsEnterpriseStandardService;
import com.jero.modules.laws.standard.service.ILawsNewsFeedService;
import com.jero.modules.laws.standard.service.ILawsOverseasStandardService;
import com.jero.modules.oss.entity.OSSFile;
import com.jero.modules.oss.service.IOSSFileService;
import com.jero.modules.system.entity.SysDepart;
import com.jero.modules.system.entity.SysUser;
import com.jero.modules.system.service.ISysDepartService;
import com.jero.modules.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ForkJoinPool;
import java.util.stream.Collectors;
/**
@@ -45,6 +61,7 @@ import java.util.stream.Collectors;
* @version 1.0
* @date 2023/11/7 17:00
*/
@Slf4j
@Service
public class LawsHomeSearchServiceImpl implements ILawsHomeSearchService {
@@ -66,6 +83,15 @@ public class LawsHomeSearchServiceImpl implements ILawsHomeSearchService {
private ISysUserService sysUserService;
@Resource
private ISysDepartService sysDepartService;
@Resource
private IOSSFileService oSSFileService;
@Resource
private JeroElasticsearchTemplate jeroElasticsearchTemplate;
@Value(value = "${jero.path.upload}")
private String uploadpath;
public static final String SEARCH_FLAG = "";//标识(es数据带此标识的代表全文和段落的数据,不带此标识的代表列表数据)
@Override
@Transactional(rollbackFor = JeroBootException.class)
@@ -104,6 +130,8 @@ public class LawsHomeSearchServiceImpl implements ILawsHomeSearchService {
queryWrapper.eq(!StringUtils.isEmpty(lawsHomeNormalSearchDTO.getGradeClassification()),"grade_classification",lawsHomeNormalSearchDTO.getGradeClassification());
queryWrapper.orderByAsc("resource_type");
queryWrapper.orderByDesc("standard_type");
// todo 排序号
// queryWrapper.orderByDesc("standard_type");
queryWrapper.orderByAsc("CHAR_LENGTH(standard_number)");
queryWrapper.orderByAsc("CHAR_LENGTH(standard_name)");
return queryWrapper;
@@ -297,6 +325,13 @@ public class LawsHomeSearchServiceImpl implements ILawsHomeSearchService {
return str;
}
private String getDict(String str,String code){
if(StringUtils.isBlank(str)){
str = "--";
}
return str;
}
@Override
public LawsHomeNormalSearchStatisticsVO getNormalSearchTitleCount(LawsHomeNormalSearchDTO lawsHomeNormalSearchDTO) {
String searchContent = lawsHomeNormalSearchDTO.getSearchContent();
@@ -376,19 +411,7 @@ public class LawsHomeSearchServiceImpl implements ILawsHomeSearchService {
List<LawsHomeNormalSearchStatisticsObjVO> listDict = new ArrayList<>();
// 标准类别
List<DictModel> listStandardClass = sysBaseAPI.getDictItems("standard_type_overseas");
for (DictModel dict : listStandardClass) {
LawsHomeNormalSearchStatisticsObjVO objVO = new LawsHomeNormalSearchStatisticsObjVO();
objVO.setItemText(dict.getText());
objVO.setItemValue(dict.getValue());
if(CollectionUtils.isEmpty(list)){
objVO.setStatisticsCount("0");
}else{
long l = list.stream().filter(o->Objects.equals(o.getStandardClass(),dict.getValue())).count();
objVO.setStatisticsCount(String.valueOf(l));
}
listDict.add(objVO);
}
obj.setStandardClass(listDict);
addStandardClass(list, obj, listDict, listStandardClass);
listDict = new ArrayList<>();
// 适用车辆类型
List<DictModel> listApplicableVehicle = sysBaseAPI.getDictItems("APPLICATIONS_OVERSEAS");
@@ -427,22 +450,57 @@ public class LawsHomeSearchServiceImpl implements ILawsHomeSearchService {
List<LawsHomeNormalSearchStatisticsObjVO> listDict = new ArrayList<>();
// 标准类别
List<DictModel> listStandardClass = sysBaseAPI.getDictItems("standard_type");
for (DictModel dict : listStandardClass) {
addStandardClass(list, obj, listDict, listStandardClass);
listDict = new ArrayList<>();
// 动力类型
List<DictModel> listDynamicType = sysBaseAPI.getDictItems("POWER_TYPE_DOMESTIC");
addDynamicType(list, obj, listDict, listDynamicType);
listDict = new ArrayList<>();
// 适用车辆类型
addApplicableVehicle(list, obj, listDict);
listDict = new ArrayList<>();
// 标准类型
addStandardProperty(list, obj, listDict);
listDict = new ArrayList<>();
// 状态
addStandardState(list, obj, listDict);
}
private void addStandardState(List<LawsHomeNormalSearchVO> list, LawsHomeNormalSearchStatisticsVO obj, List<LawsHomeNormalSearchStatisticsObjVO> listDict) {
List<DictModel> listStandardState = sysBaseAPI.getDictItems("standard_state");
for (DictModel dict : listStandardState) {
LawsHomeNormalSearchStatisticsObjVO objVO = new LawsHomeNormalSearchStatisticsObjVO();
objVO.setItemText(dict.getText());
objVO.setItemValue(dict.getValue());
if(CollectionUtils.isEmpty(list)){
objVO.setStatisticsCount("0");
}else{
long l = list.stream().filter(o->Objects.equals(o.getStandardClass(),dict.getValue())).count();
long l = list.stream().filter(o-> Objects.equals(o.getStandardState(),dict.getValue())).count();
objVO.setStatisticsCount(String.valueOf(l));
}
listDict.add(objVO);
}
obj.setStandardClass(listDict);
listDict = new ArrayList<>();
// 动力类型
List<DictModel> listDynamicType = sysBaseAPI.getDictItems("POWER_TYPE_DOMESTIC");
obj.setStandardState(listDict);
}
private void addStandardProperty(List<LawsHomeNormalSearchVO> list, LawsHomeNormalSearchStatisticsVO obj, List<LawsHomeNormalSearchStatisticsObjVO> listDict) {
List<DictModel> listStandardProperty = sysBaseAPI.getDictItems("standard_property");
for (DictModel dict : listStandardProperty) {
LawsHomeNormalSearchStatisticsObjVO objVO = new LawsHomeNormalSearchStatisticsObjVO();
objVO.setItemText(dict.getText());
objVO.setItemValue(dict.getValue());
if(CollectionUtils.isEmpty(list)){
objVO.setStatisticsCount("0");
}else{
long l = list.stream().filter(o-> Objects.equals(o.getStandardProperty(),dict.getValue())).count();
objVO.setStatisticsCount(String.valueOf(l));
}
listDict.add(objVO);
}
obj.setStandardProperty(listDict);
}
private void addDynamicType(List<LawsHomeNormalSearchVO> list, LawsHomeNormalSearchStatisticsVO obj, List<LawsHomeNormalSearchStatisticsObjVO> listDict, List<DictModel> listDynamicType) {
for (DictModel dict : listDynamicType) {
LawsHomeNormalSearchStatisticsObjVO objVO = new LawsHomeNormalSearchStatisticsObjVO();
objVO.setItemText(dict.getText());
@@ -456,8 +514,9 @@ public class LawsHomeSearchServiceImpl implements ILawsHomeSearchService {
listDict.add(objVO);
}
obj.setDynamicType(listDict);
listDict = new ArrayList<>();
// 适用车辆类型
}
private void addApplicableVehicle(List<LawsHomeNormalSearchVO> list, LawsHomeNormalSearchStatisticsVO obj, List<LawsHomeNormalSearchStatisticsObjVO> listDict) {
List<DictModel> listApplicableVehicle = sysBaseAPI.getDictItems("APPLICATIONS_DOMESTIC");
for (DictModel dict : listApplicableVehicle) {
LawsHomeNormalSearchStatisticsObjVO objVO = new LawsHomeNormalSearchStatisticsObjVO();
@@ -472,43 +531,213 @@ public class LawsHomeSearchServiceImpl implements ILawsHomeSearchService {
listDict.add(objVO);
}
obj.setApplicableVehicle(listDict);
listDict = new ArrayList<>();
// 标准类型
List<DictModel> listStandardProperty = sysBaseAPI.getDictItems("standard_property");
for (DictModel dict : listStandardProperty) {
}
private void addStandardClass(List<LawsHomeNormalSearchVO> list, LawsHomeNormalSearchStatisticsVO obj, List<LawsHomeNormalSearchStatisticsObjVO> listDict, List<DictModel> listStandardClass) {
for (DictModel dict : listStandardClass) {
LawsHomeNormalSearchStatisticsObjVO objVO = new LawsHomeNormalSearchStatisticsObjVO();
objVO.setItemText(dict.getText());
objVO.setItemValue(dict.getValue());
if(CollectionUtils.isEmpty(list)){
if (CollectionUtils.isEmpty(list)) {
objVO.setStatisticsCount("0");
}else{
long l = list.stream().filter(o->Objects.equals(o.getStandardProperty(),dict.getValue())).count();
} else {
long l = list.stream().filter(o -> Objects.equals(o.getStandardClass(), dict.getValue())).count();
objVO.setStatisticsCount(String.valueOf(l));
}
listDict.add(objVO);
}
obj.setStandardProperty(listDict);
listDict = new ArrayList<>();
// 状态
List<DictModel> listStandardState = sysBaseAPI.getDictItems("standard_state");
for (DictModel dict : listStandardState) {
LawsHomeNormalSearchStatisticsObjVO objVO = new LawsHomeNormalSearchStatisticsObjVO();
objVO.setItemText(dict.getText());
objVO.setItemValue(dict.getValue());
if(CollectionUtils.isEmpty(list)){
objVO.setStatisticsCount("0");
}else{
long l = list.stream().filter(o->Objects.equals(o.getStandardState(),dict.getValue())).count();
objVO.setStatisticsCount(String.valueOf(l));
}
listDict.add(objVO);
}
obj.setStandardState(listDict);
obj.setStandardClass(listDict);
}
@Override
public IPage<LawsHomeNormalSearchVO> getNormalSearchAllPage(LawsHomeNormalSearchDTO homeNormalSearchDTO,Integer pageNo,Integer pageSize) {
return null;
String searchContent = homeNormalSearchDTO.getSearchContent();
if(StringUtils.isBlank(searchContent)){
throw new JeroBootException("请输入检索内容");
}
// 添加检索历史
LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
LawsHomeNormalSearchHistory lawsHomeNormalSearchHistory = new LawsHomeNormalSearchHistory();
lawsHomeNormalSearchHistory.setUserId(loginUser.getId());
lawsHomeNormalSearchHistory.setSearchContent(searchContent);
lawsHomeNormalSearchHistoryService.save(lawsHomeNormalSearchHistory);
SearchVO searchVO = new SearchVO();
searchVO.setSelectValue(homeNormalSearchDTO.getSearchContent());
searchVO.setPageNo(pageNo);
searchVO.setPageSize(pageSize);
JSONArray queryJsonMustNot = new JSONArray();
if(StringUtils.isBlank(searchVO.getSelectValue())){
searchVO.setSelectValue(SEARCH_FLAG);
}
//判断索引是否存在
boolean flagTemp;
flagTemp = jeroElasticsearchTemplate.indexExists(HomeSearchEnum.INDEX_NAME_DOCUMENT_ZQ.getValue());
if (!flagTemp) {
return new Page(searchVO.getPageNo(), searchVO.getPageSize());
}
String selectValue = searchVO.getSelectValue();
//1. 需要查询的字段
List<String> fieldList = new ArrayList<>();
fieldList.add("content");//权重1
JSONArray queryMapJson = new JSONArray();
JSONArray queryMapJsonAll = new JSONArray();
Map<String,Object> mapHighlight = new HashMap<>();
Map<String,Object> mapHighlight1 = new HashMap<>();
//封装首次的条件
if (StringUtils.isNotBlank(selectValue)) {
selectValue = selectValue.toLowerCase();
for (String field : fieldList) {
getQueryMapJson(queryMapJson, selectValue, field);
//高亮
if(!"flag".equals(field)){
highlight(mapHighlight, field);
} else {
// query_string查询
Map<String, Object> map25 = new HashMap<>();
Map<String, Object> map45 = new HashMap<>();
map25.put("query", selectValue);
map25.put("fields", fieldList.toArray());
map25.put("allow_leading_wildcard", false); //禁用了前置通配符
map45.put("query_string", map25);
queryMapJson.add(map45);
}
}
mapHighlight1.put("fields",mapHighlight);
}
if(CollectionUtils.isNotEmpty(queryMapJson)){
JSONObject jsonObject = jeroElasticsearchTemplate.buildBoolQuery(null, null, queryMapJson);
queryMapJsonAll.add(jsonObject);
}
JSONObject sort = new JSONObject();
Map<String,Object> issueTimeDesc = new HashMap<>();
Map<String,Object> issueTimeAsc = new HashMap<>();
Map<String,Object> issueTime = new HashMap<>();
issueTimeDesc.put("order","desc");
issueTimeAsc.put("order","asc");
issueTime.put("resource_type",issueTimeAsc);
issueTime.put("standard_type",issueTimeDesc);
// todo 排序号
// issueTime.put("resource_type",issueTime);
issueTime.put("create_time",issueTimeDesc);
sort.putAll(issueTime);
return getIPage(selectValue, queryMapJsonAll, mapHighlight1,
pageNo, pageSize, sort,queryJsonMustNot);
}
@NotNull
private IPage getIPage(String selectValue,JSONArray queryMapJson,Map<String,Object> highlightMap, Integer pageNo, Integer pageSize,
JSONObject querySort,JSONArray queryMustNot) {
JSONObject jsonObject;
//基础添加封装
jsonObject = jeroElasticsearchTemplate.buildBoolQuery(queryMapJson, queryMustNot, null);
JSONArray jsonArraySort = new JSONArray();
if (SEARCH_FLAG.equals(selectValue)) {
jsonArraySort.add(querySort);
}
//1. 条件,分页
JSONObject queryObject = jeroElasticsearchTemplate.buildQuery(null, jsonObject, highlightMap, jsonArraySort, pageNo - 1, pageSize);
//2. 数据查询
JSONObject search = jeroElasticsearchTemplate.search(HomeSearchEnum.INDEX_NAME_DOCUMENT_ZQ.getValue(), HomeSearchEnum.TYPE_NAME_DOCUMENT_ZQ.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);
}
}
String createTime = "";
if(ObjectUtils.isNotEmpty(mapSource.get("create_time"))){
createTime = DateUtils.formatTime((Long) mapSource.get("create_time"));
}
mapSource.put("create_time",createTime);
mapList.add(mapSource);
}
//处理分页
IPage page = new Page(pageNo, pageSize);
String total = JSON.parseObject(JSON.toJSONString(JSON.parseObject(JSON.toJSONString(search.get("hits"))).get("total"))).get("value").toString();
//首次搜索的结果
page.setTotal(Long.parseLong(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", 500);//高亮字段内容长度,去除html样式标签,统计字数,设置为200
mapTemp.put("number_of_fragments", 1);//高亮内容默认分为5段, 此处设置为一段
mapTemp.put("type", "plain");
mapHighlight.put(key, mapTemp);
}
private void getQueryMapJson(JSONArray queryMapJson, String selectValue, String field) {
// 前缀匹配 缺点是前缀一定不能断开
//情况举例:用户只记得前面那段字
Map<String, Object> map21 = new HashMap<>();
Map<String, Object> map31 = new HashMap<>();
Map<String, Object> map41 = new HashMap<>();
if("content".equals(field)){
map31.put("boost",0.01);
}
map31.put("value", selectValue);
map21.put(field + ".keyword", map31);
map41.put("prefix", map21);
queryMapJson.add(map41);
// match_phrase_prefix 词组匹配查询,允许最后词组与文中的任意分词前缀匹配
Map<String, Object> map22 = new HashMap<>();
Map<String, Object> map32 = new HashMap<>();
Map<String, Object> map42 = new HashMap<>();
if("content".equals(field)){
map32.put("boost",0.01);
}
map32.put("query", selectValue);
map22.put(field, map32);
map42.put("match_phrase_prefix", map22);
queryMapJson.add(map42);
// match分词匹配查询
// 情况举例:用户可能他知道开头的前缀几个字,知道中间的几个字
Map<String, Object> map23 = new HashMap<>();
Map<String, Object> map33 = new HashMap<>();
Map<String, Object> map43 = new HashMap<>();
if("content".equals(field)){
map33.put("boost",0.01);
}
map33.put("query", selectValue);
map23.put(field, map33);
map43.put("match", map23);
queryMapJson.add(map43);
// wildcard模糊查询
//情况举例:用户只记得中间那段字
Map<String, Object> map24 = new HashMap<>();
Map<String, Object> map44 = new HashMap<>();
map24.put(field, "*" + selectValue + "*");
map44.put("wildcard",map24);
queryMapJson.add(map44);
}
@Override
@@ -517,7 +746,360 @@ public class LawsHomeSearchServiceImpl implements ILawsHomeSearchService {
}
@Override
public IPage<LawsHomeNormalSearchVO> getAdvancedSearchPage(LawsHomeNormalSearchDTO homeNormalSearchDTO,Integer pageNo,Integer pageSize) {
return null;
public void updateEsData(String resourceType, String ids, String opType) {
if(StringUtils.isBlank(resourceType)){
throw new JeroBootException("资源类型不能为空");
}
if(StringUtils.isBlank(resourceType)){
throw new JeroBootException("资源ID不能为空");
}
if(StringUtils.isBlank(resourceType)){
throw new JeroBootException("操作类型不能为空");
}
log.info("================ES更新数据开始=================");
switch (resourceType){
case LawsHomeSearchCommon.LAWS_DOMESTIC_STANDARD:
updateLawsDomesticStandardToEs(resourceType,ids,opType);
break;
case LawsHomeSearchCommon.LAWS_OVERSEAS_STANDARD:
updateLawsOverseasStandardToEs(resourceType,ids,opType);
break;
case LawsHomeSearchCommon.LAWS_ENTERPRISE_STANDARD:
updateLawsEnterpriseStandardToEs(resourceType,ids,opType);
break;
case LawsHomeSearchCommon.LAWS_NEWS_FEED:
updateLawsNewsFeedToEs(resourceType,ids,opType);
break;
default:break;
}
log.info("================ES更新数据结束=================");
}
private boolean updateLawsDomesticStandardToEs(String resourceType, String ids,String opType) {
// 批量操作
// 从第0个下标开始
int startIndex = 0;
int batchCount = 2000;
List<String> listIds = Arrays.asList(ids.split(","));
ForkJoinPool pool = new ForkJoinPool();
if(CollectionUtils.isEmpty(listIds)) {
return true;
}
// 异步执行
while (startIndex < listIds.size()) {
int endIndex = getEndIndex(startIndex, batchCount, listIds);
List<String> listIdsSub = listIds.subList(startIndex, endIndex);
List<LawsDomesticStandard> listLawsDomesticStandard = lawsDomesticStandardService.listByIds(listIdsSub);
if(CollectionUtils.isEmpty(listLawsDomesticStandard)){
continue;
}
CompletableFuture.runAsync(()->{
for (LawsDomesticStandard lawsDomesticStandard : listLawsDomesticStandard) {
try {
LawsHomeNormalSearchVO obj = new LawsHomeNormalSearchVO();
obj.setId(lawsDomesticStandard.getId());
obj.setTitle(lawsDomesticStandard.getStandardNumber() + " " + lawsDomesticStandard.getStandardName());
obj.setResourceType(resourceType);
obj.setReleaseDate(lawsDomesticStandard.getReleaseDate());
obj.setStandardClass(lawsDomesticStandard.getStandardClass());
obj.setDynamicType(lawsDomesticStandard.getDynamicType());
obj.setApplicableVehicle(lawsDomesticStandard.getApplicableVehicle());
obj.setStandardProperty(lawsDomesticStandard.getStandardProperty());
obj.setStandardState(lawsDomesticStandard.getStandardState());
if(Objects.equals(lawsDomesticStandard.getStandardClass(),"GB")){
obj.setStandardType("2");
}else if(Objects.equals(lawsDomesticStandard.getStandardClass(),"GB/T")){
obj.setStandardType("1");
}
List<OSSFile> listFile = getOssFilesLawsDomesticStandard(lawsDomesticStandard);
updateES(opType, obj, listFile, lawsDomesticStandard.getId());
}catch (Exception e){
log.error("同步ES数据异常:",e.getMessage());
}
}
},pool);
// 下一批
startIndex = startIndex + batchCount;
}
return false;
}
@NotNull
private List<OSSFile> getOssFilesLawsDomesticStandard(LawsDomesticStandard lawsDomesticStandard) {
// 从过程稿件从上到下顺序检索;如果文档里有发布稿,只检索发布稿
LambdaQueryWrapper<OSSFile> ossFile = new LambdaQueryWrapper<>();
ossFile.eq(OSSFile::getStandardId,lawsDomesticStandard.getId());
ossFile.orderByAsc(OSSFile::getCreateTime);
List<OSSFile> list = oSSFileService.list(ossFile);
List<OSSFile> listFile = new ArrayList<>();
if(!CollectionUtils.isEmpty(list)){
List<OSSFile> op = list.stream().filter(o-> Objects.equals(o.getStandardFileType(),"publish_of_original")).collect(Collectors.toList());
// 有发布稿
if(!CollectionUtils.isEmpty(op)){
listFile.addAll(op);
}else{
getOp(list, listFile, "modification_list");
getOp(list, listFile, "draft_for_review");
getOp(list, listFile, "draft_for_approval");
getOp(list, listFile, "draft_for_comment");
getOp(list, listFile, "draft");
}
}
return listFile;
}
private int getEndIndex(int startIndex, int batchCount, List<String> listIds) {
int endIndex = 0;
if (listIds.size() - batchCount < startIndex) {
endIndex = listIds.size();
} else {
endIndex = startIndex + batchCount;
}
return endIndex;
}
private void updateES(String opType, LawsHomeNormalSearchVO obj, List<OSSFile> listFile, String id) {
if (Objects.equals(opType, LawsHomeSearchCommon.LAWS_ES_ADD) ||
Objects.equals(opType, LawsHomeSearchCommon.LAWS_ES_UPDATE)) {
updateElasticsearch(obj, listFile);
} else if (Objects.equals(opType, LawsHomeSearchCommon.LAWS_ES_DELETE)) {
jeroElasticsearchTemplate.delete(HomeSearchEnum.INDEX_NAME_DOCUMENT_ZQ.getValue(), HomeSearchEnum.TYPE_NAME_DOCUMENT_ZQ.getValue(), id);
}
}
private void updateLawsOverseasStandardToEs(String resourceType, String ids,String opType) {
// 批量操作
// 从第0个下标开始
int startIndex = 0;
int batchCount = 2000;
List<String> listIds = Arrays.asList(ids.split(","));
ForkJoinPool pool = new ForkJoinPool();
// 异步执行
while (startIndex < listIds.size()) {
int endIndex = getEndIndex(startIndex, batchCount, listIds);
List<String> listIdsSub = listIds.subList(startIndex, endIndex);
List<LawsOverseasStandard> listLawsOverseasStandard = lawsOverseasStandardService.listByIds(listIdsSub);
CompletableFuture.runAsync(()->{
for (LawsOverseasStandard lawsOverseasStandard : listLawsOverseasStandard) {
try {
LawsHomeNormalSearchVO obj = new LawsHomeNormalSearchVO();
obj.setId(lawsOverseasStandard.getId());
obj.setTitle(lawsOverseasStandard.getStandardNumber() + " " + lawsOverseasStandard.getStandardName());
obj.setResourceType(resourceType);
obj.setReleaseDate(lawsOverseasStandard.getReleaseDate());
obj.setStandardClass(lawsOverseasStandard.getStandardClass());
obj.setApplicableVehicle(lawsOverseasStandard.getApplicableVehicle());
obj.setStandardState(lawsOverseasStandard.getStandardState());
List<OSSFile> listFile = getOssFilesLawsOverseasStandard(lawsOverseasStandard);
updateES(opType, obj, listFile, lawsOverseasStandard.getId());
}catch (Exception e){
log.error("同步ES数据异常:",e.getMessage());
}
}
},pool);
startIndex = startIndex + batchCount; // 下一批
}
}
@NotNull
private List<OSSFile> getOssFilesLawsOverseasStandard(LawsOverseasStandard lawsOverseasStandard) {
// 从过程稿件从上到下顺序检索;如果文档里有发布稿,只检索发布稿
LambdaQueryWrapper<OSSFile> ossFile = new LambdaQueryWrapper<>();
ossFile.eq(OSSFile::getStandardId,lawsOverseasStandard.getId());
ossFile.orderByAsc(OSSFile::getCreateTime);
List<OSSFile> list = oSSFileService.list(ossFile);
List<OSSFile> listFile = new ArrayList<>();
if(!CollectionUtils.isEmpty(list)){
List<OSSFile> op = list.stream().filter(o-> Objects.equals(o.getStandardFileType(),"publish_of_original")).collect(Collectors.toList());
List<OSSFile> opTr = list.stream().filter(o-> Objects.equals(o.getStandardFileType(),"publish_of_translation")).collect(Collectors.toList());
// 有发布稿
if(!CollectionUtils.isEmpty(op) || !CollectionUtils.isEmpty(opTr)){
if(!CollectionUtils.isEmpty(op)){
listFile.addAll(op);
}
if(!CollectionUtils.isEmpty(opTr)){
listFile.addAll(opTr);
}
}else{
getOp(list, listFile, "modification_list");
getOp(list, listFile, "draft_for_review");
getOp(list, listFile, "draft_for_approval");
getOp(list, listFile, "draft_for_comment");
getOp(list, listFile, "draft");
}
}
return listFile;
}
private void getOp(List<OSSFile> list, List<OSSFile> listFile, String modificationList) {
List<OSSFile> op = list.stream().filter(o -> Objects.equals(o.getStandardFileType(), modificationList)).collect(Collectors.toList());
if (!CollectionUtils.isEmpty(op)) {
listFile.addAll(op.stream().sorted(Comparator.comparing(OSSFile::getCreateTime)).collect(Collectors.toList()));
}
}
private void updateLawsEnterpriseStandardToEs(String resourceType, String ids,String opType) {
// 批量操作
// 从第0个下标开始
int startIndex = 0;
int batchCount = 2000;
List<String> listIds = Arrays.asList(ids.split(","));
ForkJoinPool pool = new ForkJoinPool();
// 异步执行
while (startIndex < listIds.size()) {
int endIndex = getEndIndex(startIndex, batchCount, listIds);
List<String> listIdsSub = listIds.subList(startIndex, endIndex);
List<LawsEnterpriseStandard> listLawsEnterpriseStandard = lawsEnterpriseStandardService.listByIds(listIdsSub);
CompletableFuture.runAsync(()->{
for (LawsEnterpriseStandard lawsEnterpriseStandard : listLawsEnterpriseStandard) {
try {
LawsHomeNormalSearchVO obj = new LawsHomeNormalSearchVO();
obj.setId(lawsEnterpriseStandard.getId());
obj.setTitle(lawsEnterpriseStandard.getStandardNumber() + " " + lawsEnterpriseStandard.getStandardName());
obj.setResourceType(resourceType);
obj.setReleaseDate(lawsEnterpriseStandard.getReleaseDate());
obj.setStandardState(lawsEnterpriseStandard.getStandardState());
obj.setStandardCode(lawsEnterpriseStandard.getStandardCode());
obj.setEnterpriseStandardState(lawsEnterpriseStandard.getStandardState());
obj.setGradeClassification(lawsEnterpriseStandard.getGradeClassification());
List<OSSFile> listFile = getOssFilesLawsEnterpriseStandard(lawsEnterpriseStandard);
updateES(opType, obj, listFile, lawsEnterpriseStandard.getId());
}catch (Exception e){
log.error("同步ES数据异常:",e.getMessage());
}
}
},pool);
startIndex = startIndex + batchCount; // 下一批
}
}
@NotNull
private List<OSSFile> getOssFilesLawsEnterpriseStandard(LawsEnterpriseStandard lawsEnterpriseStandard) {
// 从过程稿件从上到下顺序检索;如果文档里有发布稿,只检索发布稿
LambdaQueryWrapper<OSSFile> ossFile = new LambdaQueryWrapper<>();
ossFile.eq(OSSFile::getStandardId,lawsEnterpriseStandard.getId());
ossFile.orderByAsc(OSSFile::getCreateTime);
List<OSSFile> list = oSSFileService.list(ossFile);
List<OSSFile> listFile = new ArrayList<>();
if(!CollectionUtils.isEmpty(list)){
List<OSSFile> op = list.stream().filter(o-> Objects.equals(o.getStandardFileType(),"publish_of_original")).collect(Collectors.toList());
// 有发布稿
if(!CollectionUtils.isEmpty(op)){
listFile.addAll(op);
}else{
getOp(list, listFile, "modification_list");
getOp(list, listFile, "draft_for_review");
getOp(list, listFile, "draft_for_comment");
getOp(list, listFile, "draft");
}
}
return listFile;
}
private void updateLawsNewsFeedToEs(String resourceType, String ids,String opType) {
// 批量操作
// 从第0个下标开始
int startIndex = 0;
int batchCount = 2000;
List<String> listIds = Arrays.asList(ids.split(","));
ForkJoinPool pool = new ForkJoinPool();
// 异步执行
while (startIndex < listIds.size()) {
int endIndex = getEndIndex(startIndex, batchCount, listIds);
List<String> listIdsSub = listIds.subList(startIndex, endIndex);
List<LawsNewsFeed> listLawsNewsFeed = lawsNewsFeedService.listByIds(listIdsSub);
if(CollectionUtils.isEmpty(listLawsNewsFeed)){
continue;
}
CompletableFuture.runAsync(()->{
for (LawsNewsFeed lawsNewsFeed : listLawsNewsFeed) {
try {
LawsHomeNormalSearchVO obj = new LawsHomeNormalSearchVO();
obj.setId(lawsNewsFeed.getId());
obj.setTitle(lawsNewsFeed.getDynamicHeading());
obj.setResourceType(resourceType);
updateESLawsNewsFeed(opType, lawsNewsFeed, obj);
}catch (Exception e){
log.error("同步ES数据异常:",e.getMessage());
}
}
},pool);
startIndex = startIndex + batchCount; // 下一批
}
}
private void updateESLawsNewsFeed(String opType, LawsNewsFeed lawsNewsFeed, LawsHomeNormalSearchVO obj) {
if(Objects.equals(opType, LawsHomeSearchCommon.LAWS_ES_ADD) ||
Objects.equals(opType,LawsHomeSearchCommon.LAWS_ES_UPDATE)){
addES(obj, lawsNewsFeed.getId(), new StringBuilder(lawsNewsFeed.getDynamicDetails()));
}else if(Objects.equals(opType,LawsHomeSearchCommon.LAWS_ES_DELETE)){
jeroElasticsearchTemplate.delete(HomeSearchEnum.INDEX_NAME_DOCUMENT_ZQ.getValue(), HomeSearchEnum.TYPE_NAME_DOCUMENT_ZQ.getValue(), lawsNewsFeed.getId());
}
}
public void updateElasticsearch(LawsHomeNormalSearchVO obj,List<OSSFile> listFile){
String idTemp = obj.getId();
StringBuilder fileTextAll = getStringBuilder(listFile);
addES(obj, idTemp, fileTextAll);
}
@NotNull
private StringBuilder getStringBuilder(List<OSSFile> listFile) {
StringBuilder fileTextAll = new StringBuilder();
try {
for (OSSFile ossFile : listFile) {
if (ossFile.getFileName().endsWith(".doc")
|| ossFile.getFileName().endsWith(".docx")
|| ossFile.getFileName().endsWith(".DOC")
|| ossFile.getFileName().endsWith(".DOCX")) {
try {
String wordContent = ReadWordUtil.readWord(ossFile.getUrl(),uploadpath);
if (StringUtils.isNotBlank(wordContent)) {
fileTextAll.append(wordContent).append(",");
}
} catch (Exception e) {
log.error("文档库: 读取word内容失败");
}
} else if (ossFile.getFileName().endsWith(".pdf") || ossFile.getFileName().endsWith(".PDF")) {
try {
String pdfContent = ReadPdfUtil.readPdf(ossFile.getUrl());
if (StringUtils.isNotBlank(pdfContent)) {
fileTextAll.append(pdfContent).append(",");
}
} catch (Exception e) {
log.error("文档库: 读取PDF文件内容失败");
}
}
}
} catch (Exception e) {
e.printStackTrace();
log.error("文档库新增es错误");
}
return fileTextAll;
}
private void addES(LawsHomeNormalSearchVO obj, String idTemp, StringBuilder fileTextAll) {
Map<String, Object> map = new HashMap<>();
map.put("id", obj.getId());
map.put("title", obj.getTitle());
map.put("content", fileTextAll);
map.put("resource_type", obj.getResourceType());
map.put("standard_class", obj.getStandardClass());
map.put("dynamic_type", obj.getDynamicType());
map.put("applicable_vehicle", obj.getApplicableVehicle());
map.put("standard_property", obj.getStandardProperty());
map.put("standard_state", obj.getStandardState());
map.put("standard_code",obj.getStandardCode());
map.put("enterpriseStandardState",obj.getEnterpriseStandardState());
map.put("grade_classification",obj.getGradeClassification());
map.put("standard_type",obj.getStandardType());
map.put("create_time",new Date());
//添加es
JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(map));
try {
jeroElasticsearchTemplate.saveOrUpdate(HomeSearchEnum.INDEX_NAME_DOCUMENT_ZQ.getValue(), HomeSearchEnum.TYPE_NAME_DOCUMENT_ZQ.getValue(), idTemp, jsonObject);
} catch (Exception e) {
log.error("文档库添加es失败");
}
}
}
@@ -83,4 +83,9 @@ public class LawsHomeNormalSearchVO implements Serializable {
*/
@ApiModelProperty(value = "标准等级分类 (只有企业标准 数据字典 standard_grade_classify (全部传空)")
private String gradeClassification;
/**
* 如果是国内的情况,排序规则按照:GB、 GBT(GB/T) 、 其他标准 的方式排序
*/
private String standardType;
}