Authored by Gino Zhang

suggestByConversion增加日志和注释

@@ -288,25 +288,39 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl @@ -288,25 +288,39 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl
288 } 288 }
289 289
290 private List<String> suggestByConversion(Map<String, String> paramMap) { 290 private List<String> suggestByConversion(Map<String, String> paramMap) {
  291 + String queryWord = paramMap.get(SearchRequestParams.PARAM_SEARCH_KEYWORD).toLowerCase();
  292 + long begin = System.currentTimeMillis();
  293 + logger.info("[func=suggestByConversion][query={}][begin={}]", queryWord, begin);
  294 +
  295 + // 1) 判断是否支持从conversion获取推荐词
291 if (!searchDynamicConfigService.isSearchSuggestionFromConversionOpen()) { 296 if (!searchDynamicConfigService.isSearchSuggestionFromConversionOpen()) {
292 return new ArrayList<>(); 297 return new ArrayList<>();
293 } 298 }
294 299
295 - String queryWord = paramMap.get(SearchRequestParams.PARAM_SEARCH_KEYWORD).toLowerCase(); 300 + // 2) 异步上报query关键词 用于统计和分析
296 searchKeyWordService.recordSuggestTip(queryWord); 301 searchKeyWordService.recordSuggestTip(queryWord);
  302 +
  303 + // 3) 从conversion索引中获取转换后的关键词列表
297 String dest = getSuggestConversionDestBySource(queryWord); 304 String dest = getSuggestConversionDestBySource(queryWord);
298 if (StringUtils.isEmpty(dest)) { 305 if (StringUtils.isEmpty(dest)) {
299 return new ArrayList<>(); 306 return new ArrayList<>();
300 } 307 }
301 308
  309 + // 4) 查询suggest索引 过滤和排序
  310 + // 4.1) 根据查询条件获得不同的count字段
302 final String countFiled = getCountField(paramMap); 311 final String countFiled = getCountField(paramMap);
303 SearchParam searchParam = new SearchParam(); 312 SearchParam searchParam = new SearchParam();
304 List<String> keywordsInDest = Arrays.stream(dest.split(",")).map(String::toLowerCase).map(String::trim).collect(Collectors.toList()); 313 List<String> keywordsInDest = Arrays.stream(dest.split(",")).map(String::toLowerCase).map(String::trim).collect(Collectors.toList());
  314 +
  315 + // 4.2) 设置keyword列表
305 QueryBuilder queryBuilder = QueryBuilders.termsQuery("keyword.keyword_lowercase", keywordsInDest); 316 QueryBuilder queryBuilder = QueryBuilders.termsQuery("keyword.keyword_lowercase", keywordsInDest);
306 FunctionScoreQueryBuilder functionScoreQueryBuilder = new FunctionScoreQueryBuilder(queryBuilder); 317 FunctionScoreQueryBuilder functionScoreQueryBuilder = new FunctionScoreQueryBuilder(queryBuilder);
307 - functionScoreQueryBuilder.add(QueryBuilders.termQuery("type", 2), ScoreFunctionBuilders.weightFactorFunction(keywordsInDest.size())); 318 +
  319 + // 4.3) 确保品类的关键词排在前面
  320 + functionScoreQueryBuilder.add(QueryBuilders.termQuery("type", 2), ScoreFunctionBuilders.weightFactorFunction(keywordsInDest.size() + 1));
  321 +
  322 + // 4.4) 根据顺序从高到低加分
308 for (int index = 0; index < keywordsInDest.size(); index++) { 323 for (int index = 0; index < keywordsInDest.size(); index++) {
309 - // 根据顺序从高到低加分  
310 functionScoreQueryBuilder.add(QueryBuilders.termQuery("keyword.keyword_lowercase", keywordsInDest.get(index)), 324 functionScoreQueryBuilder.add(QueryBuilders.termQuery("keyword.keyword_lowercase", keywordsInDest.get(index)),
311 ScoreFunctionBuilders.weightFactorFunction(keywordsInDest.size() - index)); 325 ScoreFunctionBuilders.weightFactorFunction(keywordsInDest.size() - index));
312 } 326 }
@@ -314,8 +328,11 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl @@ -314,8 +328,11 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl
314 searchParam.setQuery(functionScoreQueryBuilder); 328 searchParam.setQuery(functionScoreQueryBuilder);
315 searchParam.setPage(1); 329 searchParam.setPage(1);
316 searchParam.setSize(SMART_SUGGESTION_TERM_COUNT); 330 searchParam.setSize(SMART_SUGGESTION_TERM_COUNT);
  331 +
  332 + // 4.5) 过滤关联的商品数量小于20的关键词
317 searchParam.setFiter(QueryBuilders.rangeQuery(countFiled).gte(20)); 333 searchParam.setFiter(QueryBuilders.rangeQuery(countFiled).gte(20));
318 334
  335 + // 5) 先从缓存中获取
319 final String indexName = ISearchConstants.INDEX_NAME_SUGGEST; 336 final String indexName = ISearchConstants.INDEX_NAME_SUGGEST;
320 JSONObject jsonObject = searchCacheService.getJSONObjectFromCache(indexName, searchParam); 337 JSONObject jsonObject = searchCacheService.getJSONObjectFromCache(indexName, searchParam);
321 if (jsonObject != null) { 338 if (jsonObject != null) {
@@ -323,6 +340,7 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl @@ -323,6 +340,7 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl
323 return (List<String>) jsonObject.get("keywordList"); 340 return (List<String>) jsonObject.get("keywordList");
324 } 341 }
325 342
  343 + // 6) 调用ES获取结果
326 SearchResult searchResult = searchCommonService.doSearch(indexName, searchParam); 344 SearchResult searchResult = searchCommonService.doSearch(indexName, searchParam);
327 if (searchResult == null) { 345 if (searchResult == null) {
328 return new ArrayList<>(); 346 return new ArrayList<>();
@@ -334,8 +352,10 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl @@ -334,8 +352,10 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl
334 keywordList.add((String) record.get("keyword")); 352 keywordList.add((String) record.get("keyword"));
335 } 353 }
336 354
  355 + // 7) 将结果放入缓存
337 jsonObject.put("keywordList", keywordList); 356 jsonObject.put("keywordList", keywordList);
338 searchCacheService.addJSONObjectToCache(indexName, searchParam, jsonObject); 357 searchCacheService.addJSONObjectToCache(indexName, searchParam, jsonObject);
  358 + logger.info("[func=suggestByConversion][query={}][cost={}]", queryWord, System.currentTimeMillis() - /**/begin);
339 return keywordList; 359 return keywordList;
340 } 360 }
341 361