Authored by hugufei

移包

... ... @@ -7,8 +7,8 @@ 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.beans.CacheRecallSknRequestResponseBean;
import com.yoho.search.recall.scene.beans.CacheSknInfoRequestResponseBean;
import com.yoho.search.recall.scene.cache.beans.CacheRecallSknRequestResponseBean;
import com.yoho.search.recall.scene.cache.beans.CacheSknInfoRequestResponseBean;
import com.yoho.search.recall.scene.strategy.helper.SortBuilderHelper;
import com.yoho.search.recall.scene.models.*;
import com.yoho.search.recall.scene.builder.request.RecallParamsBuilder;
... ...
... ... @@ -31,9 +31,11 @@ public class RecallParamsBuilder {
List<String> firstProductSkns = recallServiceHelper.getFirstProductSkns(paramMap);
//3、获取uid或udid
int uid = MapUtils.getIntValue(paramMap, "uid", 1);
int uid = MapUtils.getIntValue(paramMap, "uid", 0);
String udid = MapUtils.getString(paramMap, "udid", "");
if(uid==0){
uid = (int)(Math.random() * Integer.MAX_VALUE);
}
return new RecallSknParams(queryFilter,pageSize, firstProductSkns , uid, udid);
}
}
... ...
package com.yoho.search.recall.scene.builder.response;
import com.yoho.search.recall.scene.beans.CacheRecallRequestResponse;
import com.yoho.search.recall.scene.cache.beans.CacheRecallRequestResponse;
import com.yoho.search.recall.scene.models.RecallRequest;
import com.yoho.search.recall.scene.models.RecallResponse;
import com.yoho.search.recall.scene.models.RecallResponseBatch;
... ...
... ... @@ -5,12 +5,10 @@ import com.yoho.search.base.utils.CollectionUtils;
import com.yoho.search.base.utils.Transfer;
import com.yoho.search.common.cache.impls.EhCache;
import com.yoho.search.core.redis.components.YohoSearchRedisComponent;
import com.yoho.search.recall.scene.beans.CacheSknInfoRequestResponse;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.*;
... ...
package com.yoho.search.recall.scene.cache.beans;
import com.alibaba.fastjson.JSON;
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
import com.yoho.search.base.utils.Transfer;
import com.yoho.search.recall.scene.cache.CacheRequestResponse;
import com.yoho.search.recall.scene.models.RecallRequest;
import com.yoho.search.recall.scene.models.RecallResponse;
public class CacheRecallRequestResponse extends CacheRequestResponse<RecallRequest,RecallResponse> {
public CacheRecallRequestResponse(RecallRequest request) {
super(request);
}
@Override
public Transfer<String, RecallResponse> getToResponseTransfer() {
return toResponseTransfer;
}
@Override
public Transfer<RecallResponse, String> getFromResponseTransfer() {
return fromResponseTransfer;
}
private static Transfer<String,RecallResponse> toResponseTransfer = new Transfer<String, RecallResponse>() {
@Override
public RecallResponse transfer(String jsonValue) {
return JSON.parseObject(jsonValue, RecallResponse.class);
}
};
private static Transfer<RecallResponse,String> fromResponseTransfer = new Transfer<RecallResponse, String>() {
@Override
public String transfer(RecallResponse recallResponse) {
return JSON.toJSONString(recallResponse);
}
};
}
... ...
package com.yoho.search.recall.scene.cache.beans;
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.recall.scene.cache.CacheRequestResponseComponent;
import com.yoho.search.recall.scene.models.RecallRequest;
import com.yoho.search.recall.scene.models.RecallResponse;
import com.yoho.search.service.base.SearchCommonService;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Component
public class CacheRecallRequestResponseBean extends CacheRequestResponseComponent<RecallRequest,RecallResponse,CacheRecallRequestResponse>{
@Autowired
private SearchCommonService searchCommonService;
private static final int maxEsRequestCountPerTime = 10;
/**
* 批量召回入口
*
* @param batchRequests
* @return
*/
public List<CacheRecallRequestResponse> batchRecallAndCache(final List<RecallRequest> batchRequests) {
//1、构造请求
final List<CacheRecallRequestResponse> results = new ArrayList<>();
for (RecallRequest request : batchRequests) {
results.add(new CacheRecallRequestResponse(request));
}
//2、执行查询
this.bacthFillResponseWithCache(results,false);
//3、返回结果
return results;
}
@Override
public Map<RecallRequest, RecallResponse> queryMissCacheRequestResults(List<CacheRecallRequestResponse> missCacheRequests) {
//1、构造请求参数
List<SearchParam> searchParams = new ArrayList<>();
for (CacheRecallRequestResponse requestResponse : missCacheRequests) {
searchParams.add(requestResponse.getRequest().searchParam());
//控制每次请求es的数量
if(searchParams.size()>=maxEsRequestCountPerTime){
break;
}
}
//2、执行搜索
List<SearchResult> searchResults = searchCommonService.doMutiSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParams);
//3、构造返回结果
Map<RecallRequest,RecallResponse> notCachedResults = new HashMap<>();
for (int i = 0; i < missCacheRequests.size(); i++) {
RecallRequest request = missCacheRequests.get(i).getRequest();
SearchResult searchResult = searchResults.get(i);
RecallResponse response = this.buildResonse(searchResult);
notCachedResults.put(request,response);
}
return notCachedResults;
}
private RecallResponse buildResonse(SearchResult searchResult) {
List<Map<String, Object>> results = searchResult.getResultList();
List<RecallResponse.RecallSkn> recallSkns = new ArrayList<>();
for (Map<String, Object> result : results) {
Integer productSkn = MapUtils.getInteger(result, ProductIndexEsField.productSkn, 0);
Integer brandId = MapUtils.getInteger(result, ProductIndexEsField.brandId, 0);
Integer middleSortId = MapUtils.getInteger(result, ProductIndexEsField.middleSortId, 0);
recallSkns.add(new RecallResponse.RecallSkn(productSkn, brandId, middleSortId));
}
return new RecallResponse(searchResult.getTotal(), recallSkns);
}
}
... ...
package com.yoho.search.recall.scene.cache.beans;
import com.alibaba.fastjson.JSON;
import com.yoho.search.base.utils.Transfer;
import com.yoho.search.recall.scene.cache.CacheRequestResponse;
import com.yoho.search.recall.scene.models.RecallSknParams;
import com.yoho.search.recall.scene.models.RecallSknResult;
public class CacheRecallSknRequestResponse extends CacheRequestResponse<RecallSknParams,RecallSknResult> {
public CacheRecallSknRequestResponse(RecallSknParams request) {
super(request);
}
private static Transfer<String,RecallSknResult> toResponseTransfer = new Transfer<String, RecallSknResult>() {
@Override
public RecallSknResult transfer(String jsonValue) {
return JSON.parseObject(jsonValue, RecallSknResult.class);
}
};
private static Transfer<RecallSknResult,String> fromResponseTransfer = new Transfer<RecallSknResult, String>() {
@Override
public String transfer(RecallSknResult recallSknResult) {
return JSON.toJSONString(recallSknResult);
}
};
@Override
public Transfer<String, RecallSknResult> getToResponseTransfer() {
return toResponseTransfer;
}
@Override
public Transfer<RecallSknResult, String> getFromResponseTransfer() {
return fromResponseTransfer;
}
}
... ...
package com.yoho.search.recall.scene.cache.beans;
import com.yoho.search.recall.scene.builder.request.BrandRecallRequestBuilder;
import com.yoho.search.recall.scene.builder.request.CommonRecallRequestBuilder;
import com.yoho.search.recall.scene.builder.request.SortPriceRecallRequestBuilder;
import com.yoho.search.recall.scene.builder.response.RecallResponseBatchBuilder;
import com.yoho.search.recall.scene.builder.response.RecallSknResultBuilder;
import com.yoho.search.recall.scene.cache.CacheRequestResponseComponent;
import com.yoho.search.recall.scene.models.RecallRequest;
import com.yoho.search.recall.scene.models.RecallResponseBatch;
import com.yoho.search.recall.scene.models.RecallSknParams;
import com.yoho.search.recall.scene.models.RecallSknResult;
import com.yoho.search.recall.scene.persional.PersionalFactor;
import com.yoho.search.recall.scene.persional.RecallPersionalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Component
public class CacheRecallSknRequestResponseBean extends CacheRequestResponseComponent<RecallSknParams,RecallSknResult,CacheRecallSknRequestResponse>{
@Autowired
private RecallPersionalService recallPersionalService;
@Autowired
private CacheRecallRequestResponseBean cacheRecallRequestResponseBean;
@Autowired
private RecallResponseBatchBuilder recallResponseBatchBuilder;
@Autowired
private RecallSknResultBuilder recallSknResultBuilder;
@Autowired
private CommonRecallRequestBuilder commonRequestBuilder;
@Autowired
private BrandRecallRequestBuilder brandRequestBuilder;
@Autowired
private SortPriceRecallRequestBuilder sortPriceRequestBuilder;
/**
* 召回入口
* @param recallSknParams
* @return
*/
public RecallSknResult doRecallSknResult(RecallSknParams recallSknParams){
//1、构建请求
List<CacheRecallSknRequestResponse> requests = new ArrayList<>();
requests.add(new CacheRecallSknRequestResponse(recallSknParams));
//2、执行父类方法
this.bacthFillResponseWithCache(requests,false);
//3、判断
if(requests.size()==1 && requests.get(0).getResponse()!=null){
return requests.get(0).getResponse();
}
return null;
}
@Override
public Map<RecallSknParams, RecallSknResult> queryMissCacheRequestResults(List<CacheRecallSknRequestResponse> missCachseRequests) {
Map<RecallSknParams, RecallSknResult> results = new HashMap<>();
for (CacheRecallSknRequestResponse request: missCachseRequests) {
RecallSknResult response = this.doRealRecall(request.getRequest());
results.put(request.getRequest(),response);
}
return results;
}
/**
* 真正的召回入口
* @param param
* @return
*/
private RecallSknResult doRealRecall(RecallSknParams param) {
//1、获取个性化因子
PersionalFactor persionalFactor = recallPersionalService.queryPersionalFactor(param);
//2、构造请求
List<RecallRequest> batchRequests = this.buildBatchRequests(param, persionalFactor);
//3、批量召回
List<CacheRecallRequestResponse> requestResponses = cacheRecallRequestResponseBean.batchRecallAndCache(batchRequests);
//4、获取skn列表[去重]
RecallResponseBatch recallResponseBatch = recallResponseBatchBuilder.buildRecallResponseBatch(requestResponses);
//5、构造真实结果[排序,截取skn]
RecallSknResult recallSknResult = recallSknResultBuilder.builderRecallResult(recallResponseBatch, param, persionalFactor);
return recallSknResult;
}
/**
* 批量构造请求
* @param param
* @param persionalFactor
* @return
*/
private List<RecallRequest> buildBatchRequests(RecallSknParams param, PersionalFactor persionalFactor) {
//1、构造召回请求
List<RecallRequest> allRequests = new ArrayList<>();
//2、构造非个性化的请求
List<RecallRequest> commonRequests = commonRequestBuilder.buildCommonRecallRequests(param.getParamQueryFilter(), param.getFirstProductSkns());
allRequests.addAll(commonRequests);
//4、构建个性化品牌的召回请求
List<RecallRequest> brandRequests = brandRequestBuilder.buildBrandRecallRequests(param.getParamQueryFilter(), persionalFactor.getBrandIds());
allRequests.addAll(brandRequests);
//5、构建个性化品牌的召回请求
List<RecallRequest> sortPriceRequests = sortPriceRequestBuilder.buildSortPriceRecallRequests(param.getParamQueryFilter(), persionalFactor.getSortPriceAreas());
allRequests.addAll(sortPriceRequests);
return allRequests;
}
}
... ...
package com.yoho.search.recall.scene.cache.beans;
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.Transfer;
import com.yoho.search.recall.scene.cache.CacheRequestResponse;
import com.yoho.search.recall.scene.models.SknInfoRequest;
import java.util.HashMap;
import java.util.Map;
public class CacheSknInfoRequestResponse extends CacheRequestResponse<SknInfoRequest, Map<String, Object>> {
public CacheSknInfoRequestResponse(SknInfoRequest sknInfoRequest) {
super(sknInfoRequest);
}
static Transfer<String, Map<String, Object>> toResponseTransfer = 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;
}
};
static Transfer<Map<String, Object>, String> fromResponseTransfer = new Transfer<Map<String, Object>, String>() {
@Override
public String transfer(Map<String, Object> product) {
return JSON.toJSONString(product);
}
};
@Override
public Transfer<String, Map<String, Object>> getToResponseTransfer() {
return toResponseTransfer;
}
@Override
public Transfer<Map<String, Object>, String> getFromResponseTransfer() {
return fromResponseTransfer;
}
}
... ...
package com.yoho.search.recall.scene.cache.beans;
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.recall.scene.cache.CacheRequestResponseComponent;
import com.yoho.search.recall.scene.models.SknInfoRequest;
import com.yoho.search.service.base.SearchCommonService;
import com.yoho.search.service.base.index.ProductIndexBaseService;
import org.apache.commons.collections.MapUtils;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Component
public class CacheSknInfoRequestResponseBean extends CacheRequestResponseComponent<SknInfoRequest,Map<String, Object>,CacheSknInfoRequestResponse>{
@Autowired
private SearchCommonService searchCommonService;
@Autowired
private ProductIndexBaseService productIndexBaseService;
/**
* 按skn查询并按顺序返回
*
* @param productSkns
* @return
*/
public List<Map<String, Object>> queryProductListBySkn(List<Integer> productSkns,int size){
//1、批量查询SKN信息
List<CacheSknInfoRequestResponse> sknInfoCacheRequestRespons = this.batchQuery(productSkns);
//2、构造返回结果
List<Map<String, Object>> finalResults = new ArrayList<>();
for (CacheSknInfoRequestResponse sknInfoCacheRequestResponse : sknInfoCacheRequestRespons) {
if(sknInfoCacheRequestResponse !=null && sknInfoCacheRequestResponse.getResponse()!=null){
finalResults.add(sknInfoCacheRequestResponse.getResponse());
}
if(finalResults.size()>=size){
break;
}
}
return finalResults;
}
private List<CacheSknInfoRequestResponse> batchQuery(List<Integer> productSkns){
//1、构建请求与返回结果
final List<CacheSknInfoRequestResponse> requests = new ArrayList<>();
for (Integer productSkn : productSkns) {
requests.add(new CacheSknInfoRequestResponse(new SknInfoRequest(productSkn)));
}
//2、调父类方法
this.bacthFillResponseWithCache(requests,true);
//3、返回结果
return requests;
}
@Override
public Map<SknInfoRequest, Map<String, Object>> queryMissCacheRequestResults(List<CacheSknInfoRequestResponse> missCacheRequests) {
//1、合法性判断
Map<SknInfoRequest,Map<String, Object>> results = new HashMap<>();
if(missCacheRequests==null||missCacheRequests.isEmpty()){
return results;
}
//2、获取skn
List<Integer> productSkns = new ArrayList<>();
for (CacheSknInfoRequestResponse sknInfoCacheRequestResponse : missCacheRequests) {
productSkns.add(sknInfoCacheRequestResponse.getRequest().getProductSkn());
}
//3、构建SearchParam
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());
//4、构建SKN临时结果
Map<Integer,Map<String, Object>> productTempMap = new HashMap<>();
for (Map<String, Object> product: productList) {
productTempMap.put(MapUtils.getIntValue(product,"product_skn",0),product);
}
//5、构造最终结果
for (CacheSknInfoRequestResponse requestResponse :missCacheRequests ) {
results.put(requestResponse.getRequest(),productTempMap.get(requestResponse.getRequest().getProductSkn()));
}
return results;
}
}
... ...
... ... @@ -22,7 +22,7 @@ class PersionalFactorUserComponent {
}
List<PersionalFactor.SortPriceArea> sortPriceArea = new ArrayList<PersionalFactor.SortPriceArea>();
for (int i = 0; i < 50; i++) {
sortPriceArea.add(new PersionalFactor.SortPriceArea((int) (Math.random() * 1000), (int) (Math.random() * 3)));
sortPriceArea.add(new PersionalFactor.SortPriceArea((int) (Math.random() * 1000), (int) (Math.random() *10)));
}
String factor = "";
return new PersionalFactor(brandIds, sortPriceArea,factor);
... ...