Authored by Gino Zhang

搜索提示词增加拼写检查

... ... @@ -318,7 +318,10 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl
BoolQueryBuilder boolFilter = QueryBuilders.boolQuery();
boolFilter.must(QueryBuilders.termQuery("status", VALID_STATUS));
boolFilter.must(QueryBuilders.rangeQuery(countField).gte(SMART_SUGGESTION_COUNT_LIMIT));
if (!hasChangedKeyword) {
boolFilter.mustNot(QueryBuilders.termQuery("standardKeyword", CharUtils.standardized(queryWord)));
}
searchParam.setFiter(boolFilter);
// 2.4) 增加拼写纠错 为了增加缓存命中率 此次都增加拼写检查处理
... ... @@ -345,24 +348,55 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl
return null;
}
// 5) 加入缓存
suggestResult = new JSONObject();
// 5) 构建结果加入缓存
List<String> correntSpellingSuggestResultTerms = null;
String correnctSpellingKeyword = getCorrectKeywordFromResult(searchResult);
if (StringUtils.isNotEmpty(correnctSpellingKeyword) && !hasChangedKeyword) {
// 6) 执行拼写纠错处理 为了避免无限次递归 只允许拼写检查一次
logger.info("[func=suggestTipsBySuggestIndex]Switch the suggest keyword from [{}] to [{}].", queryWord, correnctSpellingKeyword);
Map<String, String> newParamMap = new HashMap<>(paramMap.size());
newParamMap.putAll(paramMap);
newParamMap.put(SearchRequestParams.PARAM_SEARCH_KEYWORD, correnctSpellingKeyword);
JSONObject correntSpellingSuggestResult = suggestTipsBySuggestIndex(paramMap, true);
correntSpellingSuggestResultTerms = correntSpellingSuggestResult != null ? (List<String>) correntSpellingSuggestResult.get("terms_suggestion") : null;
}
// 7) 构建结果加入缓存 将拼写纠错的词放在第一个
suggestResult = new JSONObject();
List<String> resultTerms = searchResult.getResultList().stream().map(map -> (String) map.get("keyword")).collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(resultTerms) || StringUtils.isEmpty(correnctSpellingKeyword) || hasChangedKeyword) {
if (CollectionUtils.isNotEmpty(correntSpellingSuggestResultTerms)) {
if (CollectionUtils.isEmpty(resultTerms)) {
// 7.1) 如果拼写检查返回的结果不为空而原先的结果为空 则使用拼写检查的
resultTerms.addAll(correntSpellingSuggestResultTerms);
} else if (correntSpellingSuggestResultTerms.contains(correnctSpellingKeyword)) {
// 7.2) 如果拼写检查返回的结果不为空而原先的结果也不为空 则将拼写纠错词放在第一位 这里用contains是确保纠错词的count不会小于20个商品
resultTerms = addCorrectSpellingKeywordToFirst(queryWord, correnctSpellingKeyword, resultTerms);
}
}
suggestResult.put("terms_suggestion", resultTerms);
searchCacheService.addJSONObjectToCache(indexName, searchParam, suggestResult);
logger.info("[func=suggestTipsBySuggestIndex][query={}][cost={}]", queryWord, System.currentTimeMillis() - begin);
return suggestResult;
}
private List<String> addCorrectSpellingKeywordToFirst(String queryWord, String correnctSpellingKeyword, List<String> resultTerms) {
if (StringUtils.isNotEmpty(correnctSpellingKeyword) && !correnctSpellingKeyword.equalsIgnoreCase(queryWord)) {
List<String> newResultTerms = new ArrayList<>(SMART_SUGGESTION_TERM_COUNT);
newResultTerms.add(correnctSpellingKeyword);
for (String item : resultTerms) {
if (newResultTerms.size() == SMART_SUGGESTION_TERM_COUNT) {
break;
}
// 6) 执行拼写纠错处理 为了避免无限次递归 只允许拼写检查一次
logger.info("[func=suggestTipsBySuggestIndex]Switch the suggest keyword from [{}] to [{}].", queryWord, correnctSpellingKeyword);
Map<String, String> newParamMap = new HashMap<>(paramMap.size());
newParamMap.putAll(paramMap);
newParamMap.put(SearchRequestParams.PARAM_SEARCH_KEYWORD, correnctSpellingKeyword);
return suggestTipsBySuggestIndex(paramMap, true);
newResultTerms.add(item);
}
return newResultTerms;
}
return resultTerms;
}
private JSONObject suggestTipsByConversionIndex(Map<String, String> paramMap) {
... ...