Skip to content

Commit caba96a

Browse files
committed
数据库的处理是不区分大小写的,那么这里的匹配串处理也应该不区分大小写,否则会出现不准确的结果
例如查询:/searchBlog?query=Java 1)修改前(查询结果中的字符串可能并不包含关键词): ... "content": "> NBlog部署维护流程记录(持续更新)" ... 2)修改后: ... "content": "核心代码。\n\n```java\n /*" ...
1 parent c0acff0 commit caba96a

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

blog-api/src/main/java/top/naccl/service/impl/BlogServiceImpl.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,16 @@ public List<Blog> getListByTitleAndCategoryId(String title, Integer categoryId)
7777
@Override
7878
public List<SearchBlog> getSearchBlogListByQueryAndIsPublished(String query) {
7979
List<SearchBlog> searchBlogs = blogMapper.getSearchBlogListByQueryAndIsPublished(query);
80+
// 数据库的处理是不区分大小写的,那么这里的匹配串处理也应该不区分大小写,否则会出现不准确的结果
81+
query = query.toUpperCase();
8082
for (SearchBlog searchBlog : searchBlogs) {
81-
String content = searchBlog.getContent();
83+
String content = searchBlog.getContent().toUpperCase();
8284
int contentLength = content.length();
8385
int index = content.indexOf(query) - 10;
84-
index = index < 0 ? 0 : index;
86+
index = Math.max(index, 0);
8587
int end = index + 21;//以关键字字符串为中心返回21个字
86-
end = end > contentLength - 1 ? contentLength - 1 : end;
87-
searchBlog.setContent(content.substring(index, end));
88+
end = Math.min(end, contentLength - 1);
89+
searchBlog.setContent(searchBlog.getContent().substring(index, end));
8890
}
8991
return searchBlogs;
9092
}

0 commit comments

Comments
 (0)