Authored by hugufei

单skn查询时也使用缓存

package com.yoho.search.recall.scene;
import com.alibaba.fastjson.JSONObject;
import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.base.utils.ProductIndexEsField;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.core.es.model.SearchResult;
import com.yoho.search.models.SearchApiResult;
import com.yoho.search.recall.scene.component.*;
import com.yoho.search.recall.scene.helper.SortBuilderHelper;
import com.yoho.search.recall.scene.models.*;
import com.yoho.search.recall.scene.persional.PersionalFactor;
import com.yoho.search.recall.scene.persional.RecallPersionalService;
import com.yoho.search.service.base.SearchCommonService;
import com.yoho.search.service.base.index.ProductIndexBaseService;
import com.yoho.search.service.helper.SearchCommonHelper;
import org.apache.commons.collections.MapUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.SortBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
... ... @@ -35,6 +46,12 @@ public class SceneRecallService {
private RecallResultBuilder recallResultBuilder;
@Autowired
private SearchCommonHelper searchCommonHelper;
@Autowired
private ProductIndexBaseService productIndexBaseService;
@Autowired
private SearchCommonService searchCommonService;
@Autowired
private BacthQueryBySknComponent bacthQueryBySknComponent;
public SearchApiResult sceneRecall(Map<String, String> paramMap) {
try {
... ... @@ -50,14 +67,15 @@ public class SceneRecallService {
PersionalFactor persionalFactor = recallPersionalService.queryPersionalFactor(recallParams);
//4、执行召回
RecallResult recallResult = this.doRecall(recallParams, persionalFactor);
//TODO
//5、构造返回结果
//5、根据召回结果查询商品信息
List<Map<String, Object>> productList = this.queryProductList(recallParams,recallResult);
//6、构造返回结果
JSONObject dataMap = new JSONObject();
dataMap.put("total", recallResult.getTotal());
dataMap.put("page", recallParams.getPage());
dataMap.put("page_size", recallParams.getPageSize());
dataMap.put("page_total", searchCommonHelper.getTotalPage(recallResult.getTotal(),recallParams.getPageSize()));
dataMap.put("product_list",recallResult);
dataMap.put("page_total", searchCommonHelper.getTotalPage(recallResult.getTotal(), recallParams.getPageSize()));
dataMap.put("product_list", productList);
return new SearchApiResult().setData(dataMap);
} catch (Exception e) {
logger.error(e.getMessage(), e);
... ... @@ -65,6 +83,13 @@ public class SceneRecallService {
}
}
/**
* 召回入口
*
* @param param
* @param persionalFactor
* @return
*/
private RecallResult doRecall(RecallParams param, PersionalFactor persionalFactor) {
//1、构造请求
List<RecallRequest> batchRequests = batchRequestsBuilder.buildBatchRequests(param, persionalFactor);
... ... @@ -77,4 +102,48 @@ public class SceneRecallService {
return recallResult;
}
private List<Map<String, Object>> queryProductList(RecallParams recallParams,RecallResult recallResult) {
//1、
List<Integer> productSkns = recallResult.getSknList();
if (productSkns != null && !productSkns.isEmpty()) {
return bacthQueryBySknComponent.queryProductListBySkn(productSkns,productSkns.size());
}else{
return this.queryProductByFilterSkn(recallResult.getNotProductSkn(),recallResult.getRealPage(),recallParams.getPageSize());
}
}
/**
* 过滤掉已召回的skn,并按人气排序
* @param notProductSkns
* @param page
* @param pageSize
* @return
*/
private List<Map<String, Object>> queryProductByFilterSkn(List<Integer> notProductSkns,int page,int pageSize){
SearchParam searchParam = new SearchParam();
//设置分页参数
searchParam.setOffset((page-1)*pageSize);
searchParam.setSize(pageSize);
//设置filter
BoolQueryBuilder filter = QueryBuilders.boolQuery();
filter.mustNot(QueryBuilders.termsQuery(ProductIndexEsField.productSkn,notProductSkns));
//设置IncludeFields
searchParam.setIncludeFields(productIndexBaseService.getProductIndexIncludeFields());
//设置排序
List<SortBuilder<?>> sortBuilders = new ArrayList<>();
sortBuilders.add(SortBuilderHelper.getHeatValueDescSort());
sortBuilders.add(SortBuilderHelper.getSevendayMoneyDescSort());
sortBuilders.add(SortBuilderHelper.getIdDescSort());
searchParam.setSortBuilders(sortBuilders);
SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX,searchParam);
return productIndexBaseService.getProductListWithPricePlan(searchResult.getResultList());
}
}
... ...
package com.yoho.search.recall.scene.component;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
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.base.utils.Transfer;
import com.yoho.search.common.cache.impls.SearchRedis;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.core.es.model.SearchResult;
import com.yoho.search.recall.scene.models.CacheRequestResponse;
import com.yoho.search.service.base.SearchCommonService;
import com.yoho.search.service.base.index.ProductIndexBaseService;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
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 BacthQueryBySknComponent {
private static final Logger logger = LoggerFactory.getLogger(BacthQueryBySknComponent.class);
@Autowired
private SearchRedis searchRedis;
@Autowired
private SearchCommonService searchCommonService;
@Autowired
private ProductIndexBaseService productIndexBaseService;
/**
* 按skn查询并按顺序返回
*
* @param productSkns
* @return
*/
public List<Map<String, Object>> queryProductListBySkn(List<Integer> productSkns,int size){
//1、构建请求
List<CacheRequestResponse<Integer, Map<String, Object>>> sknInfoRequestResponses = new ArrayList<>();
for (Integer productSkn : productSkns) {
sknInfoRequestResponses.add(new CacheRequestResponse(productSkn,redisKeyBuilderTransfer(),toValueTransfer(),fromValueTransfer()));
}
//2、批量从缓存中获取
sknInfoRequestResponses = this.batchQueryFromCache(sknInfoRequestResponses);
//3、获取未命中缓存的skn
List<Integer> notCacheSkns = new ArrayList<>();
for (CacheRequestResponse<Integer, Map<String, Object>> cacheRequestResponse : sknInfoRequestResponses) {
if(cacheRequestResponse.getValueObject()==null){
notCacheSkns.add(cacheRequestResponse.getKeyObject());
}
}
//4、执行批量查询
Map<Integer,Map<String, Object>> queryResults = this.batchQuery(notCacheSkns);
//5、填充查询结果
for (CacheRequestResponse<Integer, Map<String, Object>> cacheRequestResponse : sknInfoRequestResponses) {
if(cacheRequestResponse.getValueObject()==null){
cacheRequestResponse.setValueObject(queryResults.get(cacheRequestResponse.getKeyObject()));
cacheRequestResponse.setNeedRecache(true);
}
}
//6、将CacheRequestResponse中需要缓存的key加入缓存
this.batchAddToCache(sknInfoRequestResponses,10*60);//缓存10分钟
//7、构造返回结果
Map<Integer,Map<String, Object>> productInfoMap = new HashMap<>();
for (CacheRequestResponse<Integer, Map<String, Object>> sknInfoRequestResponse: sknInfoRequestResponses) {
productInfoMap.put(sknInfoRequestResponse.getKeyObject(),sknInfoRequestResponse.getValueObject());
}
List<Map<String, Object>> finalResults = new ArrayList<>();
for (Integer productSkn : productSkns) {
if(productInfoMap.get(productSkn)!=null){
finalResults.add(productInfoMap.get(productSkn));
}
if(finalResults.size()>=size){
break;
}
}
return finalResults;
}
private <K, V> List<CacheRequestResponse<K, V>> batchQueryFromCache(List<CacheRequestResponse<K, V>> cacheRequestResponses) {
try {
Collection<RedisKeyBuilder> keys = new ArrayList<>();
for (CacheRequestResponse<K, V> requestResponse : cacheRequestResponses) {
RedisKeyBuilder redisKeyBuilder = requestResponse.getRedisKeyBuilderTransfer().transfer(requestResponse.getKeyObject());
keys.add(redisKeyBuilder);
}
List<String> cachedValues = searchRedis.searchValueOperations.multiGet(keys);
for (int i = 0; i < cacheRequestResponses.size(); i++) {
CacheRequestResponse<K, V> requestResponse = cacheRequestResponses.get(i);
String cachedValue = cachedValues.get(i);
if (!StringUtils.isBlank(cachedValue)) {
requestResponse.setValueObject(requestResponse.getToValueTransfer().transfer(cachedValue));
requestResponse.setNeedRecache(false);
}
}
return cacheRequestResponses;
}catch (Exception e){
logger.error(e.getMessage(),e);
return cacheRequestResponses;
}
}
private <K, V> void batchAddToCache(List<CacheRequestResponse<K, V>> cacheRequestResponses,long timeOutInSecond){
try {
Map<RedisKeyBuilder, String> toCacheMap = new HashMap<>();
for (CacheRequestResponse<K, V> requestResponse : cacheRequestResponses) {
if(requestResponse.isNeedRecache() && requestResponse.getValueObject()!=null){
RedisKeyBuilder redisKeyBuilder = requestResponse.getRedisKeyBuilderTransfer().transfer(requestResponse.getKeyObject());
V value = requestResponse.getValueObject();
String cacheValue = requestResponse.getFromValueTransfer().transfer(value);
toCacheMap.put(redisKeyBuilder,cacheValue);
}
}
if(!toCacheMap.isEmpty()){
searchRedis.searchRedisTemplate.mset(toCacheMap,timeOutInSecond);
}
}catch (Exception e){
logger.error(e.getMessage(),e);
}
}
private Transfer<Integer, RedisKeyBuilder> redisKeyBuilderTransfer() {
return new Transfer<Integer, RedisKeyBuilder>() {
@Override
public RedisKeyBuilder transfer(Integer productSkn) {
return RedisKeyBuilder.newInstance().appendFixed("YOHOSEARCH:").appendFixed("SKN:").appendVar(productSkn);
}
};
}
private Transfer<String, Map<String, Object>> toValueTransfer() {
return new Transfer<String, Map<String, Object>>() {
@Override
public Map<String, Object> transfer(String value) {
Map<String, Object> product = new HashMap<>();
product.putAll(JSONObject.parseObject(value));
return product;
}
};
}
private Transfer<Map<String, Object>, String> fromValueTransfer() {
return new Transfer<Map<String, Object>, String>() {
@Override
public String transfer( Map<String, Object> product) {
return JSON.toJSONString(product);
}
};
}
private Map<Integer,Map<String, Object>> batchQuery(List<Integer> productSkns) {
SearchParam searchParam = new SearchParam();
searchParam.setOffset(0);
searchParam.setSize(productSkns.size());
searchParam.setFiter(QueryBuilders.termsQuery(ProductIndexEsField.productSkn, productSkns));
searchParam.setIncludeFields(productIndexBaseService.getProductIndexIncludeFields());
SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
List<Map<String, Object>> productList = productIndexBaseService.getProductListWithPricePlan(searchResult.getResultList());
Map<Integer,Map<String, Object>> results = new HashMap<>();
for (Map<String, Object> product: productList) {
results.put(MapUtils.getIntValue(product,"product_skn",0),product);
}
return results;
}
}
... ...
package com.yoho.search.recall.scene.component;
import com.yoho.search.recall.scene.models.*;
import com.yoho.search.recall.scene.persional.PersionalFactor;
import com.yoho.search.recall.scene.models.RecallRequest;
import com.yoho.search.recall.scene.models.RecallRequestResponse;
import com.yoho.search.recall.scene.models.RecallResponse;
import com.yoho.search.recall.scene.models.RecallResponseBatch;
import com.yoho.search.recall.scene.strategy.StrategyNameEnum;
import org.springframework.stereotype.Component;
... ...
... ... @@ -8,7 +8,6 @@ import com.yoho.search.recall.scene.persional.PersionalFactor;
import com.yoho.search.recall.scene.strategy.StrategyNameEnum;
import com.yoho.search.service.base.ProductListSortKey;
import com.yoho.search.service.base.ProductListSortService;
import com.yoho.search.service.helper.SearchCommonHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
... ... @@ -21,8 +20,6 @@ import java.util.List;
public class RecallResultBuilder {
@Autowired
private SearchCommonHelper searchCommonHelper;
@Autowired
private ProductListSortService productListSortService;
public RecallResult builderRecallResult(RecallResponseBatch recallResponseBatch, RecallParams param, PersionalFactor persionalFactor){
... ...
... ... @@ -9,8 +9,8 @@ import org.elasticsearch.search.sort.SortOrder;
public class SortBuilderHelper {
public static SortBuilder<?> getIdAscSort(){
return SortBuilders.fieldSort(ProductIndexEsField.id).order(SortOrder.ASC);
public static SortBuilder<?> getIdDescSort(){
return SortBuilders.fieldSort(ProductIndexEsField.id).order(SortOrder.DESC);
}
public static SortBuilder<?> getRandomSort(){
... ... @@ -29,4 +29,8 @@ public class SortBuilderHelper {
return SortBuilders.fieldSort(ProductIndexEsField.lastReducePriceTime).order(SortOrder.DESC);
}
public static SortBuilder<?> getSevendayMoneyDescSort(){
return SortBuilders.fieldSort(ProductIndexEsField.sevendayMoney).order(SortOrder.DESC);
}
}
... ...
package com.yoho.search.recall.scene.models;
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
import com.yoho.search.base.utils.Transfer;
public class CacheRequestResponse<K,V> {
private K keyObject;
private Transfer<K,RedisKeyBuilder> redisKeyBuilderTransfer;
private V valueObject;
private Transfer<String,V> toValueTransfer;
private Transfer<V,String> fromValueTransfer;
private boolean needRecache;
public CacheRequestResponse(K keyObject,Transfer<K,RedisKeyBuilder> redisKeyBuilderTransfer,Transfer<String,V> toValueTransfer,Transfer<V,String> fromValueTransfer){
this.keyObject = keyObject;
this.redisKeyBuilderTransfer = redisKeyBuilderTransfer;
this.toValueTransfer = toValueTransfer;
this.fromValueTransfer = fromValueTransfer;
}
public K getKeyObject() {
return keyObject;
}
public V getValueObject() {
return valueObject;
}
public Transfer<K, RedisKeyBuilder> getRedisKeyBuilderTransfer() {
return redisKeyBuilderTransfer;
}
public Transfer<String, V> getToValueTransfer() {
return toValueTransfer;
}
public Transfer<V, String> getFromValueTransfer() {
return fromValueTransfer;
}
public void setValueObject(V valueObject) {
this.valueObject = valueObject;
}
public boolean isNeedRecache() {
return needRecache;
}
public void setNeedRecache(boolean needRecache) {
this.needRecache = needRecache;
}
}
... ...
... ... @@ -7,8 +7,8 @@ public class RecallResult {
private final long recallTotal;
private final long recallPageTotal;
private final int realPage;
private final List<Integer> sknList;
private final List<Integer> notProductSkn;
private final List<Integer> sknList;//查询的页码在召回的范围内,则返回这些skn
private final List<Integer> notProductSkn;//查询的页码不在召回的范围内,则返回全部召回的skn,外层过滤这些skn
public RecallResult(long total,long recallTotal,long recallPageTotal, int realPage,List<Integer> sknList,List<Integer> notProductSkn) {
this.total = total;
... ...
... ... @@ -13,7 +13,7 @@ public interface IStrategy {
SortBuilder<?> sortBuilder();// 排序策略
String strategyCacheKey();
String strategyCacheKey();// 可用于缓存的key
int cacheTimeInSecond();//缓存时间
... ...
... ... @@ -38,7 +38,7 @@ public class CommonFirstSknStrategy implements IStrategy {
@Override
public SortBuilder<?> sortBuilder() {
return SortBuilderHelper.getIdAscSort();
return SortBuilderHelper.getIdDescSort();
}
@Override
... ...
package com.yoho.search.service.base;
import com.yoho.search.base.utils.CollectionUtils;
import com.yoho.search.base.utils.Transfer;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import java.util.*;
... ... @@ -53,4 +56,26 @@ public class ProductListSortService {
return tempResults;
}
public <K> List<Map<String,Object>> sortProductListByProductSkn(List<Map<String,Object>> productList, List<K> productSkns, int size,Transfer<Map<String,Object>,K> transfer){
List<Map<String,Object>> results = new ArrayList<>();
if(productList==null || productList.isEmpty()){
return results;
}
if(productSkns==null || productSkns.isEmpty() || size<=0){
return results;
}
Map<K, Map<String,Object>> productMap = CollectionUtils.toMap(productList, transfer);
for (K productSkn : productSkns){
if(productMap.containsKey(productSkn)){
Map<String,Object> product = productMap.get(productSkn);
results.add(new HashMap<>(product));//注意循环引用
}
if(results.size()>=size){
break;
}
}
return results;
}
}
... ...