From 2370d3de24ec9011ecc68e190838b137cd73ad24 Mon Sep 17 00:00:00 2001 From: Gary Date: Mon, 19 Feb 2024 23:23:38 +0800 Subject: [PATCH] 1.add limit for autoComplete --- .../music/controller/SearchController.java | 25 ++++++++++++------- .../music/dto/request/AutoCompleteReq.java | 23 +++++++++++++++++ .../com/luoo/music/service/SearchService.java | 4 +-- 3 files changed, 41 insertions(+), 11 deletions(-) create mode 100644 luoo_music/src/main/java/com/luoo/music/dto/request/AutoCompleteReq.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 07a3372..c7e3c85 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 @@ -5,6 +5,7 @@ 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.response.SearchCategoryDTO; import com.luoo.music.dto.response.SearchResultDTO; import io.swagger.annotations.*; @@ -35,6 +36,7 @@ import constants.Constants; @Api(tags = "雀跃APP搜索 APIs") @RequestMapping("/search") public class SearchController { + private static final int DEFAULT_AUTO_COMPLETE_LIMIT = 10; @Autowired private JournalService journalService; @@ -43,7 +45,7 @@ public class SearchController { @Autowired private SearchService searchService; - + @Autowired private TagService tagService; @@ -67,25 +69,30 @@ public class SearchController { 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.setJournals( + journals.stream().map(j -> JournalMapper.getJournalRespDTO(j)).collect(Collectors.toList())); searchResultDTO.setSongs(songs.stream().map(SongMapper::getSongRespDTO).collect(Collectors.toList())); return Result.success(searchResultDTO); } - @ApiOperation(value = "3.搜索自动补全") - @ApiImplicitParams({ @ApiImplicitParam(name = "query", value = "搜索词", required = true) }) - @GetMapping("/autoComplete/{query}") + @ApiOperation(value = "3.搜索自动补全", notes = "limit 默认查10条") + @GetMapping("/autoComplete") @GlobalInterceptor(checkAppUserLogin = true) public Result> autoComplete( @RequestHeader(value = "Authorization", required = true) String authorization, - @PathVariable @VerifyParam(required = true) String query) { - return Result.success(searchService.autoComplete(query)); + @VerifyParam(required = true) AutoCompleteReq query) { + int limit = getLimit(query.getLimit()); + return Result.success(searchService.autoComplete(query.getQuery(), limit)); + } + + private int getLimit(Integer limit) { + return null == limit || 0 == limit ? DEFAULT_AUTO_COMPLETE_LIMIT : limit; } - + private SearchCategoryDTO getSearchCategoryDTO(Tag tag) { SearchCategoryDTO searchCategoryDTO = new SearchCategoryDTO(); BeanUtils.copyProperties(tag, searchCategoryDTO); - searchCategoryDTO.setImage(Constants.TAG_RESOURCE_PREFIX+tag.getImage()); + searchCategoryDTO.setImage(Constants.TAG_RESOURCE_PREFIX + tag.getImage()); return searchCategoryDTO; } } diff --git a/luoo_music/src/main/java/com/luoo/music/dto/request/AutoCompleteReq.java b/luoo_music/src/main/java/com/luoo/music/dto/request/AutoCompleteReq.java new file mode 100644 index 0000000..78838d4 --- /dev/null +++ b/luoo_music/src/main/java/com/luoo/music/dto/request/AutoCompleteReq.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 AutoCompleteReq implements Serializable { + private static final long serialVersionUID = 1L; + @VerifyParam(required = true) + @ApiModelProperty(value = "查询分词", required = true) + private String query; + @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/SearchService.java b/luoo_music/src/main/java/com/luoo/music/service/SearchService.java index a44e842..446b783 100644 --- a/luoo_music/src/main/java/com/luoo/music/service/SearchService.java +++ b/luoo_music/src/main/java/com/luoo/music/service/SearchService.java @@ -76,8 +76,8 @@ public class SearchService { } - public List autoComplete(String query) { - Set values=redisTemplate.opsForZSet().rangeByScore(REDIS_AUTO_COMPLETE+query, 0, Double.MAX_VALUE, 0, 10); + public List autoComplete(String query, int limit) { + Set values=redisTemplate.opsForZSet().rangeByScore(REDIS_AUTO_COMPLETE+query, 0, Double.MAX_VALUE, 0, limit); return new ArrayList<>(values); }