1.seperate journal/song fuzzy search

main
Gary 9 months ago
parent 9af5009530
commit df98d5aef7

@ -18,6 +18,8 @@ import io.swagger.annotations.*;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import com.luoo.music.pojo.Journal; import com.luoo.music.pojo.Journal;
import com.luoo.music.pojo.JournalSong; import com.luoo.music.pojo.JournalSong;
@ -28,6 +30,7 @@ import com.luoo.music.service.SearchService;
import com.luoo.music.service.TagService; import com.luoo.music.service.TagService;
import annotation.GlobalInterceptor; import annotation.GlobalInterceptor;
import annotation.VerifyParam; import annotation.VerifyParam;
import api.PageResult;
import api.Result; import api.Result;
import constants.Constants; import constants.Constants;
@ -65,23 +68,39 @@ public class SearchController {
return Result.success(searchCategoryDTOs); return Result.success(searchCategoryDTOs);
} }
@ApiOperation(value = "2.搜索期刊/歌曲", notes = "根据关键词模糊搜索,如 期刊号,歌曲名 等, limit 默认查10条") @ApiOperation(value = "2.1 搜索歌曲", notes = "根据关键词模糊搜索歌曲")
@GetMapping("/fuzzy") @GetMapping("/fuzzy/song")
@GlobalInterceptor(checkAppUserLogin = true) @GlobalInterceptor(checkAppUserLogin = true)
public Result<SearchResultDTO> fuzzySearch( public Result<PageResult<SongRespDTO>> fuzzySearchSong(
@RequestHeader(value = "Authorization", required = false) String authorization, @RequestHeader(value = "Authorization", required = false) String authorization,
@VerifyParam(required = true) FuzzySearchReq query) throws InterruptedException, ExecutionException { @VerifyParam(required = true) FuzzySearchReq query) throws InterruptedException, ExecutionException {
SearchResultDTO searchResultDTO = new SearchResultDTO(); PageRequest pageRequest = PageRequest.of(getPageNum(query.getPageNum()), getPageSize(query.getPageSize()));
int limit = getLimit(query.getLimit());
CompletableFuture<List<JournalRespDTO>> cfa = CompletableFuture.supplyAsync(() -> journalService.fuzzySearch(query.getKeyword(),limit).stream().map(JournalMapper::getJournalRespDTO).collect(Collectors.toList())); Page<JournalSong> pageResults=journalSongService.fuzzySearch(query.getKeyword(),pageRequest);
CompletableFuture<List<SongRespDTO>> cfb = CompletableFuture.supplyAsync(() -> journalSongService.fuzzySearch(query.getKeyword(),limit).stream().map(SongMapper::getSongRespDTO).collect(Collectors.toList())); List<SongRespDTO> list = pageResults.stream().map(SongMapper::getSongRespDTO)
List<JournalRespDTO> journals = cfa.get(); .collect(Collectors.toList());
List<SongRespDTO> songs = cfb.get(); return Result.success(new PageResult<SongRespDTO>(Long.valueOf(list.size()), list));
}
private int getPageSize(Integer pageSize) {
return null==pageSize||0==pageSize?10:pageSize;
}
private int getPageNum(Integer pageNum) {
return null==pageNum?0:pageNum-1;
}
searchResultDTO.setJournals(journals); @ApiOperation(value = "2.2 搜索期刊", notes = "根据关键词模糊搜索期刊")
searchResultDTO.setSongs(songs); @GetMapping("/fuzzy/journal")
return Result.success(searchResultDTO); @GlobalInterceptor(checkAppUserLogin = true)
public Result<PageResult<JournalRespDTO>> fuzzySearchJournal(
@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<JournalRespDTO> list = pageResults.stream().map(JournalMapper::getJournalRespDTO)
.collect(Collectors.toList());
return Result.success(new PageResult<JournalRespDTO>(Long.valueOf(list.size()), list));
} }
@ApiOperation(value = "3.搜索自动补全", notes = "limit 默认查10条") @ApiOperation(value = "3.搜索自动补全", notes = "limit 默认查10条")

@ -7,9 +7,6 @@ import java.io.Serializable;
import annotation.VerifyParam; import annotation.VerifyParam;
/**
*
*/
@Data @Data
@ApiModel @ApiModel
public class FuzzySearchReq implements Serializable { public class FuzzySearchReq implements Serializable {
@ -18,6 +15,9 @@ public class FuzzySearchReq implements Serializable {
@ApiModelProperty(value = "关键词", required = true) @ApiModelProperty(value = "关键词", required = true)
private String keyword; private String keyword;
@VerifyParam(required = false) @VerifyParam(required = false)
@ApiModelProperty(value = "默认返10条", example = "10") @ApiModelProperty(value = "分页: 页码", example = "1")
private Integer limit; private Integer pageNum;
@VerifyParam(required = false)
@ApiModelProperty(value = "分页: 每页数量", example = "10")
private Integer pageSize;
} }

@ -284,10 +284,9 @@ public class JournalService {
} }
public List<Journal> fuzzySearch(String keyword, int limit) { public Page<Journal> fuzzySearch(String keyword, PageRequest pageRequest) {
PageRequest pageRequest = PageRequest.of(0, limit);
Specification<Journal> specification = fuzzySpecification(keyword); Specification<Journal> specification = fuzzySpecification(keyword);
return journalDao.findAll(specification,pageRequest).getContent(); return journalDao.findAll(specification,pageRequest);
} }
private Specification<Journal> fuzzySpecification(String keyword) { private Specification<Journal> fuzzySpecification(String keyword) {
return new Specification<Journal>() { return new Specification<Journal>() {
@ -298,7 +297,7 @@ public class JournalService {
predicateList.add(criteriaBuilder.like(root.get("journalNo").as(String.class),"%"+keyword+"%")); predicateList.add(criteriaBuilder.like(root.get("journalNo").as(String.class),"%"+keyword+"%"));
predicateList.add(criteriaBuilder.like(root.get("title").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))); query.orderBy(criteriaBuilder.asc(root.get("journalNo").as(Integer.class)));
return criteriaBuilder.or( predicateList.toArray(new Predicate[predicateList.size()])); return criteriaBuilder.or( predicateList.toArray(new Predicate[predicateList.size()]));
} }

@ -157,10 +157,9 @@ public class JournalSongService {
return journalSongDao.orderByField(objectIds); return journalSongDao.orderByField(objectIds);
} }
public List<JournalSong> fuzzySearch(String keyword, int limit) { public Page<JournalSong> fuzzySearch(String keyword, PageRequest pageRequest) {
PageRequest pageRequest = PageRequest.of(0, limit);
Specification<JournalSong> specification = fuzzySpecification(keyword); Specification<JournalSong> specification = fuzzySpecification(keyword);
return journalSongDao.findAll(specification,pageRequest).getContent(); return journalSongDao.findAll(specification,pageRequest);
} }
private Specification<JournalSong> fuzzySpecification(String keyword) { private Specification<JournalSong> fuzzySpecification(String keyword) {

Loading…
Cancel
Save