From d94101da0312aef51f4a038acdaf9112dc952abf Mon Sep 17 00:00:00 2001 From: Gary Date: Thu, 29 Feb 2024 14:48:38 +0800 Subject: [PATCH] 1.change fuzzy match in BOOLEAN MODE --- .../main/java/com/luoo/music/dao/JournalDao.java | 5 ++++- .../java/com/luoo/music/dao/SongInfoDao.java | 2 +- .../com/luoo/music/service/JournalService.java | 16 ++++++++++++++-- .../com/luoo/music/service/SongInfoService.java | 2 +- 4 files changed, 20 insertions(+), 5 deletions(-) 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 45126e2..4d756de 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 @@ -83,7 +83,10 @@ 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) + @Query(value = "select * from tb_journal where state='1' and is_publish='1' and journal_no=?1", nativeQuery = true) + Journal findValidJournalByJournalNo(String journalNo); + + @Query(value = "select * from tb_journal where match(journal_no, title) against(?1 IN BOOLEAN MODE) 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 d1ebbcb..3500ef2 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 @@ -39,6 +39,6 @@ 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) + @Query(value = "select * from tb_song_info where match(name, artist, album) against(?1 IN BOOLEAN MODE) 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 ac8e0b5..3ad8b78 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 @@ -2,6 +2,7 @@ package com.luoo.music.service; import java.time.LocalDateTime; import java.util.*; +import java.util.regex.Pattern; import java.util.stream.IntStream; import javax.persistence.criteria.CriteriaBuilder; @@ -38,7 +39,7 @@ import com.luoo.music.pojo.Journal; */ @Service public class JournalService { - + private static final Pattern NUMBER_PATTERN = Pattern.compile("[0-9]*"); @Autowired private JournalDao journalDao; @@ -285,11 +286,22 @@ public class JournalService { public List fuzzySearch(String keyword, PageRequest pageRequest) { + if(isNumeric(keyword)) { + Journal journal=journalDao.findValidJournalByJournalNo(keyword); + if(null!=journal) { + return Arrays.asList(journal); + } + } if(keyword.length()==1) { return searchByLike(keyword,pageRequest); } - return journalDao.fuzzySearch(keyword, pageRequest.getPageNumber()*pageRequest.getPageSize(), pageRequest.getPageSize()); + return journalDao.fuzzySearch("+\""+keyword+"\"", pageRequest.getPageNumber()*pageRequest.getPageSize(), pageRequest.getPageSize()); } + + public static boolean isNumeric(String str){ + return NUMBER_PATTERN.matcher(str).matches(); + } + private List searchByLike(String keyword, PageRequest pageRequest) { Specification specification = fuzzySpecification(keyword); return journalDao.findAll(specification,pageRequest).getContent(); 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 6b4ea17..197c8e1 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 @@ -39,7 +39,7 @@ public class SongInfoService { if(keyword.length()==1) { return searchByLike(keyword,pageRequest); } - return songInfoDao.fuzzySearch(keyword, pageRequest.getPageNumber()*pageRequest.getPageSize(), pageRequest.getPageSize()); + return songInfoDao.fuzzySearch("+\""+keyword+"\"", pageRequest.getPageNumber()*pageRequest.getPageSize(), pageRequest.getPageSize()); } private List searchByLike(String keyword, PageRequest pageRequest) {