Authored by Gino Zhang

增加一个suggestList接口用于维护

@@ -34,7 +34,7 @@ public class SuggestController { @@ -34,7 +34,7 @@ public class SuggestController {
34 } 34 }
35 35
36 /** 36 /**
37 - * 搜索建议接口 37 + * 搜索提示接口 提供给内部使用
38 * 38 *
39 * @return 39 * @return
40 */ 40 */
@@ -42,6 +42,18 @@ public class SuggestController { @@ -42,6 +42,18 @@ public class SuggestController {
42 @ResponseBody 42 @ResponseBody
43 public SearchApiResult suggestConversion(HttpServletRequest request) { 43 public SearchApiResult suggestConversion(HttpServletRequest request) {
44 Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request); 44 Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
45 - return suggestService.suggestConversion(paramMap); 45 + return suggestService.suggestConversionList(paramMap);
  46 + }
  47 +
  48 + /**
  49 + * 搜索建议词接口 提供给内部使用
  50 + *
  51 + * @return
  52 + */
  53 + @RequestMapping(method = RequestMethod.GET, value = "tools/suggestList")
  54 + @ResponseBody
  55 + public SearchApiResult suggestList(HttpServletRequest request) {
  56 + Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
  57 + return suggestService.suggestList(paramMap);
46 } 58 }
47 } 59 }
@@ -29,5 +29,13 @@ public interface ISuggestService { @@ -29,5 +29,13 @@ public interface ISuggestService {
29 * @param paramMap 查询参数 29 * @param paramMap 查询参数
30 * @return 满足查询要求的转换关系 30 * @return 满足查询要求的转换关系
31 */ 31 */
32 - SearchApiResult suggestConversion(Map<String, String> paramMap); 32 + SearchApiResult suggestConversionList(Map<String, String> paramMap);
  33 +
  34 + /**
  35 + * 根据关键词查询suggest索引
  36 + *
  37 + * @param paramMap 查询参数
  38 + * @return 满足查询要求的建议词
  39 + */
  40 + SearchApiResult suggestList(Map<String, String> paramMap);
33 } 41 }
@@ -55,7 +55,7 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl @@ -55,7 +55,7 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl
55 // 返回智能搜索词限制关联商品数量限制 55 // 返回智能搜索词限制关联商品数量限制
56 private static final int SMART_SUGGESTION_COUNT_LIMIT = 20; 56 private static final int SMART_SUGGESTION_COUNT_LIMIT = 20;
57 57
58 - private static final List<String> DEFAULT_SUGGEST_TIPS = Arrays.asList("潮流","时尚","休闲"); 58 + private static final List<String> DEFAULT_SUGGEST_TIPS = Arrays.asList("潮流", "时尚", "休闲");
59 59
60 @Autowired 60 @Autowired
61 private SearchCommonService searchCommonService; 61 private SearchCommonService searchCommonService;
@@ -464,10 +464,10 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl @@ -464,10 +464,10 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl
464 * @return 满足查询要求的转换关系 464 * @return 满足查询要求的转换关系
465 */ 465 */
466 @Override 466 @Override
467 - public SearchApiResult suggestConversion(Map<String, String> paramMap) { 467 + public SearchApiResult suggestConversionList(Map<String, String> paramMap) {
468 try { 468 try {
469 long begin = System.currentTimeMillis(); 469 long begin = System.currentTimeMillis();
470 - logger.info("[func=suggestConversion][param={}][begin={}]", paramMap, begin); 470 + logger.info("[func=suggestConversionList][param={}][begin={}]", paramMap, begin);
471 471
472 String queryWord = paramMap.get("query"); 472 String queryWord = paramMap.get("query");
473 int updateTime = StringUtils.isBlank(paramMap.get("updateTime")) ? 0 : Integer.parseInt(paramMap.get("updateTime")); 473 int updateTime = StringUtils.isBlank(paramMap.get("updateTime")) ? 0 : Integer.parseInt(paramMap.get("updateTime"));
@@ -487,6 +487,11 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl @@ -487,6 +487,11 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl
487 searchParam.setFiter(QueryBuilders.rangeQuery("updateTime").gte(updateTime)); 487 searchParam.setFiter(QueryBuilders.rangeQuery("updateTime").gte(updateTime));
488 } 488 }
489 489
  490 + if (StringUtils.isEmpty(queryWord)) {
  491 + // 没有传入query时按照时间降序排序
  492 + searchParam.setSortBuilders(Arrays.asList(SortBuilders.fieldSort("updateTime").order(SortOrder.DESC)));
  493 + }
  494 +
490 // 2) 调用ES查询 495 // 2) 调用ES查询
491 SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_CONVERSION, searchParam); 496 SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_CONVERSION, searchParam);
492 if (searchResult == null) { 497 if (searchResult == null) {
@@ -500,7 +505,54 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl @@ -500,7 +505,54 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl
500 dataMap.put("page_size", searchParam.getSize()); 505 dataMap.put("page_size", searchParam.getSize());
501 dataMap.put("page_total", searchResult.getTotalPage()); 506 dataMap.put("page_total", searchResult.getTotalPage());
502 dataMap.put("conversion_list", searchResult.getResultList()); 507 dataMap.put("conversion_list", searchResult.getResultList());
503 - logger.info("[func=suggestConversion][cost={}]", System.currentTimeMillis() - begin); 508 + logger.info("[func=suggestConversionList][cost={}]", System.currentTimeMillis() - begin);
  509 + return new SearchApiResult().setData(dataMap);
  510 + } catch (Exception e) {
  511 + logger.error(e.getMessage(), e);
  512 + return new SearchApiResult().setCode(500).setMessage(e.getMessage()).setData(null);
  513 + }
  514 + }
  515 +
  516 + /**
  517 + * 根据关键词查询suggest索引
  518 + *
  519 + * @param paramMap 查询参数
  520 + * @return 满足查询要求的建议词
  521 + */
  522 + @Override
  523 + public SearchApiResult suggestList(Map<String, String> paramMap) {
  524 + try {
  525 + long begin = System.currentTimeMillis();
  526 + logger.info("[func=suggestList][param={}][begin={}]", paramMap, begin);
  527 +
  528 + String queryWord = paramMap.get("query");
  529 + int page = StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page"));
  530 + if (page < 1) {
  531 + throw new IllegalArgumentException("分页参数不合法");
  532 + }
  533 +
  534 + // 1) 构建ES请求
  535 + SearchParam searchParam = new SearchParam();
  536 + QueryBuilder queryBuilder = StringUtils.isNotEmpty(queryWord) ? QueryBuilders.matchQuery("keyword.keyword_ik", queryWord) : QueryBuilders.matchAllQuery();
  537 + searchParam.setQuery(queryBuilder);
  538 + searchParam.setPage(page);
  539 + searchParam.setOffset((page - 1) * 10);
  540 + searchParam.setSize(10);
  541 +
  542 + // 2) 调用ES查询
  543 + SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_SUGGEST, searchParam);
  544 + if (searchResult == null) {
  545 + return null;
  546 + }
  547 +
  548 + // 3) 返回结果
  549 + Map<String, Object> dataMap = new HashMap<>();
  550 + dataMap.put("total", searchResult.getTotal());
  551 + dataMap.put("page", searchResult.getPage());
  552 + dataMap.put("page_size", searchParam.getSize());
  553 + dataMap.put("page_total", searchResult.getTotalPage());
  554 + dataMap.put("suggest_list", searchResult.getResultList());
  555 + logger.info("[func=suggestList][cost={}]", System.currentTimeMillis() - begin);
504 return new SearchApiResult().setData(dataMap); 556 return new SearchApiResult().setData(dataMap);
505 } catch (Exception e) { 557 } catch (Exception e) {
506 logger.error(e.getMessage(), e); 558 logger.error(e.getMessage(), e);