Authored by Gino Zhang

增加一个suggestList接口用于维护

... ... @@ -34,7 +34,7 @@ public class SuggestController {
}
/**
* 搜索建议接口
* 搜索提示接口 提供给内部使用
*
* @return
*/
... ... @@ -42,6 +42,18 @@ public class SuggestController {
@ResponseBody
public SearchApiResult suggestConversion(HttpServletRequest request) {
Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
return suggestService.suggestConversion(paramMap);
return suggestService.suggestConversionList(paramMap);
}
/**
* 搜索建议词接口 提供给内部使用
*
* @return
*/
@RequestMapping(method = RequestMethod.GET, value = "tools/suggestList")
@ResponseBody
public SearchApiResult suggestList(HttpServletRequest request) {
Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
return suggestService.suggestList(paramMap);
}
}
... ...
... ... @@ -29,5 +29,13 @@ public interface ISuggestService {
* @param paramMap 查询参数
* @return 满足查询要求的转换关系
*/
SearchApiResult suggestConversion(Map<String, String> paramMap);
SearchApiResult suggestConversionList(Map<String, String> paramMap);
/**
* 根据关键词查询suggest索引
*
* @param paramMap 查询参数
* @return 满足查询要求的建议词
*/
SearchApiResult suggestList(Map<String, String> paramMap);
}
... ...
... ... @@ -55,7 +55,7 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl
// 返回智能搜索词限制关联商品数量限制
private static final int SMART_SUGGESTION_COUNT_LIMIT = 20;
private static final List<String> DEFAULT_SUGGEST_TIPS = Arrays.asList("潮流","时尚","休闲");
private static final List<String> DEFAULT_SUGGEST_TIPS = Arrays.asList("潮流", "时尚", "休闲");
@Autowired
private SearchCommonService searchCommonService;
... ... @@ -464,10 +464,10 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl
* @return 满足查询要求的转换关系
*/
@Override
public SearchApiResult suggestConversion(Map<String, String> paramMap) {
public SearchApiResult suggestConversionList(Map<String, String> paramMap) {
try {
long begin = System.currentTimeMillis();
logger.info("[func=suggestConversion][param={}][begin={}]", paramMap, begin);
logger.info("[func=suggestConversionList][param={}][begin={}]", paramMap, begin);
String queryWord = paramMap.get("query");
int updateTime = StringUtils.isBlank(paramMap.get("updateTime")) ? 0 : Integer.parseInt(paramMap.get("updateTime"));
... ... @@ -487,6 +487,11 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl
searchParam.setFiter(QueryBuilders.rangeQuery("updateTime").gte(updateTime));
}
if (StringUtils.isEmpty(queryWord)) {
// 没有传入query时按照时间降序排序
searchParam.setSortBuilders(Arrays.asList(SortBuilders.fieldSort("updateTime").order(SortOrder.DESC)));
}
// 2) 调用ES查询
SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_CONVERSION, searchParam);
if (searchResult == null) {
... ... @@ -500,7 +505,54 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl
dataMap.put("page_size", searchParam.getSize());
dataMap.put("page_total", searchResult.getTotalPage());
dataMap.put("conversion_list", searchResult.getResultList());
logger.info("[func=suggestConversion][cost={}]", System.currentTimeMillis() - begin);
logger.info("[func=suggestConversionList][cost={}]", System.currentTimeMillis() - begin);
return new SearchApiResult().setData(dataMap);
} catch (Exception e) {
logger.error(e.getMessage(), e);
return new SearchApiResult().setCode(500).setMessage(e.getMessage()).setData(null);
}
}
/**
* 根据关键词查询suggest索引
*
* @param paramMap 查询参数
* @return 满足查询要求的建议词
*/
@Override
public SearchApiResult suggestList(Map<String, String> paramMap) {
try {
long begin = System.currentTimeMillis();
logger.info("[func=suggestList][param={}][begin={}]", paramMap, begin);
String queryWord = paramMap.get("query");
int page = StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page"));
if (page < 1) {
throw new IllegalArgumentException("分页参数不合法");
}
// 1) 构建ES请求
SearchParam searchParam = new SearchParam();
QueryBuilder queryBuilder = StringUtils.isNotEmpty(queryWord) ? QueryBuilders.matchQuery("keyword.keyword_ik", queryWord) : QueryBuilders.matchAllQuery();
searchParam.setQuery(queryBuilder);
searchParam.setPage(page);
searchParam.setOffset((page - 1) * 10);
searchParam.setSize(10);
// 2) 调用ES查询
SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_SUGGEST, searchParam);
if (searchResult == null) {
return null;
}
// 3) 返回结果
Map<String, Object> dataMap = new HashMap<>();
dataMap.put("total", searchResult.getTotal());
dataMap.put("page", searchResult.getPage());
dataMap.put("page_size", searchParam.getSize());
dataMap.put("page_total", searchResult.getTotalPage());
dataMap.put("suggest_list", searchResult.getResultList());
logger.info("[func=suggestList][cost={}]", System.currentTimeMillis() - begin);
return new SearchApiResult().setData(dataMap);
} catch (Exception e) {
logger.error(e.getMessage(), e);
... ...