Authored by Gino Zhang

搜索时支持从conversion搜索提取推荐词

... ... @@ -247,11 +247,27 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl
return null;
}
// 5) 将suggest分为两类:一类是从query分词出来的单个term 一类是从suggest获取的优先级较高的keyword
List<String> resultTerms = new ArrayList<>();
searchResult.getResultList().forEach(map -> {
resultTerms.add((String) map.get("keyword"));
});
final List<String> resultTerms = new ArrayList<>();
if (CollectionUtils.isNotEmpty(searchResult.getResultList())) {
// 5.1) 从suggest获取的keyword列表
searchResult.getResultList().forEach(map -> {
resultTerms.add((String) map.get("keyword"));
});
} else {
// 5.2) 从conversion获取keyword列表
String dest = getTermsBySuggestConversion(queryWord);
if (StringUtils.isNotEmpty(dest)) {
int count = 0;
for (String keyword : dest.split(",")) {
count++;
resultTerms.add(keyword);
// 最多返回五个推荐词
if (count == 5) {
break;
}
}
}
}
// 6) 加入缓存
suggestResult = new JSONObject();
... ... @@ -265,6 +281,45 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl
}
}
private String getTermsBySuggestConversion(String queryWord) {
long begin = System.currentTimeMillis();
logger.info("[func=getTermsBySuggestConversion][query={}][begin={}]", queryWord, begin);
// 2) 根据terms搜索构造搜索请求
SearchParam searchParam = new SearchParam();
MatchQueryBuilder queryBuilder = QueryBuilders.matchQuery("source", queryWord);
searchParam.setQuery(queryBuilder);
searchParam.setPage(1);
searchParam.setSize(1);
// 3) 先从缓存中获取
final String indexName = ISearchConstants.INDEX_NAME_CONVERSION;
JSONObject suggestResult = searchCacheService.getJSONObjectFromCache(indexName, searchParam);
if (suggestResult != null) {
CACHE_MATCH_REQUEST.info("match cache for product list terms conversion, keyword = {}.", queryWord);
return suggestResult.getString("dest");
}
// 4) 调用ES执行搜索
SearchResult searchResult = searchCommonService.doSearch(indexName, searchParam);
if (searchResult == null) {
return null;
}
String dest = "";
if (CollectionUtils.isNotEmpty(searchResult.getResultList())) {
// 5) 从conversion获取的keyword列表
dest = (String) searchResult.getResultList().get(0).get("dest");
}
// 6) 加入缓存
suggestResult = new JSONObject();
suggestResult.put("dest", dest);
searchCacheService.addJSONObjectToCache(indexName, searchParam, suggestResult);
logger.info("[func=getTermsBySuggestConversion][query={}][cost={}]", queryWord, System.currentTimeMillis() - begin);
return dest;
}
private SearchParam buildSearchParamForTerms(Map<String, String> paramMap) {
final String countField = getCountField(paramMap);
SearchParam searchParam = new SearchParam();
... ...