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 80ef1336b..42aecdf7d 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 @@ -486,45 +486,30 @@ public class DocumentSearchServiceImpl implements IDocumentSearchService { //二次查询 int count = 0; for (Map.Entry entry : mapSource.entrySet()) { + String valueHighlight = ""; String value = (String) entry.getValue(); //判断value中是否包含二次搜索中的字符 - boolean flag = judgeContains(value, selectValueTwo); - StringBuilder sb = new StringBuilder(); - if (flag) { - //截取第一处高亮的前后100个字符 - value = getFileText(value, selectValueTwo); - count++; - for (int i = 0; i < value.length(); i++) { - String chStr = String.valueOf(value.charAt(i)); - //第一次的搜索条件也需要高亮 - String condition = selectValueTwo + selectValue; - if (condition.contains(String.valueOf(chStr))) { - chStr = "" + chStr + ""; + if(StringUtils.isNotBlank(value)){ + if(value.contains(selectValueTwo)){ + value = subFileText(value, selectValueTwo); + count++; + String replacementTwo = "" + selectValueTwo + ""; + valueHighlight = value.replaceAll(selectValueTwo, replacementTwo); + //第一次搜索的也高亮 + if(value.contains(selectValue) && !selectValueTwo.contains(selectValue)){ + String replacementOne = "" + selectValue + ""; + valueHighlight = valueHighlight.replaceAll(selectValue,replacementOne); } - sb.append(String.valueOf(chStr)); - } - entry.setValue(sb.toString()); - } else { - //第二次搜索的时候file_text中不包含搜索条件的时候将file_text赋值空(此时展示拼接的内容) - if ("file_text".equals(entry.getKey())) { - entry.setValue(""); - value = ""; - } - count++; - //如果内容中不包含第二次搜索条件,则继续高亮第一处 - //判断value中是否包含首次搜索中的字符(因为其他的字段有可能包含二次的搜索条件查出来的这条数据,所以还得判断value中是否包含首次搜索中的字符) - boolean flagOne = judgeContains(value, selectValue); - if (flagOne) { - //如果包含第一次的搜索条件,先前后截取100字符后再高亮 - value = getFileText(value, selectValue); - for (int i = 0; i < value.length(); i++) { - String chStr = String.valueOf(value.charAt(i)); - if (selectValue.contains(String.valueOf(chStr))) { - chStr = "" + chStr + ""; - } - sb.append(String.valueOf(chStr)); + if (ObjectUtils.isNotEmpty(valueHighlight)) { + entry.setValue(valueHighlight); } - entry.setValue(sb.toString()); + }else{ + //第二次搜索的时候file_text中不包含搜索条件的时候将file_text赋值空(此时展示拼接的内容) +// if ("file_text".equals(entry.getKey())) { +// entry.setValue(""); +// value = ""; +// } +// count++; } } } @@ -539,38 +524,31 @@ public class DocumentSearchServiceImpl implements IDocumentSearchService { for (Map.Entry entry : mapSource.entrySet()) { String key = entry.getKey(); String value = String.valueOf(entry.getValue()); - StringBuilder sb = new StringBuilder(); +// StringBuilder sb = new StringBuilder(); + String valueHighlight = ""; if (StringUtils.isNotBlank(value)) { //file_text需要单独处理(截取第一处高亮的前后100个字符) if ("file_text".equals(key)) { //判断value中是否包含搜索中的字符 - boolean flag = judgeContains(value, selectValue); - if (flag) { + if(value.contains(selectValue)){ //截取第一处高亮的前后100个字符 - value = getFileText(value, selectValue); - } else { + value = subFileText(value, selectValue); + }else{ //第一次搜索的时候file_text中不包含搜索条件的时候将file_text赋值空(此时展示拼接的内容) entry.setValue(""); value = ""; } } if ("content".equals(key)) { - boolean flag = judgeContains(value, selectValue); - if (flag) { - //截取第一处高亮的前后100个字符 - value = getFileText(value, selectValue); + if(value.contains(selectValue)){ + value = subFileText(value, selectValue); } } - for (int i = 0; i < value.length(); i++) { - String chStr = String.valueOf(value.charAt(i)); - if (selectValue.contains(String.valueOf(chStr))) { - chStr = "" + chStr + ""; - } - sb.append(String.valueOf(chStr)); - } + String replacement = "" + selectValue + ""; + valueHighlight = value.replaceAll(selectValue, replacement); } - if (ObjectUtils.isNotEmpty(sb)) { - entry.setValue(sb.toString()); + if (ObjectUtils.isNotEmpty(valueHighlight)) { + entry.setValue(valueHighlight); } } } @@ -590,7 +568,177 @@ public class DocumentSearchServiceImpl implements IDocumentSearchService { } return page; } +// @NotNull +// private IPage getiPage(String selectValue, String selectValueTwo, JSONArray queryMapJson, +// JSONArray should, Integer pageNo, Integer pageSize, String paragraphFlag, +// String cut,JSONObject querySort) { +// JSONObject jsonObject = new JSONObject(); +// //基础添加封装 +// if ("paragraphFlag".equals(paragraphFlag)) { +// jsonObject = jeroElasticsearchTemplate.buildBoolQuery(queryMapJson, null, null); +// } else { +// jsonObject = jeroElasticsearchTemplate.buildBoolQuery(queryMapJson, null, null); +// } +// JSONArray jsonArraySort = new JSONArray(); +// jsonArraySort.add(querySort); +// +// //1. 条件,分页 +// JSONObject queryObject = jeroElasticsearchTemplate.buildQuery(null, +// jsonObject, +// null, +// jsonArraySort, +// pageNo-1, +// pageSize); +// //2. 数据查询 +// JSONObject search = new JSONObject(); +// if(CutEnum.CN.getValue().equals(cut)){ +// search = jeroElasticsearchTemplate.search(SearchEnum.FULL_TEXT_SEARCH_CN.getValue(), +// SearchEnum.FULL_TEXT_SEARCH_CN.getValue(), +// queryObject); +// }else{ +// search = jeroElasticsearchTemplate.search(SearchEnum.FULL_TEXT_SEARCH_EN.getValue(), +// SearchEnum.FULL_TEXT_SEARCH_EN.getValue(), +// queryObject); +// } +// +// List> list = (List>) (((Map) search.get("hits")).get("hits")); +// +// //首次搜索结果 +// List> mapList = new ArrayList<>(); +// //二次搜索结果 +// List> mapListTwo = new ArrayList<>(); +// +// for (Map stringObjectMap : list) { +// Map mapSource = (Map) stringObjectMap.get("_source"); +// if (StringUtils.isNotBlank(selectValueTwo)) { +// //二次查询 +// int count = 0; +// for (Map.Entry entry : mapSource.entrySet()) { +// String value = (String) entry.getValue(); +// //判断value中是否包含二次搜索中的字符 +// boolean flag = judgeContains(value, selectValueTwo); +// StringBuilder sb = new StringBuilder(); +// if (flag) { +// //截取第一处高亮的前后100个字符 +// value = getFileText(value, selectValueTwo); +// count++; +// for (int i = 0; i < value.length(); i++) { +// String chStr = String.valueOf(value.charAt(i)); +// //第一次的搜索条件也需要高亮 +// String condition = selectValueTwo + selectValue; +// if (condition.contains(String.valueOf(chStr))) { +// chStr = "" + chStr + ""; +// } +// sb.append(String.valueOf(chStr)); +// } +// entry.setValue(sb.toString()); +// } else { +// //第二次搜索的时候file_text中不包含搜索条件的时候将file_text赋值空(此时展示拼接的内容) +// if ("file_text".equals(entry.getKey())) { +// entry.setValue(""); +// value = ""; +// } +// count++; +// //如果内容中不包含第二次搜索条件,则继续高亮第一处 +// //判断value中是否包含首次搜索中的字符(因为其他的字段有可能包含二次的搜索条件查出来的这条数据,所以还得判断value中是否包含首次搜索中的字符) +// boolean flagOne = judgeContains(value, selectValue); +// if (flagOne) { +// //如果包含第一次的搜索条件,先前后截取100字符后再高亮 +// value = getFileText(value, selectValue); +// for (int i = 0; i < value.length(); i++) { +// String chStr = String.valueOf(value.charAt(i)); +// if (selectValue.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); +// //处理高亮module_type,title,serial_number,file_text(输入查询条件后,处理高亮字段) +// if (StringUtils.isNotBlank(selectValue)) { +// for (Map.Entry entry : mapSource.entrySet()) { +// String key = entry.getKey(); +// String value = String.valueOf(entry.getValue()); +// StringBuilder sb = new StringBuilder(); +// if (StringUtils.isNotBlank(value)) { +// //file_text需要单独处理(截取第一处高亮的前后100个字符) +// if ("file_text".equals(key)) { +// //判断value中是否包含搜索中的字符 +// boolean flag = judgeContains(value, selectValue); +// if (flag) { +// //截取第一处高亮的前后100个字符 +// value = getFileText(value, selectValue); +// } else { +// //第一次搜索的时候file_text中不包含搜索条件的时候将file_text赋值空(此时展示拼接的内容) +// entry.setValue(""); +// value = ""; +// } +// } +// if ("content".equals(key)) { +// boolean flag = judgeContains(value, selectValue); +// if (flag) { +// //截取第一处高亮的前后100个字符 +// value = getFileText(value, selectValue); +// } +// } +// for (int i = 0; i < value.length(); i++) { +// String chStr = String.valueOf(value.charAt(i)); +// if (selectValue.contains(String.valueOf(chStr))) { +// chStr = "" + chStr + ""; +// } +// sb.append(String.valueOf(chStr)); +// } +// } +// if (ObjectUtils.isNotEmpty(sb)) { +// entry.setValue(sb.toString()); +// } +// } +// } +// } +// } +// +// //处理分页 +// IPage page = new Page(pageNo, pageSize); +// if (StringUtils.isNotBlank(selectValueTwo)) { +// //二次搜索的结果 +// page.setTotal(Long.parseLong(String.valueOf(((Map) search.get("hits")).get("total")))); +// page.setRecords(mapListTwo); +// } else { +// //首次搜索的结果 +// page.setTotal(Long.parseLong(String.valueOf(((Map) search.get("hits")).get("total")))); +// page.setRecords(mapList); +// } +// return page; +// } + private String subFileText(String fileTextInfo, String selectValue) { + int i = fileTextInfo.indexOf(selectValue); + String start = ""; + String end = ""; + if(i <100 && (fileTextInfo.length() - i) <100){ + start = fileTextInfo.substring(0,i); + end = fileTextInfo.substring(i, fileTextInfo.length() - i); + }else if(i <100 && (fileTextInfo.length() - i) >= 100){ + start = fileTextInfo.substring(0, i); + end = fileTextInfo.substring(i, i+100); + }else if(i > 100 && (fileTextInfo.length() - i) >= 100){ + start = fileTextInfo.substring(i-100, i); + end = fileTextInfo.substring(i, i+100); + }else if(i > 100 && (fileTextInfo.length() - i) < 100){ + start = fileTextInfo.substring(i-100, i); + end = fileTextInfo.substring(i, fileTextInfo.length()); + } + String result = start + end; + return result; + } private String getFileText(String fileTextInfo, String selectValue) { String beginStr = ""; String endStr = "";