diff --git a/luoo_music/src/main/java/com/luoo/music/controller/SearchController.java b/luoo_music/src/main/java/com/luoo/music/controller/SearchController.java index ea21ac4..a8a1453 100644 --- a/luoo_music/src/main/java/com/luoo/music/controller/SearchController.java +++ b/luoo_music/src/main/java/com/luoo/music/controller/SearchController.java @@ -78,7 +78,7 @@ public class SearchController { @VerifyParam(required = true) FuzzySearchReq query) throws InterruptedException, ExecutionException { PageRequest pageRequest = PageRequest.of(getPageNum(query.getPageNum()), getPageSize(query.getPageSize())); - Page pageResults=songInfoService.fuzzySearch(query.getKeyword(),pageRequest); + List pageResults=songInfoService.fuzzySearch(query.getKeyword(),pageRequest); List list = pageResults.stream().map(SongMapper::getSongRespDTO) .collect(Collectors.toList()); return Result.success(new PageResult(Long.valueOf(list.size()), list)); @@ -99,7 +99,7 @@ public class SearchController { @RequestHeader(value = "Authorization", required = false) String authorization, @VerifyParam(required = true) FuzzySearchReq query) throws InterruptedException, ExecutionException { PageRequest pageRequest = PageRequest.of(getPageNum(query.getPageNum()), getPageSize(query.getPageSize())); - Page pageResults=journalService.fuzzySearch(query.getKeyword(),pageRequest); + List pageResults=journalService.fuzzySearch(query.getKeyword(),pageRequest); List list = pageResults.stream().map(JournalMapper::getJournalRespDTO) .collect(Collectors.toList()); return Result.success(new PageResult(Long.valueOf(list.size()), list)); diff --git a/luoo_music/src/main/java/com/luoo/music/dao/JournalDao.java b/luoo_music/src/main/java/com/luoo/music/dao/JournalDao.java index f0a07b3..45126e2 100644 --- a/luoo_music/src/main/java/com/luoo/music/dao/JournalDao.java +++ b/luoo_music/src/main/java/com/luoo/music/dao/JournalDao.java @@ -82,5 +82,8 @@ public interface JournalDao extends JpaRepository,JpaSpecificati @Query(value = "select * from tb_journal where state='1' and is_publish='1'", nativeQuery = true) List findValidJournals(); + + @Query(value = "select * from tb_journal where match(journal_no, title) against(?1) limit ?2,?3 ", nativeQuery = true) + List fuzzySearch(String keyword, int offset, int limit); } diff --git a/luoo_music/src/main/java/com/luoo/music/dao/SongInfoDao.java b/luoo_music/src/main/java/com/luoo/music/dao/SongInfoDao.java index 1f1a27c..d1ebbcb 100644 --- a/luoo_music/src/main/java/com/luoo/music/dao/SongInfoDao.java +++ b/luoo_music/src/main/java/com/luoo/music/dao/SongInfoDao.java @@ -38,4 +38,7 @@ public interface SongInfoDao extends JpaRepository, JpaSpecific @Query(value = "select * from tb_song_info where state='1' ", nativeQuery = true) List findValidSongs(); + + @Query(value = "select * from tb_song_info where match(name, artist, album) against(?1) limit ?2,?3 ", nativeQuery = true) + List fuzzySearch(String keyword,int offset, int limit); } diff --git a/luoo_music/src/main/java/com/luoo/music/service/JournalService.java b/luoo_music/src/main/java/com/luoo/music/service/JournalService.java index ca29f41..ac8e0b5 100644 --- a/luoo_music/src/main/java/com/luoo/music/service/JournalService.java +++ b/luoo_music/src/main/java/com/luoo/music/service/JournalService.java @@ -284,10 +284,18 @@ public class JournalService { } - public Page fuzzySearch(String keyword, PageRequest pageRequest) { + public List fuzzySearch(String keyword, PageRequest pageRequest) { + if(keyword.length()==1) { + return searchByLike(keyword,pageRequest); + } + return journalDao.fuzzySearch(keyword, pageRequest.getPageNumber()*pageRequest.getPageSize(), pageRequest.getPageSize()); + } + private List searchByLike(String keyword, PageRequest pageRequest) { Specification specification = fuzzySpecification(keyword); - return journalDao.findAll(specification,pageRequest); + return journalDao.findAll(specification,pageRequest).getContent(); } + + private Specification fuzzySpecification(String keyword) { return new Specification() { private static final long serialVersionUID = 1L; diff --git a/luoo_music/src/main/java/com/luoo/music/service/SongInfoService.java b/luoo_music/src/main/java/com/luoo/music/service/SongInfoService.java index 9d82ef9..6b4ea17 100644 --- a/luoo_music/src/main/java/com/luoo/music/service/SongInfoService.java +++ b/luoo_music/src/main/java/com/luoo/music/service/SongInfoService.java @@ -35,11 +35,18 @@ public class SongInfoService { return songInfoDao.getSongById(id); } - public Page fuzzySearch(String keyword, PageRequest pageRequest) { - Specification specification = fuzzySpecification(keyword); - return songInfoDao.findAll(specification,pageRequest); + public List fuzzySearch(String keyword, PageRequest pageRequest) { + if(keyword.length()==1) { + return searchByLike(keyword,pageRequest); + } + return songInfoDao.fuzzySearch(keyword, pageRequest.getPageNumber()*pageRequest.getPageSize(), pageRequest.getPageSize()); } + private List searchByLike(String keyword, PageRequest pageRequest) { + Specification specification = fuzzySpecification(keyword); + return songInfoDao.findAll(specification,pageRequest).getContent(); + } + private Specification fuzzySpecification(String keyword) { return new Specification() { private static final long serialVersionUID = 1L;