|
|
package com.yoho.search.recall.scene.beans.cache;
|
|
|
|
|
|
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
|
|
|
import com.yoho.search.base.utils.ISearchConstants;
|
|
|
import com.yoho.search.base.utils.ProductIndexEsField;
|
|
|
import com.yoho.search.common.cache.impls.EhCache;
|
|
|
import com.yoho.search.common.cache.impls.SearchRedis;
|
|
|
import com.yoho.search.common.cache.model.CacheObject;
|
|
|
import com.yoho.search.core.es.model.SearchParam;
|
|
|
import com.yoho.search.core.es.model.SearchResult;
|
|
|
import com.yoho.search.recall.scene.beans.persional.PageProductIdBitSetComponent;
|
|
|
import com.yoho.search.recall.scene.beans.strategy.impls.RecommendSknStrategy;
|
|
|
import com.yoho.search.recall.scene.models.common.ParamQueryFilter;
|
|
|
import com.yoho.search.recall.scene.models.personal.PageProductIdBitSet;
|
|
|
import com.yoho.search.recall.scene.models.req.RecallRequest;
|
|
|
import com.yoho.search.recall.scene.models.req.RecallRequestResponse;
|
|
|
import com.yoho.search.recall.scene.models.req.RecallResponse;
|
...
|
...
|
@@ -15,13 +20,17 @@ import com.yoho.search.service.base.SearchCommonService; |
|
|
import org.apache.commons.collections.MapUtils;
|
|
|
import org.elasticsearch.index.query.BoolQueryBuilder;
|
|
|
import org.elasticsearch.index.query.QueryBuilders;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
@Component
|
|
|
public class SknRecallCacheBean{
|
|
|
public class SknRecallCacheBean {
|
|
|
|
|
|
private static final Logger RECALL_NEW_LOGGER = LoggerFactory.getLogger("RECALL");
|
|
|
|
|
|
@Autowired
|
|
|
private SearchCommonService searchCommonService;
|
...
|
...
|
@@ -29,22 +38,132 @@ public class SknRecallCacheBean{ |
|
|
private PageProductIdBitSetComponent pageProductIdBitSetComponent;
|
|
|
@Autowired
|
|
|
private SknBaseInfoCacheBean sknBaseInfoCacheBean;
|
|
|
@Autowired
|
|
|
private SearchRedis searchRedis;
|
|
|
@Autowired
|
|
|
private EhCache ehCache;
|
|
|
|
|
|
private static final Boolean recallWithCache = true;
|
|
|
|
|
|
/**
|
|
|
* 将推荐的skn构造成召回对象
|
|
|
*
|
|
|
* @return
|
|
|
*/
|
|
|
public List<RecallRequestResponse> batchRecallRecommedSknList(UserRecallRequest userRecallRequest,List<Integer> recommedSknList,int maxReturnCount){
|
|
|
if(recommedSknList==null || recommedSknList.isEmpty()){
|
|
|
public List<RecallRequestResponse> batchRecallRecommedSknList(UserRecallRequest userRecallRequest, List<Integer> recommedSknList, int maxReturnCount) {
|
|
|
try {
|
|
|
if (recommedSknList == null || recommedSknList.isEmpty()) {
|
|
|
return new ArrayList<>();
|
|
|
//localhost:8080/yohosearch/productindex/productList.json?uid=11145&status=1&gender=1,2,3,4,5
|
|
|
//recommedSknList = Arrays.asList(50020667,50026235,50026280,50024584,50026611,50028826,50031356,50031387,50029959,51001108);
|
|
|
}
|
|
|
List<Integer> filterSknList;
|
|
|
if (recallWithCache) {
|
|
|
filterSknList = this.filterRecommendWithCache(userRecallRequest, recommedSknList);
|
|
|
} else {
|
|
|
filterSknList = this.filterRecommedSknListByEs(userRecallRequest, recommedSknList);
|
|
|
}
|
|
|
return this.buildResults(userRecallRequest, filterSknList, maxReturnCount);
|
|
|
}catch (Exception e){
|
|
|
RECALL_NEW_LOGGER.error(e.getMessage(),e);
|
|
|
return new ArrayList<>();
|
|
|
}
|
|
|
List<Integer> filterSknList = this.filterRecommedSknList(userRecallRequest,recommedSknList,maxReturnCount);
|
|
|
return this.buildResults(userRecallRequest,filterSknList);
|
|
|
}
|
|
|
|
|
|
private List<Integer> filterRecommedSknList(UserRecallRequest userRecallRequest, List<Integer> recommedSknList,int maxReturnCount){
|
|
|
if(recommedSknList==null || recommedSknList.isEmpty()){
|
|
|
/**
|
|
|
* 从ehcahc或者redis中构造返回结果
|
|
|
* @param userRecallRequest
|
|
|
* @param recommedSknList
|
|
|
* @return
|
|
|
*/
|
|
|
private List<Integer> filterRecommendWithCache(UserRecallRequest userRecallRequest, List<Integer> recommedSknList) {
|
|
|
//1、skn转成productId
|
|
|
Map<Integer, Integer> productSknToIdMap = sknBaseInfoCacheBean.queryProductSknToProductIdMap(recommedSknList);
|
|
|
if(productSknToIdMap==null || productSknToIdMap.isEmpty()){
|
|
|
return new ArrayList<>();
|
|
|
}
|
|
|
List<Integer> recommedProductIds = new ArrayList<>();
|
|
|
recommedProductIds.addAll(productSknToIdMap.values());
|
|
|
|
|
|
//2、先从本地缓存获取
|
|
|
ParamQueryFilter paramQueryFilter = userRecallRequest.getParamQueryFilter();
|
|
|
RedisKeyBuilder redisKeyBuilder = pageProductIdBitSetComponent.genRedisKeyBuilder(paramQueryFilter);
|
|
|
Map<Integer, Boolean> ehcacheQueryResult = ehCache.getFromBitSet(redisKeyBuilder,recommedProductIds);
|
|
|
if (ehcacheQueryResult != null) {
|
|
|
return this.filterSknByCacheQueryResult(recommedSknList,productSknToIdMap, ehcacheQueryResult);
|
|
|
}
|
|
|
|
|
|
//3、再从redis的bitset缓存结构中过滤
|
|
|
Map<Integer, Boolean> redisQueryResult = searchRedis.getFromBitSet(redisKeyBuilder, recommedProductIds);
|
|
|
if (redisQueryResult != null) {
|
|
|
//TODO 回写本地缓存
|
|
|
return this.filterSknByCacheQueryResult(recommedSknList, productSknToIdMap,redisQueryResult);
|
|
|
}
|
|
|
|
|
|
//4、执行查询,添加到缓存
|
|
|
PageProductIdBitSet pageProductIdBitSet = pageProductIdBitSetComponent.queryPageProductIdBitSet(userRecallRequest.getParamQueryFilter());
|
|
|
//3.1)添加到本地缓存
|
|
|
this.ehCache.setBitSet(redisKeyBuilder,pageProductIdBitSet.getProductIdBitSet(),pageProductIdBitSetComponent.cacheTimeInMinute());
|
|
|
//3.2)添加到redis缓存
|
|
|
//TODO 异步加入redis缓存
|
|
|
this.searchRedis.setBitSet(redisKeyBuilder,pageProductIdBitSet.getProductIdBitSet(),pageProductIdBitSetComponent.cacheTimeInMinute());
|
|
|
return this.filterSknByPageProductIdBitSet(recommedSknList,productSknToIdMap, pageProductIdBitSet.getProductIdBitSet());
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 从bitset对象中构造返回对象
|
|
|
*
|
|
|
* @param recommedSknList
|
|
|
* @param bitSet
|
|
|
* @return
|
|
|
*/
|
|
|
private List<Integer> filterSknByPageProductIdBitSet(List<Integer> recommedSknList,Map<Integer, Integer> productSknToIdMap, BitSet bitSet) {
|
|
|
List<Integer> existProductSkns = new ArrayList<>();
|
|
|
for (Integer recommedSkn : recommedSknList) {
|
|
|
Integer productId = productSknToIdMap.get(recommedSkn);
|
|
|
if(productId==null){
|
|
|
continue;
|
|
|
}
|
|
|
if (!bitSet.get(productId)) {
|
|
|
continue;
|
|
|
}
|
|
|
existProductSkns.add(recommedSkn);
|
|
|
}
|
|
|
return existProductSkns;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 根据redis返回结果按顺序构结果,只返回为true的
|
|
|
*
|
|
|
* @param recommedSknList
|
|
|
* @param cacheQueryResult
|
|
|
* @return
|
|
|
*/
|
|
|
private List<Integer> filterSknByCacheQueryResult(List<Integer> recommedSknList,Map<Integer, Integer> productSknToIdMap, Map<Integer, Boolean> cacheQueryResult) {
|
|
|
List<Integer> existProductSkns = new ArrayList<>();
|
|
|
for (Integer recommedSkn : recommedSknList) {
|
|
|
Integer productId = productSknToIdMap.get(recommedSkn);
|
|
|
if(productId==null){
|
|
|
continue;
|
|
|
}
|
|
|
if (!cacheQueryResult.getOrDefault(productId, false)) {
|
|
|
continue;
|
|
|
}
|
|
|
existProductSkns.add(recommedSkn);
|
|
|
}
|
|
|
return existProductSkns;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 直接从es召回
|
|
|
*
|
|
|
* @param userRecallRequest
|
|
|
* @param recommedSknList
|
|
|
* @return
|
|
|
*/
|
|
|
private List<Integer> filterRecommedSknListByEs(UserRecallRequest userRecallRequest, List<Integer> recommedSknList) {
|
|
|
if (recommedSknList == null || recommedSknList.isEmpty()) {
|
|
|
return new ArrayList<>();
|
|
|
}
|
|
|
//1、构造searchParam
|
...
|
...
|
@@ -53,7 +172,7 @@ public class SknRecallCacheBean{ |
|
|
searchParam.setQuery(paramQueryFilter.getParamQuery());
|
|
|
BoolQueryBuilder realFilter = QueryBuilders.boolQuery();
|
|
|
realFilter.must(paramQueryFilter.getParamFilter());
|
|
|
realFilter.must(QueryBuilders.termsQuery(ProductIndexEsField.productSkn,recommedSknList));
|
|
|
realFilter.must(QueryBuilders.termsQuery(ProductIndexEsField.productSkn, recommedSknList));
|
|
|
searchParam.setFiter(realFilter);
|
|
|
|
|
|
//2、设置分页参数
|
...
|
...
|
@@ -74,11 +193,8 @@ public class SknRecallCacheBean{ |
|
|
|
|
|
//5、按顺序构造最终结果
|
|
|
List<Integer> results = new ArrayList<>();
|
|
|
for (Integer recommedSkn: recommedSknList) {
|
|
|
if(results.size()>=maxReturnCount){
|
|
|
continue;
|
|
|
}
|
|
|
if(existsProductSkns.contains(recommedSkn)){
|
|
|
for (Integer recommedSkn : recommedSknList) {
|
|
|
if (existsProductSkns.contains(recommedSkn)) {
|
|
|
results.add(recommedSkn);
|
|
|
}
|
|
|
}
|
...
|
...
|
@@ -87,18 +203,22 @@ public class SknRecallCacheBean{ |
|
|
|
|
|
/**
|
|
|
* 将推荐的skn构造成召回对象
|
|
|
*
|
|
|
* @return
|
|
|
*/
|
|
|
private List<RecallRequestResponse> buildResults(UserRecallRequest userRecallRequest,final List<Integer> recommedSknList){
|
|
|
private List<RecallRequestResponse> buildResults(UserRecallRequest userRecallRequest, final List<Integer> recommedSknList, int maxReturnCount) {
|
|
|
List<RecallRequestResponse> results = new ArrayList<>();
|
|
|
if(recommedSknList==null || recommedSknList.isEmpty()){
|
|
|
if (recommedSknList == null || recommedSknList.isEmpty()) {
|
|
|
return results;
|
|
|
}
|
|
|
ParamQueryFilter paramQueryFilter = userRecallRequest.getParamQueryFilter();
|
|
|
for (Integer recommendSkn : recommedSknList){
|
|
|
RecallRequest recallRequest = new RecallRequest(paramQueryFilter,new RecommendSknStrategy(recommendSkn));
|
|
|
for (Integer recommendSkn : recommedSknList) {
|
|
|
if (results.size() >= maxReturnCount) {
|
|
|
break;
|
|
|
}
|
|
|
RecallRequest recallRequest = new RecallRequest(paramQueryFilter, new RecommendSknStrategy(recommendSkn));
|
|
|
RecallRequestResponse recallRequestResponse = new RecallRequestResponse(recallRequest);
|
|
|
recallRequestResponse.setResponse(new RecallResponse(1L, Arrays.asList(recommendSkn)),false);
|
|
|
recallRequestResponse.setResponse(new RecallResponse(1L, Arrays.asList(recommendSkn)), false);
|
|
|
results.add(recallRequestResponse);
|
|
|
}
|
|
|
return results;
|
...
|
...
|
|