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;
@ -43,7 +45,7 @@ public class SearchController {
@Autowired @Autowired
private SearchService searchService; private SearchService searchService;
@Autowired @Autowired
private TagService tagService; private TagService tagService;
@ -67,25 +69,30 @@ 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) {
SearchCategoryDTO searchCategoryDTO = new SearchCategoryDTO(); SearchCategoryDTO searchCategoryDTO = new SearchCategoryDTO();
BeanUtils.copyProperties(tag, searchCategoryDTO); BeanUtils.copyProperties(tag, searchCategoryDTO);
searchCategoryDTO.setImage(Constants.TAG_RESOURCE_PREFIX+tag.getImage()); searchCategoryDTO.setImage(Constants.TAG_RESOURCE_PREFIX + tag.getImage());
return searchCategoryDTO; return searchCategoryDTO;
} }
} }

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