1.add limit for autoComplete

main
Gary 9 months ago
parent 72bb00b392
commit 2370d3de24

@ -5,6 +5,7 @@ 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.response.SearchCategoryDTO; import com.luoo.music.dto.response.SearchCategoryDTO;
import com.luoo.music.dto.response.SearchResultDTO; import com.luoo.music.dto.response.SearchResultDTO;
import io.swagger.annotations.*; import io.swagger.annotations.*;
@ -35,6 +36,7 @@ import constants.Constants;
@Api(tags = "雀跃APP搜索 APIs") @Api(tags = "雀跃APP搜索 APIs")
@RequestMapping("/search") @RequestMapping("/search")
public class SearchController { public class SearchController {
private static final int DEFAULT_AUTO_COMPLETE_LIMIT = 10;
@Autowired @Autowired
private JournalService journalService; private JournalService journalService;
@ -67,19 +69,24 @@ public class SearchController {
SearchResultDTO searchResultDTO = new SearchResultDTO(); SearchResultDTO searchResultDTO = new SearchResultDTO();
List<Journal> journals = journalService.fuzzySearch(keyword); List<Journal> journals = journalService.fuzzySearch(keyword);
List<JournalSong> songs = journalSongService.fuzzySearch(keyword); List<JournalSong> 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())); searchResultDTO.setSongs(songs.stream().map(SongMapper::getSongRespDTO).collect(Collectors.toList()));
return Result.success(searchResultDTO); return Result.success(searchResultDTO);
} }
@ApiOperation(value = "3.搜索自动补全") @ApiOperation(value = "3.搜索自动补全", notes = "limit 默认查10条")
@ApiImplicitParams({ @ApiImplicitParam(name = "query", value = "搜索词", required = true) }) @GetMapping("/autoComplete")
@GetMapping("/autoComplete/{query}")
@GlobalInterceptor(checkAppUserLogin = true) @GlobalInterceptor(checkAppUserLogin = true)
public Result<List<String>> autoComplete( public Result<List<String>> autoComplete(
@RequestHeader(value = "Authorization", required = true) String authorization, @RequestHeader(value = "Authorization", required = true) String authorization,
@PathVariable @VerifyParam(required = true) String query) { @VerifyParam(required = true) AutoCompleteReq query) {
return Result.success(searchService.autoComplete(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) { private SearchCategoryDTO getSearchCategoryDTO(Tag tag) {

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

@ -76,8 +76,8 @@ public class SearchService {
} }
public List<String> autoComplete(String query) { public List<String> autoComplete(String query, int limit) {
Set<String> values=redisTemplate.opsForZSet().rangeByScore(REDIS_AUTO_COMPLETE+query, 0, Double.MAX_VALUE, 0, 10); Set<String> values=redisTemplate.opsForZSet().rangeByScore(REDIS_AUTO_COMPLETE+query, 0, Double.MAX_VALUE, 0, limit);
return new ArrayList<>(values); return new ArrayList<>(values);
} }

Loading…
Cancel
Save