Authored by zhaojun2

brand product

... ... @@ -2,18 +2,22 @@ package com.yoho.search.service.scene.activity;
import com.yoho.search.base.helper.Word2VectorCalculator;
import com.yoho.search.base.utils.CollectionUtils;
import com.yoho.search.base.utils.ProductIndexEsField;
import com.yoho.search.cache.beans.AbstractCacheBean;
import com.yoho.search.core.personalized.models.PersonalizedSearch;
import com.yoho.search.core.personalized.models.UserPersonalFactorRspNew;
import com.yoho.search.service.recall.beans.helper.StrategyHelper;
import com.yoho.search.service.recall.beans.persional.ProductFeatureFactorComponent;
import com.yoho.search.service.recall.beans.persional.UserPersionalFactorComponent;
import com.yoho.search.service.recall.beans.strategy.IStrategy;
import com.yoho.search.service.recall.beans.strategy.impls.SortBrandHeatValueStrategy;
import com.yoho.search.service.recall.beans.strategy.StrategyEnum;
import com.yoho.search.service.recall.beans.vector.BrandVectorCacheBean;
import com.yoho.search.service.recall.models.common.ParamQueryFilter;
import com.yoho.search.service.recall.models.personal.BrandVectorScore;
import com.yoho.search.service.recall.models.personal.UserPersonalFactor;
import com.yoho.search.service.recall.models.personal.UserFeatureFactor;
import com.yoho.search.service.recall.models.req.RecallRequest;
import com.yoho.search.service.recall.models.req.RecallRequestResponse;
import com.yoho.search.service.recall.models.req.UserRecallRequest;
import com.yoho.search.service.scene.shopbrand.AggProductResponse;
import com.yoho.search.service.scorer.personal.PersonalVectorFeatureSearch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
... ... @@ -31,6 +35,10 @@ public class AggBrandProductCacheBean extends AbstractCacheBean<BrandProductRequ
private BrandVectorCacheBean brandVectorCacheBean;
@Autowired
private BatchBrandProductCacheBean batchBrandProductCacheBean;
@Autowired
private PersonalVectorFeatureSearch personalVectorFeatureSearch;
@Autowired
private ProductFeatureFactorComponent productFeatureFactorComponent;
/**
* 入口
... ... @@ -81,13 +89,50 @@ public class AggBrandProductCacheBean extends AbstractCacheBean<BrandProductRequ
}
//2、获取召回结果
batchRecall(brandProductRequest, userPersonalbrandIds, aggBrandIds);
List<BatchBrandProductRequestResponse> brandProductList = batchRecall(brandProductRequest, userPersonalbrandIds, aggBrandIds);
if (brandProductRequest.hasUidOrUdid()) {
doCalScoreAndSort(brandProductList, brandProductRequest.getUid(), brandProductRequest.getBrandCount());
}
//直接截取
return null;
}
/**
* 粗排-按相关性计算得分,并按得分排序
*
* @param sknResultList
* @param uid
*/
private void doCalScoreAndSort(List<BatchBrandProductRequestResponse> sknResultList, int uid, int pageSize) {
//1、获取用户向量
Map<String, String> paramMap = new HashMap<>();
paramMap.put("uid", "" + uid);
PersonalizedSearch personalizedSearch = personalVectorFeatureSearch.queryPersonalizedSearch(paramMap);
UserFeatureFactor userFeatureFactor = new UserFeatureFactor(personalizedSearch);
//2、计算相关性
int recommendSknIndex = 10000;
for (BatchBrandProductRequestResponse brandProduct : sknResultList){
AggProductResponse aggProductResponse = brandProduct.getResponse();
List<Map<String, Object>> productList = aggProductResponse.getProduct_list();
for (Map<String, Object> product : productList) {
double score = 0d;
score = productFeatureFactorComponent.calProductFeatureFactor(userFeatureFactor, product.get(ProductIndexEsField.productFeatureFactor).toString());
}
}
}
private List<Integer> buildUserPersonalBrandIds(List<Integer> aggBrandIds, UserPersonalFactorRspNew userFactor, int count) {
List<Integer> result = new ArrayList<>();
double userBrandVectorNorm = Word2VectorCalculator.getVectorListNorm(userFactor.getBrandVectorW2v());
... ... @@ -111,11 +156,11 @@ public class AggBrandProductCacheBean extends AbstractCacheBean<BrandProductRequ
return result;
}
private List<BrandProductRequestResponse> batchRecall(BrandProductRequest brandProductRequest, List<Integer> userPersonalbrandIds, List<Integer> aggBrandIds) {
private List<BatchBrandProductRequestResponse> batchRecall(BrandProductRequest brandProductRequest, List<Integer> userPersonalbrandIds, List<Integer> aggBrandIds) {
List<Integer> brandIds = new ArrayList<>(userPersonalbrandIds);
brandIds.addAll(aggBrandIds);
int brandProductCount = brandProductRequest.getBrandProductCount() + 10;
int brandProductCount = brandProductRequest.getBrandProductCount() + 20;
ParamQueryFilter paramQueryFilter = brandProductRequest.getParamQueryFilter();
List<RecallRequest> brandRequests = brandIds.stream().map(brandId -> {
... ... @@ -123,11 +168,8 @@ public class AggBrandProductCacheBean extends AbstractCacheBean<BrandProductRequ
return new RecallRequest(paramQueryFilter, strategy);
}).collect(Collectors.toList());
return null;
List<BatchBrandProductRequestResponse> requestResponses = batchBrandProductCacheBean.batchRecallAndCache(brandRequests);
return requestResponses;
}
... ...
... ... @@ -8,17 +8,14 @@ import com.yoho.search.common.SearchCommonService;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.core.es.model.SearchResult;
import com.yoho.search.service.recall.models.req.RecallRequest;
import com.yoho.search.service.recall.models.req.RecallRequestResponse;
import com.yoho.search.service.recall.models.req.RecallResponse;
import com.yoho.search.service.recall.models.req.SknBaseInfoResponse;
import org.apache.commons.collections.MapUtils;
import com.yoho.search.service.scene.shopbrand.AggProductResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.*;
@Component
public class BatchBrandProductCacheBean extends AbstractCacheBean<RecallRequest, SknBaseInfoResponse, BatchBrandProductRequestResponse> {
public class BatchBrandProductCacheBean extends AbstractCacheBean<RecallRequest, AggProductResponse, BatchBrandProductRequestResponse> {
@Autowired
... ... @@ -48,8 +45,8 @@ public class BatchBrandProductCacheBean extends AbstractCacheBean<RecallRequest,
}
@Override
protected Map<RecallRequest, SknBaseInfoResponse> queryMissCacheRequestResults(List<BatchBrandProductRequestResponse> missCacheRequests) {
Map<RecallRequest, SknBaseInfoResponse> results = new HashMap<>();
protected Map<RecallRequest, AggProductResponse> queryMissCacheRequestResults(List<BatchBrandProductRequestResponse> missCacheRequests) {
Map<RecallRequest, AggProductResponse> results = new HashMap<>();
if (missCacheRequests == null || missCacheRequests.isEmpty()) {
return results;
}
... ... @@ -61,25 +58,15 @@ public class BatchBrandProductCacheBean extends AbstractCacheBean<RecallRequest,
searchParams.add(searchParam);
}
List<SearchResult> searchResults = searchCommonService.doMutiSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParams);
for (int i = 0; i < missCacheRequests.size(); i++) {
RecallRequest request = missCacheRequests.get(i).getRequest();
SearchResult searchResult = searchResults.get(i);
SknBaseInfoResponse response = this.buildResponse(searchResult);
results.put(request,response);
}
AggProductResponse response = new AggProductResponse();
return null;
}
private SknBaseInfoResponse buildResponse(SearchResult searchResult) {
List<Map<String, Object>> productList = searchResult.getResultList();
List<Integer> recallSknList = new ArrayList<>();
//1、构建结果
for (Map<String, Object> productInfo : productList) {
Integer productSkn = MapUtils.getInteger(productInfo, ProductIndexEsField.productSkn, 0);
recallSknList.add(productSkn);
SearchResult searchResult = searchResults.get(i);
response.setProduct_list(searchResult.getResultList());
response.setCount((int)searchResult.getTotal());
results.put(request, response);
}
return null;//new RecallResponse(searchResult.getTotal(),recallSknList);
return results;
}
}
... ...
... ... @@ -3,20 +3,20 @@ package com.yoho.search.service.scene.activity;
import com.yoho.search.base.utils.Transfer;
import com.yoho.search.cache.model.AbstractCacheRequestResponse;
import com.yoho.search.service.recall.models.req.RecallRequest;
import com.yoho.search.service.recall.models.req.SknBaseInfoResponse;
import com.yoho.search.service.scene.shopbrand.AggProductResponse;
public class BatchBrandProductRequestResponse extends AbstractCacheRequestResponse<RecallRequest, SknBaseInfoResponse> {
public class BatchBrandProductRequestResponse extends AbstractCacheRequestResponse<RecallRequest, AggProductResponse> {
public BatchBrandProductRequestResponse(RecallRequest request) {
super(request);
}
@Override
public Transfer<String, SknBaseInfoResponse> getToResponseTransfer() {
public Transfer<String, AggProductResponse> getToResponseTransfer() {
return null;
}
@Override
public Transfer<SknBaseInfoResponse, String> getFromResponseTransfer() {
public Transfer<AggProductResponse, String> getFromResponseTransfer() {
return null;
}
}
... ...
package com.yoho.search.service.scene.shopbrand;
import java.util.List;
import java.util.Map;
public class AggProductResponse {
private Integer count;
private Map<String, Object> info;
private List<Map<String, Object>> product_list;
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
public Map<String, Object> getInfo() {
return info;
}
public void setInfo(Map<String, Object> info) {
this.info = info;
}
public List<Map<String, Object>> getProduct_list() {
return product_list;
}
public void setProduct_list(List<Map<String, Object>> product_list) {
this.product_list = product_list;
}
}
... ...
... ... @@ -18,7 +18,7 @@ import java.util.List;
import java.util.Map;
@Service
public class ShopProductCacheBean extends AbstractCacheBean<ShopProductRequest, ShopProductResponse, ShopProductRequestResponse> {
public class ShopProductCacheBean extends AbstractCacheBean<ShopProductRequest, AggProductResponse, ShopProductRequestResponse> {
@Autowired
private SearchCommonService searchCommonService;
... ... @@ -28,13 +28,13 @@ public class ShopProductCacheBean extends AbstractCacheBean<ShopProductRequest,
private ProductListHelper productListHelper;
public Map<String, ShopProductResponse> queryShopProductList(List<ShopProductRequest> requests) {
public Map<String, AggProductResponse> queryShopProductList(List<ShopProductRequest> requests) {
List<ShopProductRequestResponse> requestResponses = new ArrayList<>();
requests.forEach(e -> requestResponses.add(new ShopProductRequestResponse(e)));
//2、执行查询
this.bacthFillResponseWithCache(requestResponses,requests.size());
//3、返回结果
Map<String, ShopProductResponse> responseMap = new HashMap<>();
Map<String, AggProductResponse> responseMap = new HashMap<>();
requestResponses.forEach(e -> {
if (e.getResponse()!=null && e.getResponse().getCount() > 0) {
responseMap.put(e.getRequest().getShopId().toString(), e.getResponse());
... ... @@ -49,8 +49,8 @@ public class ShopProductCacheBean extends AbstractCacheBean<ShopProductRequest,
}
@Override
protected Map<ShopProductRequest, ShopProductResponse> queryMissCacheRequestResults(List<ShopProductRequestResponse> missCacheRequests) {
Map<ShopProductRequest, ShopProductResponse> results = new HashMap<>();
protected Map<ShopProductRequest, AggProductResponse> queryMissCacheRequestResults(List<ShopProductRequestResponse> missCacheRequests) {
Map<ShopProductRequest, AggProductResponse> results = new HashMap<>();
if (missCacheRequests == null || missCacheRequests.isEmpty()) {
return results;
}
... ... @@ -79,7 +79,7 @@ public class ShopProductCacheBean extends AbstractCacheBean<ShopProductRequest,
}
for (ShopProductRequestResponse requestResponse : missCacheRequests) {
String shopId = requestResponse.getRequest().getShopId().toString();
ShopProductResponse response = new ShopProductResponse();
AggProductResponse response = new AggProductResponse();
response.setCount(shopProductCountMap.get(shopId) != null ? shopProductCountMap.get(shopId).intValue() : 0);
response.setInfo(shopMap.get(shopId));
response.setProduct_list(shopProductListMap.get(shopId));
... ...
... ... @@ -4,19 +4,19 @@ import com.alibaba.fastjson.JSON;
import com.yoho.search.base.utils.Transfer;
import com.yoho.search.cache.model.AbstractCacheRequestResponse;
public class ShopProductRequestResponse extends AbstractCacheRequestResponse<ShopProductRequest, ShopProductResponse> {
public class ShopProductRequestResponse extends AbstractCacheRequestResponse<ShopProductRequest, AggProductResponse> {
public ShopProductRequestResponse(ShopProductRequest request) {
super(request);
}
@Override
public Transfer<String, ShopProductResponse> getToResponseTransfer() {
return (v) -> JSON.parseObject(v, ShopProductResponse.class);
public Transfer<String, AggProductResponse> getToResponseTransfer() {
return (v) -> JSON.parseObject(v, AggProductResponse.class);
}
@Override
public Transfer<ShopProductResponse, String> getFromResponseTransfer() {
public Transfer<AggProductResponse, String> getFromResponseTransfer() {
return (v) -> JSON.toJSONString(v);
}
}
... ...
... ... @@ -112,9 +112,9 @@ public class ShopsService extends BaseService implements ApplicationEventPublish
ParamQueryFilter paramQueryFilter = new ParamQueryFilter(searchParam.getQuery(), (BoolQueryBuilder) searchParam.getFiter());
requests.add(new ShopProductRequest(paramQueryFilter, Integer.valueOf(shopId), topHitCount));
}
Map<String, ShopProductResponse> responseMap = shopProductCacheBean.queryShopProductList(requests);
Map<String, AggProductResponse> responseMap = shopProductCacheBean.queryShopProductList(requests);
// 3、返回生成结果
Integer total = responseMap.values().stream().collect(Collectors.summingInt(ShopProductResponse::getCount));
Integer total = responseMap.values().stream().collect(Collectors.summingInt(AggProductResponse::getCount));
JSONObject realResult = new JSONObject();
realResult.put("total", total);
realResult.put("shops", responseMap);
... ...