Authored by Gino Zhang

suggestByConversion增加日志和注释

... ... @@ -288,25 +288,39 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl
}
private List<String> suggestByConversion(Map<String, String> paramMap) {
String queryWord = paramMap.get(SearchRequestParams.PARAM_SEARCH_KEYWORD).toLowerCase();
long begin = System.currentTimeMillis();
logger.info("[func=suggestByConversion][query={}][begin={}]", queryWord, begin);
// 1) 判断是否支持从conversion获取推荐词
if (!searchDynamicConfigService.isSearchSuggestionFromConversionOpen()) {
return new ArrayList<>();
}
String queryWord = paramMap.get(SearchRequestParams.PARAM_SEARCH_KEYWORD).toLowerCase();
// 2) 异步上报query关键词 用于统计和分析
searchKeyWordService.recordSuggestTip(queryWord);
// 3) 从conversion索引中获取转换后的关键词列表
String dest = getSuggestConversionDestBySource(queryWord);
if (StringUtils.isEmpty(dest)) {
return new ArrayList<>();
}
// 4) 查询suggest索引 过滤和排序
// 4.1) 根据查询条件获得不同的count字段
final String countFiled = getCountField(paramMap);
SearchParam searchParam = new SearchParam();
List<String> keywordsInDest = Arrays.stream(dest.split(",")).map(String::toLowerCase).map(String::trim).collect(Collectors.toList());
// 4.2) 设置keyword列表
QueryBuilder queryBuilder = QueryBuilders.termsQuery("keyword.keyword_lowercase", keywordsInDest);
FunctionScoreQueryBuilder functionScoreQueryBuilder = new FunctionScoreQueryBuilder(queryBuilder);
functionScoreQueryBuilder.add(QueryBuilders.termQuery("type", 2), ScoreFunctionBuilders.weightFactorFunction(keywordsInDest.size()));
// 4.3) 确保品类的关键词排在前面
functionScoreQueryBuilder.add(QueryBuilders.termQuery("type", 2), ScoreFunctionBuilders.weightFactorFunction(keywordsInDest.size() + 1));
// 4.4) 根据顺序从高到低加分
for (int index = 0; index < keywordsInDest.size(); index++) {
// 根据顺序从高到低加分
functionScoreQueryBuilder.add(QueryBuilders.termQuery("keyword.keyword_lowercase", keywordsInDest.get(index)),
ScoreFunctionBuilders.weightFactorFunction(keywordsInDest.size() - index));
}
... ... @@ -314,8 +328,11 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl
searchParam.setQuery(functionScoreQueryBuilder);
searchParam.setPage(1);
searchParam.setSize(SMART_SUGGESTION_TERM_COUNT);
// 4.5) 过滤关联的商品数量小于20的关键词
searchParam.setFiter(QueryBuilders.rangeQuery(countFiled).gte(20));
// 5) 先从缓存中获取
final String indexName = ISearchConstants.INDEX_NAME_SUGGEST;
JSONObject jsonObject = searchCacheService.getJSONObjectFromCache(indexName, searchParam);
if (jsonObject != null) {
... ... @@ -323,6 +340,7 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl
return (List<String>) jsonObject.get("keywordList");
}
// 6) 调用ES获取结果
SearchResult searchResult = searchCommonService.doSearch(indexName, searchParam);
if (searchResult == null) {
return new ArrayList<>();
... ... @@ -334,8 +352,10 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl
keywordList.add((String) record.get("keyword"));
}
// 7) 将结果放入缓存
jsonObject.put("keywordList", keywordList);
searchCacheService.addJSONObjectToCache(indexName, searchParam, jsonObject);
logger.info("[func=suggestByConversion][query={}][cost={}]", queryWord, System.currentTimeMillis() - /**/begin);
return keywordList;
}
... ...