修改标题检索bug

This commit is contained in:
梁琦涛
2024-07-24 16:15:10 +08:00
parent 3f8f304197
commit b0d0ac4de8
2 changed files with 68 additions and 12 deletions
@@ -38,6 +38,7 @@ import com.jero.modules.laws.standard.service.ILawsDomesticStandardService;
import com.jero.modules.laws.standard.service.ILawsEnterpriseStandardService;
import com.jero.modules.laws.standard.service.ILawsNewsFeedService;
import com.jero.modules.laws.standard.service.ILawsOverseasStandardService;
import com.jero.modules.laws.utils.HighlightUtil;
import com.jero.modules.oss.entity.OSSFile;
import com.jero.modules.oss.service.IOSSFileService;
import com.jero.modules.tag.entity.LawsTag;
@@ -167,7 +168,9 @@ public class LawsHomeSearchServiceImpl implements ILawsHomeSearchService {
select.append("( ");
for (int i = 0; i < searchContent.length; i++) {
regexp.append(searchContent[i]);
select.append(" if(standard_name like '%").append(searchContent[i]).append("%',1,0) ");
select.append(" LENGTH(standard_name) - LENGTH(REPLACE(standard_name, '" + searchContent[i] + "'," +
" '" + (searchContent[i].length() > 1 ?
searchContent[i].substring(0,searchContent[i].length()-1) : "") + "')) ");
if(i != searchContent.length - 1){
regexp.append("|");
select.append("+");
@@ -175,7 +178,7 @@ public class LawsHomeSearchServiceImpl implements ILawsHomeSearchService {
}
select.append(") as sort_index");
QueryWrapper<LawsHomeNormalSearchDTO> queryWrapper = new QueryWrapper<>();
queryWrapper.select("id,standard_name,id,resource_type,standard_name," +
queryWrapper.select("id,standard_name,resource_type," +
"create_time,standard_type_order,standard_state_order,standard_class," +
"dynamic_type,applicable_vehicle,standard_property," +
"standard_state,standard_code,grade_classification,implementation_date, " + select);
@@ -259,11 +262,7 @@ public class LawsHomeSearchServiceImpl implements ILawsHomeSearchService {
LawsNewsFeed obj = op.get();
String[] text = searchContent.split(BLANK_SPACE);
String content = obj.getDynamicHeading();
for (int i = 0; i < text.length; i++) {
String searchContentSpan = SPAN_CLASS + text[i] + SPAN;
content = content.replace(text[i],searchContentSpan);
}
content = HighlightUtil.highlightKeywords(content, text, SPAN_CLASS,SPAN);
p.setTitle(content);
p.setReleaseDate(obj.getReleaseTime());
// 生成正文
@@ -291,11 +290,7 @@ public class LawsHomeSearchServiceImpl implements ILawsHomeSearchService {
private String getString(String searchContent, String standardNumber, String standardName) {
String[] text = searchContent.split(BLANK_SPACE);
String content = standardNumber + " " + standardName;
for (int i = 0; i < text.length; i++) {
String searchContentSpan = SPAN_CLASS + text[i] + SPAN;
content = content.replace(text[i], searchContentSpan);
}
content = HighlightUtil.highlightKeywords(content, text, SPAN_CLASS,SPAN);
return content;
}
@@ -0,0 +1,61 @@
package com.jero.modules.laws.utils;
/**
* @author lqt
* @version 1.0
* @date 2024/7/24 14:10
*/
public class HighlightUtil {
/**
* 替换字符串中的关键词为高亮显示格式,确保每个关键词只被替换一次,同时避免高亮标签自身被替换。
* @param text 原始字符串
* @param keywords 需要高亮显示的关键词数组
* @param highlightStartTag 高亮开始标签,如 "<span class='highlight'>"
* @param highlightEndTag 高亮结束标签,如 "</span>"
* @return 替换后的高亮字符串
*/
public static String highlightKeywords(String text, String[] keywords, String highlightStartTag,
String highlightEndTag) {
if (text == null || keywords == null || highlightStartTag == null || highlightEndTag == null) {
throw new IllegalArgumentException("Input parameters cannot be null.");
}
// 临时替换高亮标签以避免它们被误识别为关键词
String tempStartTag = "";
String tempEndTag = "ב";
text = text.replace(highlightStartTag, tempStartTag).replace(highlightEndTag, tempEndTag);
StringBuilder highlightedText = new StringBuilder(text);
for (String keyword : keywords) {
if (!keyword.isEmpty()) {
int keywordLength = keyword.length();
int startIndex = 0;
while ((startIndex = highlightedText.indexOf(keyword, startIndex)) != -1) {
highlightedText.replace(startIndex, startIndex + keywordLength,
tempStartTag + keyword + tempEndTag);
startIndex += tempStartTag.length() + keywordLength + tempEndTag.length(); // 跳过替换后文本的长度
}
}
}
// 将临时标签替换回原来的高亮标签
return highlightedText.toString().replace(tempStartTag, highlightStartTag).replace(tempEndTag, highlightEndTag);
}
public static void main(String[] args) {
// String originalText = "测试标准解读一二三四五六七八九十1234567890qwertyuiop一二三四五六七八九十1234—2024";
// String[] keywords = {"1", "T","s"};
// String highlightTag = "<span class='highlight'>";
// String highlightTagEnd = "</span>";
// String highlightedText = highlightKeywords(originalText, keywords, highlightTag,highlightTagEnd);
// System.out.println(highlightedText);
String name = "Alice%s";
int age = 30;
double height = 1.75;
String formatted = String.format("Name:%sAge:%sHeight: %.2f meters.", name, name, height);
System.out.println(formatted);
}
}