Skip to content

Commit a41bedd

Browse files
authored
Merge pull request #132 from Chaobk/master
fix: 关键词查询不区分大小写
2 parents c0acff0 + caba96a commit a41bedd

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)