1.speed up fuzzy search

main
Gary 9 months ago
parent c0a7d5961b
commit 25717ffbd9

@ -1,13 +1,19 @@
package com.luoo.music.controller; package com.luoo.music.controller;
import java.util.List; import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import com.luoo.music.dto.mapper.JournalMapper; import com.luoo.music.dto.mapper.JournalMapper;
import com.luoo.music.dto.mapper.SongMapper; import com.luoo.music.dto.mapper.SongMapper;
import com.luoo.music.dto.request.AutoCompleteReq; 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.SearchCategoryDTO;
import com.luoo.music.dto.response.SearchResultDTO; import com.luoo.music.dto.response.SearchResultDTO;
import com.luoo.music.dto.response.SongRespDTO;
import io.swagger.annotations.*; import io.swagger.annotations.*;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
@ -59,19 +65,22 @@ public class SearchController {
return Result.success(searchCategoryDTOs); return Result.success(searchCategoryDTOs);
} }
@ApiOperation(value = "2.搜索期刊/歌曲", notes = "根据关键词模糊搜索,如 期刊号,歌曲名 等") @ApiOperation(value = "2.搜索期刊/歌曲", notes = "根据关键词模糊搜索,如 期刊号,歌曲名 等, limit 默认查10条")
@ApiImplicitParams({ @ApiImplicitParam(name = "keyword", value = "搜索关键词", required = true) }) @GetMapping("/fuzzy")
@GetMapping("/fuzzy/{keyword}")
@GlobalInterceptor(checkAppUserLogin = true) @GlobalInterceptor(checkAppUserLogin = true)
public Result<SearchResultDTO> fuzzySearch( public Result<SearchResultDTO> fuzzySearch(
@RequestHeader(value = "Authorization", required = false) String authorization, @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(); SearchResultDTO searchResultDTO = new SearchResultDTO();
List<Journal> journals = journalService.fuzzySearch(keyword); int limit = getLimit(query.getLimit());
List<JournalSong> songs = journalSongService.fuzzySearch(keyword);
searchResultDTO.setJournals( CompletableFuture<List<JournalRespDTO>> cfa = CompletableFuture.supplyAsync(() -> journalService.fuzzySearch(query.getKeyword(),limit).stream().map(JournalMapper::getJournalRespDTO).collect(Collectors.toList()));
journals.stream().map(j -> JournalMapper.getJournalRespDTO(j)).collect(Collectors.toList())); CompletableFuture<List<SongRespDTO>> cfb = CompletableFuture.supplyAsync(() -> journalSongService.fuzzySearch(query.getKeyword(),limit).stream().map(SongMapper::getSongRespDTO).collect(Collectors.toList()));
searchResultDTO.setSongs(songs.stream().map(SongMapper::getSongRespDTO).collect(Collectors.toList())); List<JournalRespDTO> journals = cfa.get();
List<SongRespDTO> songs = cfb.get();
searchResultDTO.setJournals(journals);
searchResultDTO.setSongs(songs);
return Result.success(searchResultDTO); return Result.success(searchResultDTO);
} }

@ -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;
}

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

@ -1,6 +1,7 @@
package com.luoo.music.service; package com.luoo.music.service;
import com.luoo.music.dao.JournalSongDao; import com.luoo.music.dao.JournalSongDao;
import com.luoo.music.dto.response.SearchCategoryDTO;
import com.luoo.music.pojo.Journal; import com.luoo.music.pojo.Journal;
import com.luoo.music.pojo.JournalSong; import com.luoo.music.pojo.JournalSong;
@ -18,6 +19,7 @@ import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root; import javax.persistence.criteria.Root;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -155,9 +157,10 @@ public class JournalSongService {
return journalSongDao.orderByField(objectIds); return journalSongDao.orderByField(objectIds);
} }
public List<JournalSong> fuzzySearch(String keyword) { public List<JournalSong> fuzzySearch(String keyword, int limit) {
PageRequest pageRequest = PageRequest.of(0, limit);
Specification<JournalSong> specification = fuzzySpecification(keyword); Specification<JournalSong> specification = fuzzySpecification(keyword);
return journalSongDao.findAll(specification); return journalSongDao.findAll(specification,pageRequest).getContent();
} }
private Specification<JournalSong> fuzzySpecification(String keyword) { private Specification<JournalSong> fuzzySpecification(String keyword) {
return new Specification<JournalSong>() { return new Specification<JournalSong>() {
@ -174,4 +177,5 @@ public class JournalSongService {
} }
}; };
} }
} }

Loading…
Cancel
Save