1.improve the performance of fuzzy search

main
Gary 9 months ago
parent 093911da73
commit 9862028f81

@ -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<SongInfo> pageResults=songInfoService.fuzzySearch(query.getKeyword(),pageRequest);
List<SongInfo> pageResults=songInfoService.fuzzySearch(query.getKeyword(),pageRequest);
List<SongRespDTO> list = pageResults.stream().map(SongMapper::getSongRespDTO)
.collect(Collectors.toList());
return Result.success(new PageResult<SongRespDTO>(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<Journal> pageResults=journalService.fuzzySearch(query.getKeyword(),pageRequest);
List<Journal> pageResults=journalService.fuzzySearch(query.getKeyword(),pageRequest);
List<JournalRespDTO> list = pageResults.stream().map(JournalMapper::getJournalRespDTO)
.collect(Collectors.toList());
return Result.success(new PageResult<JournalRespDTO>(Long.valueOf(list.size()), list));

@ -82,5 +82,8 @@ public interface JournalDao extends JpaRepository<Journal,String>,JpaSpecificati
@Query(value = "select * from tb_journal where state='1' and is_publish='1'", nativeQuery = true)
List<Journal> findValidJournals();
@Query(value = "select * from tb_journal where match(journal_no, title) against(?1) limit ?2,?3 ", nativeQuery = true)
List<Journal> fuzzySearch(String keyword, int offset, int limit);
}

@ -38,4 +38,7 @@ public interface SongInfoDao extends JpaRepository<SongInfo,String>, JpaSpecific
@Query(value = "select * from tb_song_info where state='1' ", nativeQuery = true)
List<SongInfo> findValidSongs();
@Query(value = "select * from tb_song_info where match(name, artist, album) against(?1) limit ?2,?3 ", nativeQuery = true)
List<SongInfo> fuzzySearch(String keyword,int offset, int limit);
}

@ -284,10 +284,18 @@ public class JournalService {
}
public Page<Journal> fuzzySearch(String keyword, PageRequest pageRequest) {
public List<Journal> 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<Journal> searchByLike(String keyword, PageRequest pageRequest) {
Specification<Journal> specification = fuzzySpecification(keyword);
return journalDao.findAll(specification,pageRequest);
return journalDao.findAll(specification,pageRequest).getContent();
}
private Specification<Journal> fuzzySpecification(String keyword) {
return new Specification<Journal>() {
private static final long serialVersionUID = 1L;

@ -35,9 +35,16 @@ public class SongInfoService {
return songInfoDao.getSongById(id);
}
public Page<SongInfo> fuzzySearch(String keyword, PageRequest pageRequest) {
public List<SongInfo> 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<SongInfo> searchByLike(String keyword, PageRequest pageRequest) {
Specification<SongInfo> specification = fuzzySpecification(keyword);
return songInfoDao.findAll(specification,pageRequest);
return songInfoDao.findAll(specification,pageRequest).getContent();
}
private Specification<SongInfo> fuzzySpecification(String keyword) {

Loading…
Cancel
Save