搜索中心-问题知识库检索加权限。与问题知识库权限一样。

This commit is contained in:
wangzhijiang
2022-08-23 18:15:47 +08:00
parent 1a0c6e1ad6
commit b71ca5aad4
@@ -4,12 +4,14 @@ package com.jero.modules.searchcenter.service.impl;
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.constant.enums.CutEnum;
import com.jero.common.constant.enums.ModuleEnum;
import com.jero.common.constant.enums.YesOrNoEnum;
import com.jero.common.es.JeroElasticsearchTemplate;
import com.jero.common.system.vo.LoginUser;
import com.jero.generater.modules.online.cgform.entity.OnlCgformField;
import com.jero.generater.modules.online.cgform.service.impl.OnlCgformFieldServiceImpl;
import com.jero.modules.collection.entity.OnlCgformCollection;
@@ -20,6 +22,9 @@ import com.jero.modules.document.service.IBussDocumentLibraryEOService;
import com.jero.modules.document.service.impl.BussDocumentLibraryEOServiceImpl;
import com.jero.modules.oss.entity.OSSFile;
import com.jero.modules.oss.service.IOSSFileService;
import com.jero.modules.problemKnowledgeBase.entity.ProblemKnowledgeBaseUserEO;
import com.jero.modules.problemKnowledgeBase.enums.ShowPermissionsEnum;
import com.jero.modules.problemKnowledgeBase.service.IProblemKnowledgeBaseUserEOService;
import com.jero.modules.searchcenter.vo.SearchVO;
import com.jero.modules.subscribe.entity.OnlCgformSubscribe;
import com.jero.modules.subscribe.service.IOnlCgformSubscribeService;
@@ -30,6 +35,7 @@ import com.jero.modules.system.service.impl.SysDictItemServiceImpl;
import org.apache.commons.collections.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.Autowired;
import org.springframework.stereotype.Component;
@@ -65,6 +71,8 @@ public class DocumentSearchServiceImpl implements IDocumentSearchService {
private IOnlCgformCollectionService iOnlCgformCollectionService;
@Autowired
private IOnlCgformSubscribeService iOnlCgformSubscribeService;
@Autowired
private IProblemKnowledgeBaseUserEOService problemKnowledgeBaseUserEOService;
public static final String SEARCH_FLAG = "";//标识(es数据带此标识的代表全文和段落的数据,不带此标识的代表列表数据)
@@ -1093,6 +1101,8 @@ public class DocumentSearchServiceImpl implements IDocumentSearchService {
}
JSONArray queryJsonMustNot = new JSONArray();
JSONArray queryShould = new JSONArray();
String selectValue = (String) map.get("selectValue");
String selectValueTwo = (String) map.get("selectValueTwo");
String problem_type = (String) map.get("problem_type");
@@ -1101,19 +1111,26 @@ public class DocumentSearchServiceImpl implements IDocumentSearchService {
List<String> fieldList = new ArrayList<>();
fieldList.add("title");//权重5
fieldList.add("content");//权重4
// fieldList.add("problem_type");//权重3
// fieldList.add("target_market"); //权重2
fieldList.add("module_type");
fieldList.add("id");
fieldList.add("flag");
//fieldList.add("module_type");
//fieldList.add("id");
//fieldList.add("flag");
JSONArray queryMapJson = new JSONArray();
JSONArray queryMapInputJson = new JSONArray();
JSONArray queryMapJsonTwo = new JSONArray();
JSONArray queryMapJsonThree = new JSONArray();
JSONArray queryMapJsonProblemType = new JSONArray();
JSONArray queryMapJsonTargetMarket = new JSONArray();
JSONArray queryMapJsonAll = new JSONArray();
//基础查询权限,展示权限为公开的,所有人都能查看,展示权限为私密的,只有配置了权限的用户可以查看。
if("base".equals("base")){
JSONArray queryMapJsonBase = new JSONArray();
this.createQueryWhere(ShowPermissionsEnum.OPEN.getValue(),"show_permissions",queryMapJsonBase);
JSONObject jsonObject = jeroElasticsearchTemplate.buildBoolQuery(null, null, queryMapJsonBase);
queryMapJsonAll.add(jsonObject);
}
Map<String,Object> mapHighlight = new HashMap<>();
Map<String,Object> mapHighlight1 = new HashMap<>();
//封装首次的条件
@@ -1128,14 +1145,19 @@ public class DocumentSearchServiceImpl implements IDocumentSearchService {
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 (field.equals("title")){
// map2放最内层
map2.put(field, "*" + selectValue + "*");
// map4放query
map4.put("wildcard",map2);
} else {
map3.put("query",selectValue);
map2.put(field, map3);
map4.put("match",map2);
}
queryMapInputJson.add(map4);
//高亮
if(!"flag".equals(field)){
@@ -1147,41 +1169,23 @@ public class DocumentSearchServiceImpl implements IDocumentSearchService {
//封装问题类型选择框查询条件。
if (StringUtils.isNotBlank(problem_type)) {
Map<String, Object> map2 = new HashMap<>();
Map<String, Object> map3 = new HashMap<>();
Map<String, Object> map4 = new HashMap<>();
map3.put("query",problem_type);
map2.put("problem_type", map3);
map4.put("match",map2);
queryMapJson.add(map4);
//高亮
highlight(mapHighlight, problem_type);
mapHighlight1.put("fields",mapHighlight);
this.createQueryWhere(problem_type,"problem_type",queryMapJson);
}
//封装市场选择框查询条件。
if (StringUtils.isNotBlank(target_market)) {
Map<String, Object> map2 = new HashMap<>();
Map<String, Object> map3 = new HashMap<>();
Map<String, Object> map4 = new HashMap<>();
map3.put("query",target_market);
map2.put("target_market", map3);
map4.put("match",map2);
queryMapJson.add(map4);
//高亮
highlight(mapHighlight, target_market);
mapHighlight1.put("fields",mapHighlight);
this.createQueryWhere(target_market,"target_market",queryMapJson);
}
if(CollectionUtils.isNotEmpty(queryMapJson)){
JSONObject jsonObject = jeroElasticsearchTemplate.buildBoolQuery(null, null, queryMapJson);
JSONObject jsonObject = jeroElasticsearchTemplate.buildBoolQuery(queryMapJson, null, null);
queryMapJsonAll.add(jsonObject);
}
if(CollectionUtils.isNotEmpty(queryMapInputJson)){
JSONObject jsonObject = jeroElasticsearchTemplate.buildBoolQuery(null, null, queryMapInputJson);
queryMapJsonAll.add(jsonObject);
}
//封装二次的条件
if (StringUtils.isNotBlank(selectValueTwo)) {
for (String field : fieldList) {
@@ -1194,13 +1198,10 @@ public class DocumentSearchServiceImpl implements IDocumentSearchService {
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);
@@ -1241,20 +1242,20 @@ public class DocumentSearchServiceImpl implements IDocumentSearchService {
Integer pageNo = Integer.parseInt(map.get("pageNo").toString());
Integer pageSize = Integer.parseInt(map.get("pageSize").toString());
IPage page = this.getProblemKnowledgeBasePage(selectValue, selectValueTwo, queryMapJsonAll, mapHighlight1,null,
pageNo, pageSize, null,(String) map.get("cut"),sort,queryJsonMustNot);
pageNo, pageSize, null,(String) map.get("cut"),sort,queryJsonMustNot,queryShould);
return page;
}
@NotNull
private IPage getProblemKnowledgeBasePage(String selectValue, String selectValueTwo, JSONArray queryMapJson,Map<String,Object> highlightMap,
JSONArray should, Integer pageNo, Integer pageSize, String paragraphFlag,
String cut,JSONObject querySort,JSONArray queryMustNot) {
String cut,JSONObject querySort,JSONArray queryMustNot,JSONArray queryShould) {
JSONObject jsonObject = new JSONObject();
//基础添加封装
if ("paragraphFlag".equals(paragraphFlag)) {
jsonObject = jeroElasticsearchTemplate.buildBoolQuery(queryMapJson, queryMustNot, null);
jsonObject = jeroElasticsearchTemplate.buildBoolQuery(queryMapJson, queryMustNot, queryShould);
} else {
jsonObject = jeroElasticsearchTemplate.buildBoolQuery(queryMapJson, queryMustNot, null);
jsonObject = jeroElasticsearchTemplate.buildBoolQuery(queryMapJson, queryMustNot, queryShould);
}
JSONArray jsonArraySort = new JSONArray();
jsonArraySort.add(querySort);
@@ -1295,10 +1296,10 @@ public class DocumentSearchServiceImpl implements IDocumentSearchService {
mapSource.put(key,fieldConyent);
}
//如果这一条数据中有高亮字段值,但是file_text中没有高亮值, 则赋控制,否则列表中会展示所有的文件内容, 赋空后,则展示基础数据
String fileText = (String) mapSource.get("content");
/*String fileText = (String) mapSource.get("content");
if(StringUtils.isNotBlank(fileText) && !fileText.contains("<text class='highlight-class'>")){
mapSource.put("content","");
}
}*/
}
mapList.add(mapSource);
}
@@ -1317,6 +1318,58 @@ public class DocumentSearchServiceImpl implements IDocumentSearchService {
return page;
}
/**
* 创建查询条件
* @param queryValue 查询的值
* @param fieldKey 查询的字段
* @param jsonArray
*/
public void createQueryWhere (String queryValue,String fieldKey,JSONArray jsonArray){
/**
* fieldKeyMap:字段keyMap,用于存 需要查询的字段 以及字段中的条件 "queryMap"
* queryMap:存放查询条件map
* matchMap:匹配Map
*/
Map<String, Object> fieldKeyMap = new HashMap<>();
Map<String, Object> queryMap = new HashMap<>();
Map<String, Object> matchMap = new HashMap<>();
queryMap.put("query", queryValue);
fieldKeyMap.put(fieldKey, queryMap);
matchMap.put("match",fieldKeyMap);
// 如果是私密,并且查询字段key是展示权限 的话,查询当前用户拥有权限的问题知识库数据
if(StringUtils.equals(fieldKey,"show_permissions")){
//查询出当前登录人所拥有的问题知识库私密数据权限。
LoginUser currentOperatorUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
QueryWrapper<ProblemKnowledgeBaseUserEO> userPermissionQueryWrapper = new QueryWrapper<>();
userPermissionQueryWrapper.lambda().eq(ProblemKnowledgeBaseUserEO::getUserId,currentOperatorUser.getId());
List<ProblemKnowledgeBaseUserEO> problemKnowledgeBaseUserEOList = this.problemKnowledgeBaseUserEOService.list(userPermissionQueryWrapper);
if(CollectionUtils.isNotEmpty(problemKnowledgeBaseUserEOList)){
List<String> problemKnowledgeBaseIdList = problemKnowledgeBaseUserEOList.stream().map(ProblemKnowledgeBaseUserEO::getProblemKnowledgeBaseId).distinct().collect(Collectors.toList());
//in查询该用户拥有权限的问题知识库id。
Map<String, Object> fieldKeyMap2 = new HashMap<>();
Map<String, Object> matchMap2 = new HashMap<>();
fieldKeyMap2.put("id", problemKnowledgeBaseIdList);
matchMap2.put("terms",fieldKeyMap2);
jsonArray.add(matchMap2);
}else {
Map<String, Object> fieldKeyMap2 = new HashMap<>();
Map<String, Object> queryMap2 = new HashMap<>();
Map<String, Object> matchMap2 = new HashMap<>();
queryMap2.put("query", "null");
fieldKeyMap2.put("id", queryMap2);
matchMap2.put("match",fieldKeyMap2);
jsonArray.add(matchMap2);
}
}
jsonArray.add(matchMap);
}
}