From 25717ffbd9e4da55c10064989430d317feafad32 Mon Sep 17 00:00:00 2001 From: Gary Date: Wed, 21 Feb 2024 17:59:34 +0800 Subject: [PATCH] 1.speed up fuzzy search --- .../music/controller/SearchController.java | 27 ++++++++++++------- .../music/dto/request/FuzzySearchReq.java | 23 ++++++++++++++++ .../luoo/music/service/JournalService.java | 7 ++--- .../music/service/JournalSongService.java | 8 ++++-- 4 files changed, 51 insertions(+), 14 deletions(-) create mode 100644 luoo_music/src/main/java/com/luoo/music/dto/request/FuzzySearchReq.java 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 7eb0423..947f7e7 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 @@ -1,13 +1,19 @@ package com.luoo.music.controller; import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; import java.util.stream.Collectors; import com.luoo.music.dto.mapper.JournalMapper; import com.luoo.music.dto.mapper.SongMapper; import com.luoo.music.dto.request.AutoCompleteReq; +import com.luoo.music.dto.request.FuzzySearchReq; +import com.luoo.music.dto.response.JournalRespDTO; import com.luoo.music.dto.response.SearchCategoryDTO; import com.luoo.music.dto.response.SearchResultDTO; +import com.luoo.music.dto.response.SongRespDTO; + import io.swagger.annotations.*; import org.springframework.beans.BeanUtils; @@ -59,19 +65,22 @@ public class SearchController { return Result.success(searchCategoryDTOs); } - @ApiOperation(value = "2.搜索期刊/歌曲", notes = "根据关键词模糊搜索,如 期刊号,歌曲名 等") - @ApiImplicitParams({ @ApiImplicitParam(name = "keyword", value = "搜索关键词", required = true) }) - @GetMapping("/fuzzy/{keyword}") + @ApiOperation(value = "2.搜索期刊/歌曲", notes = "根据关键词模糊搜索,如 期刊号,歌曲名 等, limit 默认查10条") + @GetMapping("/fuzzy") @GlobalInterceptor(checkAppUserLogin = true) public Result fuzzySearch( @RequestHeader(value = "Authorization", required = false) String authorization, - @PathVariable @VerifyParam(required = true) String keyword) { + @VerifyParam(required = true) FuzzySearchReq query) throws InterruptedException, ExecutionException { SearchResultDTO searchResultDTO = new SearchResultDTO(); - List journals = journalService.fuzzySearch(keyword); - List songs = journalSongService.fuzzySearch(keyword); - searchResultDTO.setJournals( - journals.stream().map(j -> JournalMapper.getJournalRespDTO(j)).collect(Collectors.toList())); - searchResultDTO.setSongs(songs.stream().map(SongMapper::getSongRespDTO).collect(Collectors.toList())); + int limit = getLimit(query.getLimit()); + + CompletableFuture> cfa = CompletableFuture.supplyAsync(() -> journalService.fuzzySearch(query.getKeyword(),limit).stream().map(JournalMapper::getJournalRespDTO).collect(Collectors.toList())); + CompletableFuture> cfb = CompletableFuture.supplyAsync(() -> journalSongService.fuzzySearch(query.getKeyword(),limit).stream().map(SongMapper::getSongRespDTO).collect(Collectors.toList())); + List journals = cfa.get(); + List songs = cfb.get(); + + searchResultDTO.setJournals(journals); + searchResultDTO.setSongs(songs); return Result.success(searchResultDTO); } diff --git a/luoo_music/src/main/java/com/luoo/music/dto/request/FuzzySearchReq.java b/luoo_music/src/main/java/com/luoo/music/dto/request/FuzzySearchReq.java new file mode 100644 index 0000000..685ca28 --- /dev/null +++ b/luoo_music/src/main/java/com/luoo/music/dto/request/FuzzySearchReq.java @@ -0,0 +1,23 @@ +package com.luoo.music.dto.request; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +import java.io.Serializable; + +import annotation.VerifyParam; + +/** + * 搜索自动补全请求参数 + */ +@Data +@ApiModel +public class FuzzySearchReq implements Serializable { + private static final long serialVersionUID = 1L; + @VerifyParam(required = true) + @ApiModelProperty(value = "关键词", required = true) + private String keyword; + @VerifyParam(required = false) + @ApiModelProperty(value = "默认返10条, null/0 返回10 条,-1 返回所有", example = "10") + private Integer 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 04112dc..b63c741 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,9 +284,10 @@ public class JournalService { } - public List fuzzySearch(String keyword) { + public List fuzzySearch(String keyword, int limit) { + PageRequest pageRequest = PageRequest.of(0, limit); Specification specification = fuzzySpecification(keyword); - return journalDao.findAll(specification); + return journalDao.findAll(specification,pageRequest).getContent(); } private Specification fuzzySpecification(String keyword) { return new Specification() { @@ -297,8 +298,8 @@ public class JournalService { predicateList.add(criteriaBuilder.like(root.get("journalNo").as(String.class),"%"+keyword+"%")); predicateList.add(criteriaBuilder.like(root.get("title").as(String.class),"%"+keyword+"%")); - query.orderBy(criteriaBuilder.desc(root.get("journalNo").as(Integer.class))); + return criteriaBuilder.or( predicateList.toArray(new Predicate[predicateList.size()])); } }; diff --git a/luoo_music/src/main/java/com/luoo/music/service/JournalSongService.java b/luoo_music/src/main/java/com/luoo/music/service/JournalSongService.java index 4afc444..793670f 100644 --- a/luoo_music/src/main/java/com/luoo/music/service/JournalSongService.java +++ b/luoo_music/src/main/java/com/luoo/music/service/JournalSongService.java @@ -1,6 +1,7 @@ package com.luoo.music.service; import com.luoo.music.dao.JournalSongDao; +import com.luoo.music.dto.response.SearchCategoryDTO; import com.luoo.music.pojo.Journal; import com.luoo.music.pojo.JournalSong; @@ -18,6 +19,7 @@ import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Root; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Map; @@ -155,9 +157,10 @@ public class JournalSongService { return journalSongDao.orderByField(objectIds); } - public List fuzzySearch(String keyword) { + public List fuzzySearch(String keyword, int limit) { + PageRequest pageRequest = PageRequest.of(0, limit); Specification specification = fuzzySpecification(keyword); - return journalSongDao.findAll(specification); + return journalSongDao.findAll(specification,pageRequest).getContent(); } private Specification fuzzySpecification(String keyword) { return new Specification() { @@ -174,4 +177,5 @@ public class JournalSongService { } }; } + }