搜索--搜索高亮
This commit is contained in:
+201
-53
@@ -486,45 +486,30 @@ public class DocumentSearchServiceImpl implements IDocumentSearchService {
|
|||||||
//二次查询
|
//二次查询
|
||||||
int count = 0;
|
int count = 0;
|
||||||
for (Map.Entry<String, Object> entry : mapSource.entrySet()) {
|
for (Map.Entry<String, Object> entry : mapSource.entrySet()) {
|
||||||
|
String valueHighlight = "";
|
||||||
String value = (String) entry.getValue();
|
String value = (String) entry.getValue();
|
||||||
//判断value中是否包含二次搜索中的字符
|
//判断value中是否包含二次搜索中的字符
|
||||||
boolean flag = judgeContains(value, selectValueTwo);
|
if(StringUtils.isNotBlank(value)){
|
||||||
StringBuilder sb = new StringBuilder();
|
if(value.contains(selectValueTwo)){
|
||||||
if (flag) {
|
value = subFileText(value, selectValueTwo);
|
||||||
//截取第一处高亮的前后100个字符
|
count++;
|
||||||
value = getFileText(value, selectValueTwo);
|
String replacementTwo = "<text class='highlight-class'>" + selectValueTwo + "</text>";
|
||||||
count++;
|
valueHighlight = value.replaceAll(selectValueTwo, replacementTwo);
|
||||||
for (int i = 0; i < value.length(); i++) {
|
//第一次搜索的也高亮
|
||||||
String chStr = String.valueOf(value.charAt(i));
|
if(value.contains(selectValue) && !selectValueTwo.contains(selectValue)){
|
||||||
//第一次的搜索条件也需要高亮
|
String replacementOne = "<text class='highlight-class'>" + selectValue + "</text>";
|
||||||
String condition = selectValueTwo + selectValue;
|
valueHighlight = valueHighlight.replaceAll(selectValue,replacementOne);
|
||||||
if (condition.contains(String.valueOf(chStr))) {
|
|
||||||
chStr = "<text class='highlight-class'>" + chStr + "</text>";
|
|
||||||
}
|
}
|
||||||
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++;
|
|
||||||
//如果内容中不包含第二次搜索条件,则继续高亮第一处
|
|
||||||
//判断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 = "<text class='highlight-class'>" + chStr + "</text>";
|
|
||||||
}
|
|
||||||
sb.append(String.valueOf(chStr));
|
|
||||||
}
|
}
|
||||||
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<String, Object> entry : mapSource.entrySet()) {
|
for (Map.Entry<String, Object> entry : mapSource.entrySet()) {
|
||||||
String key = entry.getKey();
|
String key = entry.getKey();
|
||||||
String value = String.valueOf(entry.getValue());
|
String value = String.valueOf(entry.getValue());
|
||||||
StringBuilder sb = new StringBuilder();
|
// StringBuilder sb = new StringBuilder();
|
||||||
|
String valueHighlight = "";
|
||||||
if (StringUtils.isNotBlank(value)) {
|
if (StringUtils.isNotBlank(value)) {
|
||||||
//file_text需要单独处理(截取第一处高亮的前后100个字符)
|
//file_text需要单独处理(截取第一处高亮的前后100个字符)
|
||||||
if ("file_text".equals(key)) {
|
if ("file_text".equals(key)) {
|
||||||
//判断value中是否包含搜索中的字符
|
//判断value中是否包含搜索中的字符
|
||||||
boolean flag = judgeContains(value, selectValue);
|
if(value.contains(selectValue)){
|
||||||
if (flag) {
|
|
||||||
//截取第一处高亮的前后100个字符
|
//截取第一处高亮的前后100个字符
|
||||||
value = getFileText(value, selectValue);
|
value = subFileText(value, selectValue);
|
||||||
} else {
|
}else{
|
||||||
//第一次搜索的时候file_text中不包含搜索条件的时候将file_text赋值空(此时展示拼接的内容)
|
//第一次搜索的时候file_text中不包含搜索条件的时候将file_text赋值空(此时展示拼接的内容)
|
||||||
entry.setValue("");
|
entry.setValue("");
|
||||||
value = "";
|
value = "";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ("content".equals(key)) {
|
if ("content".equals(key)) {
|
||||||
boolean flag = judgeContains(value, selectValue);
|
if(value.contains(selectValue)){
|
||||||
if (flag) {
|
value = subFileText(value, selectValue);
|
||||||
//截取第一处高亮的前后100个字符
|
|
||||||
value = getFileText(value, selectValue);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (int i = 0; i < value.length(); i++) {
|
String replacement = "<text class='highlight-class'>" + selectValue + "</text>";
|
||||||
String chStr = String.valueOf(value.charAt(i));
|
valueHighlight = value.replaceAll(selectValue, replacement);
|
||||||
if (selectValue.contains(String.valueOf(chStr))) {
|
|
||||||
chStr = "<text class='highlight-class'>" + chStr + "</text>";
|
|
||||||
}
|
|
||||||
sb.append(String.valueOf(chStr));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (ObjectUtils.isNotEmpty(sb)) {
|
if (ObjectUtils.isNotEmpty(valueHighlight)) {
|
||||||
entry.setValue(sb.toString());
|
entry.setValue(valueHighlight);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -590,7 +568,177 @@ public class DocumentSearchServiceImpl implements IDocumentSearchService {
|
|||||||
}
|
}
|
||||||
return page;
|
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<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<>();
|
||||||
|
//
|
||||||
|
// 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 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 = "<text class='highlight-class'>" + chStr + "</text>";
|
||||||
|
// }
|
||||||
|
// 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 = "<text class='highlight-class'>" + chStr + "</text>";
|
||||||
|
// }
|
||||||
|
// 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<String, Object> 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 = "<text class='highlight-class'>" + chStr + "</text>";
|
||||||
|
// }
|
||||||
|
// 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) {
|
private String getFileText(String fileTextInfo, String selectValue) {
|
||||||
String beginStr = "";
|
String beginStr = "";
|
||||||
String endStr = "";
|
String endStr = "";
|
||||||
|
|||||||
Reference in New Issue
Block a user