1.change fuzzy match in BOOLEAN MODE

main
Gary 9 months ago
parent 9862028f81
commit d94101da03

@ -83,7 +83,10 @@ public interface JournalDao extends JpaRepository<Journal,String>,JpaSpecificati
@Query(value = "select * from tb_journal where state='1' and is_publish='1'", nativeQuery = true) @Query(value = "select * from tb_journal where state='1' and is_publish='1'", nativeQuery = true)
List<Journal> findValidJournals(); List<Journal> 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<Journal> fuzzySearch(String keyword, int offset, int limit); List<Journal> fuzzySearch(String keyword, int offset, int limit);
} }

@ -39,6 +39,6 @@ public interface SongInfoDao extends JpaRepository<SongInfo,String>, JpaSpecific
@Query(value = "select * from tb_song_info where state='1' ", nativeQuery = true) @Query(value = "select * from tb_song_info where state='1' ", nativeQuery = true)
List<SongInfo> findValidSongs(); List<SongInfo> 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<SongInfo> fuzzySearch(String keyword,int offset, int limit); List<SongInfo> fuzzySearch(String keyword,int offset, int limit);
} }

@ -2,6 +2,7 @@ package com.luoo.music.service;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.*; import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.IntStream; import java.util.stream.IntStream;
import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaBuilder;
@ -38,7 +39,7 @@ import com.luoo.music.pojo.Journal;
*/ */
@Service @Service
public class JournalService { public class JournalService {
private static final Pattern NUMBER_PATTERN = Pattern.compile("[0-9]*");
@Autowired @Autowired
private JournalDao journalDao; private JournalDao journalDao;
@ -285,11 +286,22 @@ public class JournalService {
public List<Journal> fuzzySearch(String keyword, PageRequest pageRequest) { public List<Journal> fuzzySearch(String keyword, PageRequest pageRequest) {
if(isNumeric(keyword)) {
Journal journal=journalDao.findValidJournalByJournalNo(keyword);
if(null!=journal) {
return Arrays.asList(journal);
}
}
if(keyword.length()==1) { if(keyword.length()==1) {
return searchByLike(keyword,pageRequest); 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<Journal> searchByLike(String keyword, PageRequest pageRequest) { private List<Journal> searchByLike(String keyword, PageRequest pageRequest) {
Specification<Journal> specification = fuzzySpecification(keyword); Specification<Journal> specification = fuzzySpecification(keyword);
return journalDao.findAll(specification,pageRequest).getContent(); return journalDao.findAll(specification,pageRequest).getContent();

@ -39,7 +39,7 @@ public class SongInfoService {
if(keyword.length()==1) { if(keyword.length()==1) {
return searchByLike(keyword,pageRequest); 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<SongInfo> searchByLike(String keyword, PageRequest pageRequest) { private List<SongInfo> searchByLike(String keyword, PageRequest pageRequest) {

Loading…
Cancel
Save