Authored by Gino Zhang

搜索提示词增加拼写检查

@@ -318,7 +318,10 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl @@ -318,7 +318,10 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl
318 BoolQueryBuilder boolFilter = QueryBuilders.boolQuery(); 318 BoolQueryBuilder boolFilter = QueryBuilders.boolQuery();
319 boolFilter.must(QueryBuilders.termQuery("status", VALID_STATUS)); 319 boolFilter.must(QueryBuilders.termQuery("status", VALID_STATUS));
320 boolFilter.must(QueryBuilders.rangeQuery(countField).gte(SMART_SUGGESTION_COUNT_LIMIT)); 320 boolFilter.must(QueryBuilders.rangeQuery(countField).gte(SMART_SUGGESTION_COUNT_LIMIT));
  321 + if (!hasChangedKeyword) {
321 boolFilter.mustNot(QueryBuilders.termQuery("standardKeyword", CharUtils.standardized(queryWord))); 322 boolFilter.mustNot(QueryBuilders.termQuery("standardKeyword", CharUtils.standardized(queryWord)));
  323 + }
  324 +
322 searchParam.setFiter(boolFilter); 325 searchParam.setFiter(boolFilter);
323 326
324 // 2.4) 增加拼写纠错 为了增加缓存命中率 此次都增加拼写检查处理 327 // 2.4) 增加拼写纠错 为了增加缓存命中率 此次都增加拼写检查处理
@@ -345,24 +348,55 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl @@ -345,24 +348,55 @@ public class SuggestServiceImpl implements ISuggestService, ApplicationEventPubl
345 return null; 348 return null;
346 } 349 }
347 350
348 - // 5) 加入缓存  
349 - suggestResult = new JSONObject(); 351 + // 5) 构建结果加入缓存
  352 + List<String> correntSpellingSuggestResultTerms = null;
350 String correnctSpellingKeyword = getCorrectKeywordFromResult(searchResult); 353 String correnctSpellingKeyword = getCorrectKeywordFromResult(searchResult);
  354 + if (StringUtils.isNotEmpty(correnctSpellingKeyword) && !hasChangedKeyword) {
  355 + // 6) 执行拼写纠错处理 为了避免无限次递归 只允许拼写检查一次
  356 + logger.info("[func=suggestTipsBySuggestIndex]Switch the suggest keyword from [{}] to [{}].", queryWord, correnctSpellingKeyword);
  357 + Map<String, String> newParamMap = new HashMap<>(paramMap.size());
  358 + newParamMap.putAll(paramMap);
  359 + newParamMap.put(SearchRequestParams.PARAM_SEARCH_KEYWORD, correnctSpellingKeyword);
  360 + JSONObject correntSpellingSuggestResult = suggestTipsBySuggestIndex(paramMap, true);
  361 + correntSpellingSuggestResultTerms = correntSpellingSuggestResult != null ? (List<String>) correntSpellingSuggestResult.get("terms_suggestion") : null;
  362 + }
  363 +
  364 + // 7) 构建结果加入缓存 将拼写纠错的词放在第一个
  365 + suggestResult = new JSONObject();
351 List<String> resultTerms = searchResult.getResultList().stream().map(map -> (String) map.get("keyword")).collect(Collectors.toList()); 366 List<String> resultTerms = searchResult.getResultList().stream().map(map -> (String) map.get("keyword")).collect(Collectors.toList());
352 - if (CollectionUtils.isNotEmpty(resultTerms) || StringUtils.isEmpty(correnctSpellingKeyword) || hasChangedKeyword) { 367 + if (CollectionUtils.isNotEmpty(correntSpellingSuggestResultTerms)) {
  368 + if (CollectionUtils.isEmpty(resultTerms)) {
  369 + // 7.1) 如果拼写检查返回的结果不为空而原先的结果为空 则使用拼写检查的
  370 + resultTerms.addAll(correntSpellingSuggestResultTerms);
  371 + } else if (correntSpellingSuggestResultTerms.contains(correnctSpellingKeyword)) {
  372 + // 7.2) 如果拼写检查返回的结果不为空而原先的结果也不为空 则将拼写纠错词放在第一位 这里用contains是确保纠错词的count不会小于20个商品
  373 + resultTerms = addCorrectSpellingKeywordToFirst(queryWord, correnctSpellingKeyword, resultTerms);
  374 + }
  375 + }
  376 +
353 suggestResult.put("terms_suggestion", resultTerms); 377 suggestResult.put("terms_suggestion", resultTerms);
354 searchCacheService.addJSONObjectToCache(indexName, searchParam, suggestResult); 378 searchCacheService.addJSONObjectToCache(indexName, searchParam, suggestResult);
355 logger.info("[func=suggestTipsBySuggestIndex][query={}][cost={}]", queryWord, System.currentTimeMillis() - begin); 379 logger.info("[func=suggestTipsBySuggestIndex][query={}][cost={}]", queryWord, System.currentTimeMillis() - begin);
356 return suggestResult; 380 return suggestResult;
  381 +
357 } 382 }
358 383
  384 + private List<String> addCorrectSpellingKeywordToFirst(String queryWord, String correnctSpellingKeyword, List<String> resultTerms) {
  385 + if (StringUtils.isNotEmpty(correnctSpellingKeyword) && !correnctSpellingKeyword.equalsIgnoreCase(queryWord)) {
  386 + List<String> newResultTerms = new ArrayList<>(SMART_SUGGESTION_TERM_COUNT);
  387 + newResultTerms.add(correnctSpellingKeyword);
  388 + for (String item : resultTerms) {
  389 + if (newResultTerms.size() == SMART_SUGGESTION_TERM_COUNT) {
  390 + break;
  391 + }
359 392
360 - // 6) 执行拼写纠错处理 为了避免无限次递归 只允许拼写检查一次  
361 - logger.info("[func=suggestTipsBySuggestIndex]Switch the suggest keyword from [{}] to [{}].", queryWord, correnctSpellingKeyword);  
362 - Map<String, String> newParamMap = new HashMap<>(paramMap.size());  
363 - newParamMap.putAll(paramMap);  
364 - newParamMap.put(SearchRequestParams.PARAM_SEARCH_KEYWORD, correnctSpellingKeyword);  
365 - return suggestTipsBySuggestIndex(paramMap, true); 393 + newResultTerms.add(item);
  394 + }
  395 +
  396 + return newResultTerms;
  397 + }
  398 +
  399 + return resultTerms;
366 } 400 }
367 401
368 private JSONObject suggestTipsByConversionIndex(Map<String, String> paramMap) { 402 private JSONObject suggestTipsByConversionIndex(Map<String, String> paramMap) {