Authored by hugufei

添加召回时的缓存

... ... @@ -185,5 +185,16 @@ public class SearchCacheFactory {
int cacheInMinute = 60;
return this.getOrCreateSearchCache("HOT_SALE_RANK", cacheType, cacheInMinute);
}
/**
* 召回的缓存
*
* @return
*/
public SearchCache getRecallCache() {
CacheType cacheType = CacheType.SEARCH_REDIS;
int cacheInMinute = 10;
return this.getOrCreateSearchCache("RECALL", cacheType, cacheInMinute);
}
}
... ...
... ... @@ -5,6 +5,8 @@ import java.util.Arrays;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
... ... @@ -17,8 +19,12 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.base.utils.ProductIndexEsField;
import com.yoho.search.common.cache.SearchCacheFactory;
import com.yoho.search.common.cache.model.SearchCache;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.core.es.model.SearchResult;
import com.yoho.search.service.base.SearchCacheService;
... ... @@ -41,6 +47,23 @@ public abstract class AbstractRecallService {
private SearchServiceHelper searchServiceHelper;
@Autowired
private SearchCacheService searchCacheService;
@Autowired
private SearchCacheFactory searchCacheFactory;
private SearchCache searchCache;
@PostConstruct
void init() {
searchCache = searchCacheFactory.getRecallCache();
}
private List<SearchParam> getSearchpaParams(List<RecallSearchParam> recallSearchParams) {
List<SearchParam> results = new ArrayList<SearchParam>();
for (RecallSearchParam recallSearchParam : recallSearchParams) {
results.add(recallSearchParam.getSearchParam());
}
return results;
}
/**
* 0、召回skns以及总数
... ... @@ -59,36 +82,47 @@ public abstract class AbstractRecallService {
List<RecallSearchParam> recallSearchParams = this.getRecallSearchParams(paramMap, recallStrategys);
logger.info("[func=getRecallSearchParams][cost={}ms]", System.currentTimeMillis() - begin);
// 3、执行查询
// 3、缓存中获取
List<SearchParam> searchParams = this.getSearchpaParams(recallSearchParams);
JSONObject jsonObject = searchCacheService.getJSONObjectFromCache(searchCache, ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParams);
if (jsonObject != null) {
return JSON.toJavaObject(jsonObject, RecallProductSknList.class);
}
// 4、查询
begin = System.currentTimeMillis();
RecallResult recallResult = this.queryRecallResult(recallSearchParams);
logger.info("[func=queryRecallResult][cost={}ms]", System.currentTimeMillis() - begin);
// 4、从兜底策略中获取总数
// 5、从兜底策略中获取总数
long total = this.getTotalFromRecallResult(recallResult);
// 5、粗排
// 6、粗排
begin = System.currentTimeMillis();
recallResult = this.doSketchyRank(paramMap, recallResult);
logger.info("[func=doSketchyRank][cost={}ms]", System.currentTimeMillis() - begin);
// 6、精排
// 7、精排
begin = System.currentTimeMillis();
recallResult = this.doCarefulRank(paramMap, recallResult);
logger.info("[func=doCarefulRank][cost={}ms]", System.currentTimeMillis() - begin);
// 7、重排
// 8、重排
begin = System.currentTimeMillis();
recallResult = this.doReRank(paramMap, recallResult);
logger.info("[func=doReRank][cost={}ms]", System.currentTimeMillis() - begin);
// 8、构造返回结果
// 9、构造返回结果
begin = System.currentTimeMillis();
List<Integer> productSknList = new ArrayList<Integer>();
for (Map<String, Object> product : recallResult.getProductList()) {
productSknList.add(MapUtils.getInteger(product, ProductIndexEsField.productSkn));
}
return new RecallProductSknList(total, productSknList);
RecallProductSknList recallProductSknList = new RecallProductSknList(total, productSknList);
// 10、加入缓存
searchCacheService.addJSONObjectToCache(searchCache, ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParams, (JSONObject) JSON.toJSON(recallProductSknList));
return recallProductSknList;
}
/**
... ...
package com.yoho.search.service.scene.recall.model;
import java.io.Serializable;
import java.util.List;
public class RecallProductSknList {
public class RecallProductSknList implements Serializable {
private static final long serialVersionUID = -7892222806575478562L;
private long total;
private List<Integer> productSknList;
public RecallProductSknList() {
super();
}
public RecallProductSknList(long total, List<Integer> productSknList) {
super();
this.total = total;
... ... @@ -20,4 +27,12 @@ public class RecallProductSknList {
return productSknList;
}
public void setTotal(long total) {
this.total = total;
}
public void setProductSknList(List<Integer> productSknList) {
this.productSknList = productSknList;
}
}
... ...