...
|
...
|
@@ -22,7 +22,6 @@ import org.springframework.stereotype.Service; |
|
|
import org.springframework.util.Assert;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.*;
|
|
|
import java.util.concurrent.ExecutionException;
|
|
|
import java.util.concurrent.ExecutorService;
|
...
|
...
|
@@ -40,8 +39,6 @@ public class SearchKeyWordService { |
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(SearchKeyWordService.class);
|
|
|
|
|
|
private static final Logger EMPTY_RESULT = LoggerFactory.getLogger("EMPTY_RESULT");
|
|
|
|
|
|
@Resource(name = "yhNoSyncZSetOperations")
|
|
|
private YHZSetOperations<String, String> yhNoSyncZSetOperations;
|
|
|
|
...
|
...
|
@@ -118,10 +115,6 @@ public class SearchKeyWordService { |
|
|
}
|
|
|
}
|
|
|
|
|
|
public void recordSuggestTip(String queryWord) {
|
|
|
recordKeyWord(RedisKeys.YOHO_SEARCH_KEYWORDS_TIPS, queryWord);
|
|
|
}
|
|
|
|
|
|
// 异步的做法是防止redis报错影响搜索主流程
|
|
|
private void recordKeyWord(String redisKeyTemplate, String queryWord) {
|
|
|
service.submit(new Runnable() {
|
...
|
...
|
@@ -183,79 +176,61 @@ public class SearchKeyWordService { |
|
|
}
|
|
|
}
|
|
|
|
|
|
public Double getKeywordCount(String redisKeyTemplate, String queryWord){
|
|
|
public void recordSuggestRecom(String queryWord) {
|
|
|
recordKeyWord(RedisKeys.YOHO_SEARCH_KEYWORDS_TIPS, queryWord);
|
|
|
}
|
|
|
|
|
|
public Double getKeywordCount(String redisKeyTemplate, String queryWord) {
|
|
|
try {
|
|
|
return yhNoSyncZSetOperations.score(RedisKeys.getRedisKey4Yesterday(redisKeyTemplate), queryWord);
|
|
|
}catch (Exception e){
|
|
|
} catch (Exception e) {
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 获取【热搜】toplist
|
|
|
public Map<String, Object> getHotkeyWords(int limit, boolean isReturnTodayRecords) {
|
|
|
return this.getListByScoreDesc(RedisKeys.YOHO_SEARCH_KEYWORDS_HOT, limit, isReturnTodayRecords);
|
|
|
public Map<String, Object> getHotkeyWords(int limit, String dateStr) {
|
|
|
return this.getListByScoreDesc(RedisKeys.YOHO_SEARCH_KEYWORDS_HOT, limit, dateStr);
|
|
|
}
|
|
|
|
|
|
// 获取空结果的toplist
|
|
|
public Map<String, Object> getEmptyKeyWords(int limit, boolean isReturnTodayRecords) {
|
|
|
return this.getListByScoreDesc(RedisKeys.YOHO_SEARCH_KEYWORDS_EMPTY, limit, isReturnTodayRecords);
|
|
|
public Map<String, Object> getEmptyKeyWords(int limit, String dateStr) {
|
|
|
return this.getListByScoreDesc(RedisKeys.YOHO_SEARCH_KEYWORDS_EMPTY, limit, dateStr);
|
|
|
}
|
|
|
|
|
|
// 获取只有一页结果的toplist
|
|
|
public Map<String, Object> getLessKeyWords(int limit, boolean isReturnTodayRecords) {
|
|
|
return this.getListByScoreDesc(RedisKeys.YOHO_SEARCH_KEYWORDS_LESS, limit, isReturnTodayRecords);
|
|
|
public Map<String, Object> getLessKeyWords(int limit, String dateStr) {
|
|
|
return this.getListByScoreDesc(RedisKeys.YOHO_SEARCH_KEYWORDS_LESS, limit, dateStr);
|
|
|
}
|
|
|
|
|
|
// 获取需要搜索推荐的关键词
|
|
|
public Map<String, Object> getNeedRecomKeyWords(int limit, String dateStr) {
|
|
|
return this.getListByScoreDesc(RedisKeys.YOHO_SEARCH_KEYWORDS_TIPS, limit, dateStr);
|
|
|
}
|
|
|
|
|
|
private Map<String, Object> getListByScoreDesc(String redisKeyTemplate, int limit, boolean isReturnTodayRecords) {
|
|
|
private Map<String, Object> getListByScoreDesc(String redisKeyTemplate, int limit, String dateStr) {
|
|
|
Map<String, Object> resultMap = new HashMap<>(3);
|
|
|
resultMap.put("dateForRedisKey", dateForRedisKey);
|
|
|
if (this.dateForRedisKey == null) {
|
|
|
return resultMap;
|
|
|
|
|
|
String date = dateStr;
|
|
|
if (StringUtils.isEmpty(date)) {
|
|
|
date = this.dateForRedisKey;
|
|
|
}
|
|
|
if (StringUtils.isEmpty(date)) {
|
|
|
date = DateUtil.DateToString(new Date(), DateStyle.YYYYMMDD);
|
|
|
}
|
|
|
|
|
|
String redisKey = RedisKeys.getRedisKey4Yesterday(redisKeyTemplate);
|
|
|
String redisKey = String.format(redisKeyTemplate, date);
|
|
|
Set<ZSetOperations.TypedTuple<String>> redisResults = yhNoSyncZSetOperations.reverseRangeWithScores(redisKey, 0, limit);
|
|
|
List<KeyWordWithCount> results = new ArrayList<KeyWordWithCount>();
|
|
|
for (TypedTuple<String> typedTuple : redisResults) {
|
|
|
results.add(new KeyWordWithCount(typedTuple.getValue(), (int) typedTuple.getScore().doubleValue()));
|
|
|
}
|
|
|
|
|
|
if (isReturnTodayRecords) {
|
|
|
// 也返回今天的数据
|
|
|
String redisKey4Today = RedisKeys.getRedisKey4Today(redisKeyTemplate);
|
|
|
Set<ZSetOperations.TypedTuple<String>> redisResults4Today = yhNoSyncZSetOperations.reverseRangeWithScores(redisKey4Today, 0, limit);
|
|
|
List<KeyWordWithCount> results4Today = new ArrayList<KeyWordWithCount>();
|
|
|
for (TypedTuple<String> typedTuple : redisResults4Today) {
|
|
|
results4Today.add(new KeyWordWithCount(typedTuple.getValue(), (int) typedTuple.getScore().doubleValue()));
|
|
|
}
|
|
|
|
|
|
resultMap.put("0", results4Today);
|
|
|
}
|
|
|
|
|
|
resultMap.put("redisKey", redisKey);
|
|
|
resultMap.put("-1", results);
|
|
|
resultMap.put("keywords", results);
|
|
|
return resultMap;
|
|
|
}
|
|
|
|
|
|
public void handleEmptyRecords(Map<String, String> paramMap) {
|
|
|
StringBuilder paramStringBuilder = new StringBuilder();
|
|
|
for (Map.Entry<String, String> entry : paramMap.entrySet()) {
|
|
|
paramStringBuilder.append("&").append(entry.getKey()).append("=").append(entry.getValue());
|
|
|
}
|
|
|
String paramString = paramStringBuilder.toString().replaceFirst("&", "");
|
|
|
EMPTY_RESULT.info("empty records for search.json: time[{}], param [{}]", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis()), paramString);
|
|
|
|
|
|
// 商品池或品类列表 为空,则告警
|
|
|
if (paramMap.get("filter_poolId") != null) {// ||
|
|
|
// paramMap.get("msort")!=null
|
|
|
// ||
|
|
|
// paramMap.get("misort")!=null
|
|
|
// ||
|
|
|
// paramMap.get("sort")!=null
|
|
|
// /TODO
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public String deleteRedisKey(String redisKey) {
|
|
|
if (yhNoSyncRedisTemplate.hasKey(redisKey)) {
|
|
|
yhNoSyncRedisTemplate.delete(redisKey);
|
...
|
...
|
|