文档库--搜索(全部)

This commit is contained in:
zhn
2022-03-18 16:40:38 +08:00
parent 9f98d9704d
commit c9a2691468
8 changed files with 380 additions and 30 deletions
@@ -2,10 +2,10 @@ package com.jero.common.es;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import com.jero.common.util.RestUtil;
import com.jero.common.util.oConvertUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
@@ -13,7 +13,11 @@ import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* 关于 ElasticSearch 的一些方法(创建索引、添加数据、查询等)
@@ -418,11 +422,24 @@ public class JeroElasticsearchTemplate {
* @param size 返回条目数
* @return { "query": query }
*/
public JSONObject buildQuery(List<String> _source, JSONObject query,String sort, int from, int size) {
// public JSONObject buildQuery(List<String> _source, JSONObject query,List<Map<String, Object>> highlight,String sort, int from, int size) {
// JSONObject json = new JSONObject();
// if (_source != null) {
// json.put("_source", _source);
// }
// json.put("highlight", highlight);
// json.put("query", query);
// json.put("from", from);
// json.put("size", size);
//// json.put("sort", sort);
// return json;
// }
public JSONObject buildQuery(List<String> _source, JSONObject query,Map<String, Object> highlight,String sort, int from, int size) {
JSONObject json = new JSONObject();
if (_source != null) {
json.put("_source", _source);
}
json.put("highlight", highlight);
json.put("query", query);
json.put("from", from);
json.put("size", size);
@@ -1,4 +1,4 @@
package com.jero.common.config;
package com.jero.common.esconfig;
import com.jero.common.util.SpringContextHolder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
@@ -7,7 +7,8 @@ package com.jero.modules.document.enums;
*/
public enum SearchEnum {
INDEX_NAME_DOCUMENT("文档库索引名称","documentLibrary"),
TYPE_NAME_DOCUMENT("文档库类型","document");
TYPE_NAME_DOCUMENT("文档库类型","document"),
FULL_TEXT_SEARCH("全部","fulltextsearch");
@@ -238,6 +238,7 @@ 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);
}
} catch (Exception e) {
log.error("文档库删除es失败");
@@ -918,9 +919,11 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl<BussDocumentLi
//数据值
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"));
mapFullText.put("file_text", map.get("文件内容"));
StringBuilder sb = new StringBuilder();
try {
for (Map.Entry<String, Object> entry : map.entrySet()) {
@@ -981,10 +984,13 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl<BussDocumentLi
//添加es
map.put("id", idTemp);
map.put("module_type", "文档库");
String json = JSON.toJSONString(map);
JSONObject jsonObject = JSON.parseObject(json);
JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(map));
JSONObject jsonObjectAll = JSON.parseObject(JSON.toJSONString(mapFullText));
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);
} catch (Exception e) {
log.error("文档库添加es失败");
}
@@ -1207,6 +1213,7 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl<BussDocumentLi
mapFullText.put("module_type", "文档库");
mapFullText.put("serial_number", map.get("serial_number"));
mapFullText.put("title", map.get("title"));
mapFullText.put("file_text", "文件内容-->废止");
for (Map.Entry<String, Object> entry : map.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
@@ -1300,13 +1307,13 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl<BussDocumentLi
bussDocumentLibraryEOMapper.deleteById((String) map.get("id"));
bussDocumentLibraryEOMapper.insertInfo(insertField, insertValue);
map.put("module_type", "文档库");
String json = JSON.toJSONString(map);//map转String
JSONObject jsonObject = JSON.parseObject(json);
JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(map));
JSONObject jsonObjectAll = JSON.parseObject(JSON.toJSONString(mapFullText));
//修改es
try {
jeroElasticsearchTemplate.saveOrUpdate(SearchEnum.INDEX_NAME_DOCUMENT.getValue(), SearchEnum.TYPE_NAME_DOCUMENT.getValue(), (String) map.get("id"), jsonObject);
//添加全文
jeroElasticsearchTemplate.saveOrUpdate(SearchEnum.FULL_TEXT_SEARCH.getValue(), SearchEnum.FULL_TEXT_SEARCH.getValue(), (String) map.get("id"), jsonObjectAll);
} catch (Exception e) {
log.error("文档库编辑es失败");
}
@@ -4,11 +4,13 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jero.common.api.vo.Result;
import com.jero.common.aspect.annotation.AutoLog;
import com.jero.modules.searchcenter.service.impl.IDocumentSearchService;
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.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@@ -68,13 +70,22 @@ public class DocumentSearchController {
return Result.OK(infoList);
}
@ApiOperation(value="删除", notes="删除")
@ApiOperation(value="全部查询", notes="全部查询")
@GetMapping(value = "/getFullTextInfoList")
public Result<?> getFullTextInfoList(@RequestBody SearchVO searchVO) {
IPage pageInfo = iDocumentSearchService.getFullTextInfoList(searchVO);
return Result.OK(pageInfo);
}
@ApiOperation(value="根据id删除", notes="根据id删除")
@GetMapping(value = "/deleteById")
public Result<?> deleteById(@RequestParam String ids) {
iDocumentSearchService.deleteById(ids);
return Result.OK();
}
@ApiOperation(value="删除", notes="删除")
@ApiOperation(value="全部删除", notes="全部删除")
@GetMapping(value = "/deleteAll")
public Result<?> deleteAll() {
iDocumentSearchService.deleteAll();
@@ -15,6 +15,7 @@ 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.searchcenter.vo.SearchVO;
import com.jero.modules.system.entity.SysCategory;
import com.jero.modules.system.entity.SysDictItem;
import com.jero.modules.system.service.impl.SysCategoryServiceImpl;
@@ -66,6 +67,8 @@ public class DocumentSearchServiceImpl implements IDocumentSearchService {
return header;
}
@Override
public IPage getInfoList(Map<String, Object> map) {
//文档库所有的字段
@@ -153,21 +156,15 @@ public class DocumentSearchServiceImpl implements IDocumentSearchService {
must.add(queryMap);
}
}
// String indexName = "documentLibrary";
// String typeName = "document";
//列表
//封装查询条件
JSONObject jsonObject = jeroElasticsearchTemplate.buildBoolQuery(must, null, null);
//封装分页和字段值
JSONObject queryObject = jeroElasticsearchTemplate.buildQuery(sourceList, jsonObject, null, Integer.parseInt(map.get("pageNo").toString()), Integer.parseInt(map.get("pageSize").toString()));
//
//数据查询
//1. 封装分页和字段值
JSONObject queryObject = jeroElasticsearchTemplate.buildQuery(sourceList, jsonObject, null, null, Integer.parseInt(map.get("pageNo").toString()), Integer.parseInt(map.get("pageSize").toString()));
//2. 数据查询
JSONObject search = jeroElasticsearchTemplate.search(SearchEnum.INDEX_NAME_DOCUMENT.getValue(), SearchEnum.TYPE_NAME_DOCUMENT.getValue(), queryObject);
//处理数据字典
List<SysDictItem> sysDictItems = sysDictItemServiceImpl.selectItemsAll();//下拉数据字典
List<SysCategory> categoryList = sysCategoryService.list();//树形数据字典
@@ -189,7 +186,7 @@ public class DocumentSearchServiceImpl implements IDocumentSearchService {
}
//处理文件
if(FieldFileList.contains(entry.getKey())){
if (FieldFileList.contains(entry.getKey())) {
List<OSSFile> fileInfos = iOSSFileService.getFileInfos((String) entry.getValue());
if (fileInfos.size() > 0) {
for (OSSFile fileInfo : fileInfos) {
@@ -199,7 +196,7 @@ public class DocumentSearchServiceImpl implements IDocumentSearchService {
}
});
mapTemp.put("file_name",fileList);
mapTemp.put("file_name", fileList);
//下拉单选和下拉多选数据字典处理
// mapTemp.entrySet().forEach(entry -> {
@@ -215,21 +212,234 @@ public class DocumentSearchServiceImpl implements IDocumentSearchService {
return page;
}
@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","<span class='highlight-class'>");
// map3.put("post_tags","</span>");
// 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","<span class='highlight-class'>");
// map3.put("post_tags","</span>");
// map1.put("content", map3);
// highlightMap.put("fields", map1);
//
// //处理首次搜索和在结果中搜索
// Map<String, Object> map2 = new HashMap<>();
// map2.put("content",selectValue);
// queryMap.put("match", map2);
// }
String selectValue = searchVO.getSelectValue();
String selectValueTwo = searchVO.getSelectValueTwo();
//测试数据
// selectValue = "看看";
// selectValueTwo = "喔喔";
//不查询高亮字段
//1. 需要查询的字段
List<String> fieldList = new ArrayList<>();
fieldList.add("module_type");
fieldList.add("title");
fieldList.add("serial_number");
fieldList.add("file_text");
fieldList.add("content");
JSONArray queryMapJson = new JSONArray();
for (String field : fieldList) {
Map<String, Object> queryMap = new HashMap<>();
Map<String, Object> map2 = new HashMap<>();
map2.put(field, selectValue);
queryMap.put("match", map2);
queryMapJson.add(queryMap);
}
//基础添加封装
JSONObject jsonObject = jeroElasticsearchTemplate.buildBoolQuery(null, null, queryMapJson);
//全部
//1. 条件,分页
JSONObject queryObject = jeroElasticsearchTemplate.buildQuery(null,
jsonObject,
null,
null,
searchVO.getPage(),
searchVO.getPageSize());
// JSONObject queryObject11 = jeroElasticsearchTemplate.buildQuery(null, jsonObject11,highlightMap,null, Integer.parseInt(map.get("pageNo").toString()), Integer.parseInt(map.get("pageSize").toString()));
//2. 数据查询
JSONObject search = jeroElasticsearchTemplate.search("fulltextsearch", "fulltextsearch", queryObject);
//全部的数据(调试接口用--最后删除即可)
JSONObject searchAll = jeroElasticsearchTemplate.search("fulltextsearch", "fulltextsearch", new JSONObject());
List<Map<String, Object>> list = (List<Map<String, Object>>) (((Map) search.get("hits")).get("hits"));
//首次搜索结果
List<Map<String, Object>> mapList = new ArrayList<>();
//二次搜索结果
List<Map<String, Object>> mapListTwo = new ArrayList<>();
//如果勾选了列表数据,则先通过ids过滤一遍数据
if (list.size() != 0 && StringUtils.isNotBlank(searchVO.getIds())) {
list = list.stream().filter(e -> searchVO.getIds().contains((String) e.get("id"))).collect(Collectors.toList());
}
for (Map<String, Object> stringObjectMap : list) {
Map<String, Object> mapSource = (Map<String, Object>) stringObjectMap.get("_source");
if (StringUtils.isNotBlank(selectValueTwo)) {
//二次查询
int count = 0;
for (Map.Entry<String, Object> entry : mapSource.entrySet()) {
String key = entry.getKey();
String value = (String) entry.getValue();
//判断value中是否包含二次搜索中的字符
boolean flag = judgeContains(value, selectValueTwo);
if (flag) {
count++;
StringBuilder sb = new StringBuilder();
if (StringUtils.isNotBlank(value)) {
for (int i = 0; i < value.length(); i++) {
String chStr = String.valueOf(value.charAt(i));
if (selectValueTwo.contains(String.valueOf(chStr))) {
chStr = "<span class='highlight-class'>" + chStr + "</span>";
}
sb.append(String.valueOf(chStr));
}
}
entry.setValue(sb.toString());
}
}
if (count > 0) {
mapListTwo.add(mapSource);
}
} 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
for (Map.Entry<String, Object> entry : mapSource.entrySet()) {
String key = entry.getKey();
String value = (String) entry.getValue();
StringBuilder sb = new StringBuilder();
if (StringUtils.isNotBlank(value)) {
for (int i = 0; i < value.length(); i++) {
String chStr = String.valueOf(value.charAt(i));
if (selectValue.contains(String.valueOf(chStr))) {
chStr = "<span class='highlight-class'>" + chStr + "</span>";
int c = 0;
}
sb.append(String.valueOf(chStr));
}
}
entry.setValue(sb.toString());
}
}
}
//处理分页
IPage page = new Page(searchVO.getPage(), searchVO.getPageSize());
if (StringUtils.isNotBlank(selectValueTwo)) {
//二次搜索的结果
page.setTotal(Long.parseLong(String.valueOf(mapListTwo.size())));
page.setRecords(mapListTwo);
} else {
//首次搜索的结果
page.setTotal(Long.parseLong(String.valueOf(mapList.size())));
page.setRecords(mapList);
}
return page;
}
private boolean judgeContains(String oldStr, String newStr) {
if (StringUtils.isBlank(oldStr) || StringUtils.isBlank(newStr)) {
return false;
}
int count = 0;
for (int i = 0; i < newStr.length(); i++) {
String chStr = String.valueOf(newStr.charAt(i));
if (oldStr.contains(chStr)) {
count++;
}
}
if (count > 0) {
return true;
} else {
return false;
}
}
private void solveTime(Map<String, Object> mapTemp) {
String implementTime = (String) mapTemp.get("implement_time");
String issueTime = (String) mapTemp.get("issue_time");
if(StringUtils.isNotBlank(implementTime)){
mapTemp.put("implement_time",implementTime.split(" ")[0]);
if (StringUtils.isNotBlank(implementTime)) {
mapTemp.put("implement_time", implementTime.split(" ")[0]);
}
if(StringUtils.isNotBlank(issueTime)){
mapTemp.put("issue_time",issueTime.split(" ")[0]);
if (StringUtils.isNotBlank(issueTime)) {
mapTemp.put("issue_time", issueTime.split(" ")[0]);
}
}
@Override
public void deleteById(String ids) {
for (String id : ids.split(",")) {
jeroElasticsearchTemplate.delete(SearchEnum.INDEX_NAME_DOCUMENT.getValue(),SearchEnum.TYPE_NAME_DOCUMENT.getValue(),id);
jeroElasticsearchTemplate.delete(SearchEnum.INDEX_NAME_DOCUMENT.getValue(), SearchEnum.TYPE_NAME_DOCUMENT.getValue(), id);
}
}
@@ -1,6 +1,7 @@
package com.jero.modules.searchcenter.service.impl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jero.modules.searchcenter.vo.SearchVO;
import java.util.List;
import java.util.Map;
@@ -18,7 +19,10 @@ public interface IDocumentSearchService {
IPage getInfoList(Map<String, Object> map);
IPage getFullTextInfoList(SearchVO searchVO);
void deleteById(String ids);
void deleteAll();
}
@@ -0,0 +1,100 @@
package com.jero.modules.searchcenter.vo;
import com.jero.modules.split.common.BasePage;
public class SearchVO extends BasePage {
//记录首页搜索框中对应的数据
private String selectValue;
//在结果中搜索
private String selectValueTwo;
//在结果中搜索(ids)
private String ids;
//记录从哪个索引表中查数据
private String selectIndex;
private String keywords;
private String resultKeyword;
// 条目搜索条件
private String itemsName;
private String itemsNum;
private String parts;
public String getIds() {
return ids;
}
public void setIds(String ids) {
this.ids = ids;
}
public String getSelectValueTwo() {
return selectValueTwo;
}
public void setSelectValueTwo(String selectValueTwo) {
this.selectValueTwo = selectValueTwo;
}
public String getSelectValue() {
return selectValue;
}
public void setSelectValue(String selectValue) {
this.selectValue = selectValue;
}
public String getSelectIndex() {
return selectIndex;
}
public void setSelectIndex(String selectIndex) {
this.selectIndex = selectIndex;
}
public String getKeywords() {
return keywords;
}
public void setKeywords(String keywords) {
this.keywords = keywords;
}
public String getResultKeyword() {
return resultKeyword;
}
public void setResultKeyword(String resultKeyword) {
this.resultKeyword = resultKeyword;
}
public String getItemsName() {
return itemsName;
}
public void setItemsName(String itemsName) {
this.itemsName = itemsName;
}
public String getItemsNum() {
return itemsNum;
}
public void setItemsNum(String itemsNum) {
this.itemsNum = itemsNum;
}
public String getParts() {
return parts;
}
public void setParts(String parts) {
this.parts = parts;
}
}