|
|
package com.yoho.search.service.service;
|
|
|
|
|
|
import com.google.common.cache.CacheBuilder;
|
|
|
import com.google.common.cache.CacheLoader;
|
|
|
import com.yoho.core.redis.YHRedisTemplate;
|
|
|
import com.yoho.core.redis.YHZSetOperations;
|
|
|
import com.yoho.search.base.utils.DateStyle;
|
|
|
import com.yoho.search.base.utils.DateUtil;
|
|
|
import com.yoho.search.base.utils.ISearchConstants;
|
|
|
import com.yoho.search.base.utils.RedisKeys;
|
|
|
import com.yoho.search.core.es.IElasticsearchClient;
|
|
|
import com.yoho.search.core.es.impl.YohoIndexHelper;
|
|
|
import com.yoho.search.base.utils.RedisKeys;
|
|
|
import com.yoho.search.service.vo.KeyWordWithCount;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse.AnalyzeToken;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
...
|
...
|
@@ -16,10 +19,12 @@ import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.data.redis.core.ZSetOperations;
|
|
|
import org.springframework.data.redis.core.ZSetOperations.TypedTuple;
|
|
|
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;
|
|
|
import java.util.concurrent.Executors;
|
|
|
import java.util.concurrent.TimeUnit;
|
...
|
...
|
@@ -72,8 +77,35 @@ public class SearchKeyWordService { |
|
|
* @param keyWord
|
|
|
* @return
|
|
|
*/
|
|
|
public List<String> getAnalyzeTerms(String keyWord, String analyzer) {
|
|
|
public List<String> getAnalyzeTerms(final String keyWord, final String analyzer, boolean useCache) {
|
|
|
if (!useCache) {
|
|
|
return getAnalyzeTermsDirect(keyWord, analyzer);
|
|
|
}
|
|
|
|
|
|
try {
|
|
|
return CacheBuilder.newBuilder()
|
|
|
.maximumSize(10000)
|
|
|
.expireAfterWrite(10, TimeUnit.MINUTES)
|
|
|
.build(new CacheLoader<String, List<String>>() {
|
|
|
@Override
|
|
|
public List<String> load(String cacheKey) throws Exception {
|
|
|
String[] arrays = cacheKey.split("@", 2);
|
|
|
Assert.isTrue(arrays != null && arrays.length == 2);
|
|
|
return getAnalyzeTermsDirect(arrays[1], arrays[0]);
|
|
|
}
|
|
|
}).get(analyzer + "@" + keyWord);
|
|
|
} catch (ExecutionException e) {
|
|
|
logger.error(keyWord, e);
|
|
|
return new ArrayList<>();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public List<String> getAnalyzeTermsDirect(String keyWord, String analyzer) {
|
|
|
try {
|
|
|
if (StringUtils.isEmpty(keyWord)) {
|
|
|
return new ArrayList<>();
|
|
|
}
|
|
|
|
|
|
List<AnalyzeToken> tokens = getAnalyzeTokens(keyWord, analyzer);
|
|
|
List<String> results = new ArrayList<String>();
|
|
|
for (AnalyzeToken analyzeToken : tokens) {
|
...
|
...
|
@@ -86,7 +118,7 @@ public class SearchKeyWordService { |
|
|
}
|
|
|
}
|
|
|
|
|
|
public void recordSuggestTip(String queryWord){
|
|
|
public void recordSuggestTip(String queryWord) {
|
|
|
recordKeyWord(RedisKeys.YOHO_SEARCH_KEYWORDS_TIPS, queryWord);
|
|
|
}
|
|
|
|
...
|
...
|
@@ -216,12 +248,11 @@ public class SearchKeyWordService { |
|
|
}
|
|
|
}
|
|
|
|
|
|
public String deleteRedisKey(String redisKey){
|
|
|
if(yhNoSyncRedisTemplate.hasKey(redisKey)){
|
|
|
public String deleteRedisKey(String redisKey) {
|
|
|
if (yhNoSyncRedisTemplate.hasKey(redisKey)) {
|
|
|
yhNoSyncRedisTemplate.delete(redisKey);
|
|
|
return "The key has been deleted succede!";
|
|
|
}
|
|
|
else{
|
|
|
} else {
|
|
|
return "The key doesn't exist.";
|
|
|
}
|
|
|
}
|
...
|
...
|
|