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.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<Journal> journals = journalService.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()));
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<List<String>> 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;
}
}

@ -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) {
Set<String> values=redisTemplate.opsForZSet().rangeByScore(REDIS_AUTO_COMPLETE+query, 0, Double.MAX_VALUE, 0, 10);
public List<String> autoComplete(String query, int limit) {
Set<String> values=redisTemplate.opsForZSet().rangeByScore(REDIS_AUTO_COMPLETE+query, 0, Double.MAX_VALUE, 0, limit);
return new ArrayList<>(values);
}

Loading…
Cancel
Save