diff --git a/jero-boot/jero-boot-base/jero-boot-base-core/src/main/java/com/jero/common/es/JeroElasticsearchTemplate.java b/jero-boot/jero-boot-base/jero-boot-base-core/src/main/java/com/jero/common/es/JeroElasticsearchTemplate.java index f7ad875d0..46ed348c7 100644 --- a/jero-boot/jero-boot-base/jero-boot-base-core/src/main/java/com/jero/common/es/JeroElasticsearchTemplate.java +++ b/jero-boot/jero-boot-base/jero-boot-base-core/src/main/java/com/jero/common/es/JeroElasticsearchTemplate.java @@ -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 _source, JSONObject query,String sort, int from, int size) { +// public JSONObject buildQuery(List _source, JSONObject query,List> 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 _source, JSONObject query,Map 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); diff --git a/jero-boot/jero-boot-base/jero-boot-base-tools/src/main/java/com/jero/common/config/CommonConfig.java b/jero-boot/jero-boot-base/jero-boot-base-tools/src/main/java/com/jero/common/esconfig/CommonConfig.java similarity index 94% rename from jero-boot/jero-boot-base/jero-boot-base-tools/src/main/java/com/jero/common/config/CommonConfig.java rename to jero-boot/jero-boot-base/jero-boot-base-tools/src/main/java/com/jero/common/esconfig/CommonConfig.java index 344cf6c6a..e67a030cd 100644 --- a/jero-boot/jero-boot-base/jero-boot-base-tools/src/main/java/com/jero/common/config/CommonConfig.java +++ b/jero-boot/jero-boot-base/jero-boot-base-tools/src/main/java/com/jero/common/esconfig/CommonConfig.java @@ -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; diff --git a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/document/enums/SearchEnum.java b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/document/enums/SearchEnum.java index 98fd77083..6fc880e4c 100644 --- a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/document/enums/SearchEnum.java +++ b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/document/enums/SearchEnum.java @@ -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"); diff --git a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/document/service/impl/BussDocumentLibraryEOServiceImpl.java b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/document/service/impl/BussDocumentLibraryEOServiceImpl.java index 9badc7a2b..3de8336ee 100644 --- a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/document/service/impl/BussDocumentLibraryEOServiceImpl.java +++ b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/document/service/impl/BussDocumentLibraryEOServiceImpl.java @@ -238,6 +238,7 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl 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 entry : map.entrySet()) { @@ -981,10 +984,13 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl废止"); for (Map.Entry entry : map.entrySet()) { String key = entry.getKey(); Object value = entry.getValue(); @@ -1300,13 +1307,13 @@ public class BussDocumentLibraryEOServiceImpl extends ServiceImpl 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(); diff --git a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/searchcenter/service/impl/DocumentSearchServiceImpl.java b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/searchcenter/service/impl/DocumentSearchServiceImpl.java index e9e38a305..4b02f8fcd 100644 --- a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/searchcenter/service/impl/DocumentSearchServiceImpl.java +++ b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/searchcenter/service/impl/DocumentSearchServiceImpl.java @@ -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 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 sysDictItems = sysDictItemServiceImpl.selectItemsAll();//下拉数据字典 List categoryList = sysCategoryService.list();//树形数据字典 @@ -189,7 +186,7 @@ public class DocumentSearchServiceImpl implements IDocumentSearchService { } //处理文件 - if(FieldFileList.contains(entry.getKey())){ + if (FieldFileList.contains(entry.getKey())) { List 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 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 highlightMap = new HashMap<>(); +// List> highlightList = new ArrayList<>(); +//// Map queryMap = new HashMap<>(); +// JSONArray queryMapJson = new JSONArray(); +// if("fullTextSearch".equals(indexName) && StringUtils.isNotBlank(selectValue)){ +// //封装高亮查询条件 +// Map map1 = new HashMap<>(); +//// Map map2 = new HashMap<>(); +//// map2.put("content",selectValue); +// Map map3 = new HashMap<>(); +// map3.put("pre_tags",""); +// map3.put("post_tags",""); +// for (String s : listTemp) { +// Map map4 = new HashMap<>(); +// Map queryMap = new HashMap<>(); +// Map 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 highlightMap = new HashMap<>(); +// Map queryMap = new HashMap<>(); +// JSONArray queryMapJson = new JSONArray(); +// if("fullTextSearch".equals(indexName) && StringUtils.isNotBlank(selectValue)){ +// //封装高亮查询条件 +// Map map1 = new HashMap<>(); +// Map map3 = new HashMap<>(); +// map3.put("pre_tags",""); +// map3.put("post_tags",""); +// map1.put("content", map3); +// highlightMap.put("fields", map1); +// +// //处理首次搜索和在结果中搜索 +// Map map2 = new HashMap<>(); +// map2.put("content",selectValue); +// queryMap.put("match", map2); +// } + String selectValue = searchVO.getSelectValue(); + String selectValueTwo = searchVO.getSelectValueTwo(); + //测试数据 +// selectValue = "看看"; +// selectValueTwo = "喔喔"; + //不查询高亮字段 + //1. 需要查询的字段 + List 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 queryMap = new HashMap<>(); + Map 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> list = (List>) (((Map) search.get("hits")).get("hits")); + + //首次搜索结果 + List> mapList = new ArrayList<>(); + //二次搜索结果 + List> 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 stringObjectMap : list) { + Map mapSource = (Map) stringObjectMap.get("_source"); + if (StringUtils.isNotBlank(selectValueTwo)) { + //二次查询 + int count = 0; + for (Map.Entry 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 = "" + chStr + ""; + } + sb.append(String.valueOf(chStr)); + } + } + entry.setValue(sb.toString()); + } + } + if (count > 0) { + mapListTwo.add(mapSource); + } + } else { + //首次查询 + mapList.add(mapSource); + + //目前只能处理单字段高亮(多字段高亮问题解决后可以用此方法) + //----------------------------------------------------------------- +// Map mapHighlight = (Map) stringObjectMap.get("highlight"); + //content高亮处理() +// String content = ""; +// for (Map.Entry entry : mapHighlight.entrySet()) { +// List valueList = (ArrayList)entry.getValue(); +// for (String s : valueList) { +// content += s; +// } +// } + //----------------------------------------------------------------------------- + //处理高亮module_type,title,serial_number,file_text + for (Map.Entry 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 = "" + chStr + ""; + 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 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); } } diff --git a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/searchcenter/service/impl/IDocumentSearchService.java b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/searchcenter/service/impl/IDocumentSearchService.java index 0b60fb041..a89cc70e7 100644 --- a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/searchcenter/service/impl/IDocumentSearchService.java +++ b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/searchcenter/service/impl/IDocumentSearchService.java @@ -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 map); + IPage getFullTextInfoList(SearchVO searchVO); + void deleteById(String ids); + void deleteAll(); } diff --git a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/searchcenter/vo/SearchVO.java b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/searchcenter/vo/SearchVO.java new file mode 100644 index 000000000..892895a13 --- /dev/null +++ b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/searchcenter/vo/SearchVO.java @@ -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; + } +}