Authored by zhaojun2

agg brand product

package com.yoho.search.service.scene.activity;
import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.base.utils.ProductIndexEsField;
import com.yoho.search.cache.CacheTimeConstants;
import com.yoho.search.cache.beans.AbstractCacheComponent;
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.helper.SearchParamHelper;
import com.yoho.search.service.recall.models.common.ParamQueryFilter;
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.*;
@Component
public class AggBrandListService extends AbstractCacheComponent<List<AggBrand>> {
@Autowired
private SearchCommonService searchCommonService;
@Autowired
private SearchParamHelper searchParamHelper;
public List<AggBrand> aggBrands(ParamQueryFilter paramQueryFilter) {
List<AggBrand> value;
try {
value = queryWithCache(paramQueryFilter, null);
} catch (Exception e) {
return Collections.emptyList();
}
return value != null ? value : Collections.emptyList();
}
private List<AggBrand> genAggBrand(Map<String, Aggregation> aggMaps) {
MultiBucketsAggregation aggregation = (MultiBucketsAggregation)aggMaps.get("brandAgg");
Iterator<? extends MultiBucketsAggregation.Bucket> itSizeAgg = aggregation.getBuckets().iterator();
List<AggBrand> aggBrands = new ArrayList<>();
while (itSizeAgg.hasNext()) {
AggBrand aggBrand = new AggBrand();
MultiBucketsAggregation.Bucket ltSize = itSizeAgg.next();
aggBrand.setBrandId(Integer.valueOf(ltSize.getKeyAsString()));
aggBrand.setCount(Long.valueOf(ltSize.getDocCount()).intValue());
aggBrands.add(aggBrand);
}
return aggBrands;
}
@Override
protected List<AggBrand> doRealQuery(ParamQueryFilter paramQueryFilter, Map<String, String> paramMap) {
List<AbstractAggregationBuilder<?>> aggregationBuilders = new ArrayList<>();
TermsAggregationBuilder firstAggregationBuilder = AggregationBuilders.terms("brandAgg").field(ProductIndexEsField.brandId).size(500);
aggregationBuilders.add(firstAggregationBuilder);
SearchParam searchParam = searchParamHelper.buildSearchParam(paramQueryFilter);
searchParam.setAggregationBuilders(aggregationBuilders);
SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
Map<String, Aggregation> aggregationMap = searchResult.getAggMaps();
return this.genAggBrand(aggregationMap);
}
@Override
protected int cacheTimeInMinute() {
return CacheTimeConstants.CACHE_60_MINUTE;
}
@Override
protected String cacheSceneKey() {
return "BRAND_PRODUCT_LIST_NEW";
}
}
... ...
... ... @@ -2,21 +2,17 @@ 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.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.UserFeatureFactor;
import com.yoho.search.service.recall.models.req.RecallRequest;
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;
... ... @@ -28,7 +24,7 @@ import java.util.stream.Collectors;
public class AggBrandProductCacheBean extends AbstractCacheBean<BrandProductRequest, BrandProductResponse, BrandProductRequestResponse> {
@Autowired
private AggBrandService aggBrandService;
private AggBrandListService aggBrandService;
@Autowired
private UserPersionalFactorComponent userComponent;
@Autowired
... ... @@ -72,9 +68,10 @@ public class AggBrandProductCacheBean extends AbstractCacheBean<BrandProductRequ
* @return
*/
private BrandProductResponse doRealRecall(BrandProductRequest brandProductRequest) {
BrandProductResponse response = new BrandProductResponse();
//1 获取个性化的和非个性化的brandId viewNum个
List<AggBrand> aggBrands = aggBrandService.aggBrands(brandProductRequest.getParamQueryFilter());
if (org.springframework.util.CollectionUtils.isEmpty(aggBrands)) {
if (aggBrands == null || aggBrands.isEmpty()) {
return null;
}
List<Integer> aggBrandIds = aggBrands.stream().map(AggBrand::getBrandId).collect(Collectors.toList());
... ... @@ -86,53 +83,64 @@ public class AggBrandProductCacheBean extends AbstractCacheBean<BrandProductRequ
if (userPersonalbrandIds.size() < brandProductRequest.getBrandCount()) {
aggBrandIds.removeAll(userPersonalbrandIds);
aggBrandIds = CollectionUtils.safeSubList(aggBrandIds, 0, brandProductRequest.getBrandCount() - userPersonalbrandIds.size());
userPersonalbrandIds.addAll(aggBrandIds);
}
//2、获取召回结果
List<BatchBrandProductRequestResponse> brandProductList = batchRecall(brandProductRequest, userPersonalbrandIds, aggBrandIds);
if (brandProductRequest.hasUidOrUdid()) {
doCalScoreAndSort(brandProductList, brandProductRequest.getUid(), brandProductRequest.getBrandCount());
List<BatchBrandProductRequestResponse> brandProductListList = batchRecall(brandProductRequest, userPersonalbrandIds);
Map<Integer, List<BrandProduct>> recallResultMap = new HashMap<>();
for (BatchBrandProductRequestResponse requestResponse : brandProductListList) {
if (requestResponse.getResponse() != null && !org.springframework.util.CollectionUtils.isEmpty(requestResponse.getResponse().getProductList())) {
Integer brandId = ((BrandHeatValueStrategy)requestResponse.getRequest().strategy()).getBrandId();
List<BrandProduct> brandProductList = requestResponse.getResponse().getProductList();
recallResultMap.put(brandId, brandProductList);
}
}
UserFeatureFactor userFeatureFactor = brandProductRequest.getUid() <= 0 ? null : getUserFeatureFactor(brandProductRequest.getUid());
doCalScoreAndSort(recallResultMap, userFeatureFactor);
//直接截取
List<BrandProduct> recallSknList = new ArrayList<>();
int productNum = brandProductRequest.getBrandProductCount();
for (Map.Entry<Integer, List<BrandProduct>> brandProductList : recallResultMap.entrySet()){
recallSknList.addAll(CollectionUtils.safeSubList(brandProductList.getValue(), 0, productNum));
}
if (!org.springframework.util.CollectionUtils.isEmpty(recallSknList)) {
doCalScoreAndSort(recallSknList, userFeatureFactor);
}
recallSknList = CollectionUtils.safeSubList(recallSknList, 0, brandProductRequest.getBrandCount());
response.setTotal(recallSknList.size());
response.setProductList(recallSknList);
return response;
}
return null;
private void doCalScoreAndSort(List<BrandProduct> productList, UserFeatureFactor userFeatureFactor) {
for (BrandProduct product : productList) {
double score = productFeatureFactorComponent.calProductFeatureFactor(userFeatureFactor, product.getProductFeatureFactor());
if (score == 0) {
score = (double)product.getHeatValue()/ 100;
}
product.setScore(score);
}
Collections.sort(productList, (o1, o2) -> o2.getScore().compareTo(o1.getScore()));
}
private void doCalScoreAndSort(Map<Integer, List<BrandProduct>> recallResultMap, UserFeatureFactor userFeatureFactor) {
//2、计算相关性
for (Map.Entry<Integer, List<BrandProduct>> brandProductList : recallResultMap.entrySet()){
if (brandProductList != null && !org.springframework.util.CollectionUtils.isEmpty(brandProductList.getValue())) {
List<BrandProduct> productList = brandProductList.getValue();
doCalScoreAndSort(productList, userFeatureFactor);
}
}
}
/**
* 粗排-按相关性计算得分,并按得分排序
*
* @param sknResultList
* @param uid
*/
private void doCalScoreAndSort(List<BatchBrandProductRequestResponse> sknResultList, int uid, int pageSize) {
private UserFeatureFactor getUserFeatureFactor(int uid) {
//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());
}
}
return new UserFeatureFactor(personalizedSearch);
}
private List<Integer> buildUserPersonalBrandIds(List<Integer> aggBrandIds, UserPersonalFactorRspNew userFactor, int count) {
List<Integer> result = new ArrayList<>();
double userBrandVectorNorm = Word2VectorCalculator.getVectorListNorm(userFactor.getBrandVectorW2v());
... ... @@ -156,21 +164,15 @@ public class AggBrandProductCacheBean extends AbstractCacheBean<BrandProductRequ
return result;
}
private List<BatchBrandProductRequestResponse> batchRecall(BrandProductRequest brandProductRequest, List<Integer> userPersonalbrandIds, List<Integer> aggBrandIds) {
List<Integer> brandIds = new ArrayList<>(userPersonalbrandIds);
brandIds.addAll(aggBrandIds);
private List<BatchBrandProductRequestResponse> batchRecall(BrandProductRequest brandProductRequest, List<Integer> brandIds) {
int brandProductCount = brandProductRequest.getBrandProductCount() + 20;
ParamQueryFilter paramQueryFilter = brandProductRequest.getParamQueryFilter();
List<RecallRequest> brandRequests = brandIds.stream().map(brandId -> {
IStrategy strategy = new BrandHeatValueStrategy(brandId, brandProductCount);
return new RecallRequest(paramQueryFilter, strategy);
}).collect(Collectors.toList());
List<BatchBrandProductRequestResponse> requestResponses = batchBrandProductCacheBean.batchRecallAndCache(brandRequests);
return requestResponses;
}
}
... ...
package com.yoho.search.service.scene.activity;
import com.yoho.search.models.SearchApiResult;
import com.yoho.search.service.helper.*;
import com.yoho.search.service.recall.models.common.ParamQueryFilter;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Service;
import java.util.*;
@Service
public class AggBrandProductListService implements ApplicationEventPublisherAware {
private static final Logger logger = LoggerFactory.getLogger(AggBrandProductListService.class);
private ApplicationEventPublisher publisher;
@Autowired
private AggBrandProductCacheBean brandProductCacheBean;
@Autowired
private SearchQueryHelper searchServiceHelepr;
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.publisher = applicationEventPublisher;
}
public SearchApiResult aggProductListByBrandV2(Map<String, String> paramMap) {
logger.info("[func=aggProductListByBrand][param={}][begin={}]", paramMap.toString(), System.currentTimeMillis());
// 1、获取商品总数量参数
int totalViewNum = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum"));
try {
BrandProductRequest brandProductRequest = buildBrandProductRequest(paramMap, totalViewNum);
if (brandProductRequest != null) {
BrandProductResponse response = brandProductCacheBean.queryBrandProductResult(brandProductRequest);
return new SearchApiResult().setMessage("agg productList by brand list").setData(response);
}
return new SearchApiResult().setMessage("agg productList by brand list").setData(null);
}catch (Exception e) {
System.out.println(e.getMessage());
}
return new SearchApiResult().setMessage("agg productList by brand list").setData(null);
}
private BrandProductRequest buildBrandProductRequest(Map<String, String> paramMap, int totalViewNum) {
try {
QueryBuilder query = searchServiceHelepr.constructQueryBuilder(paramMap);
BoolQueryBuilder filter = searchServiceHelepr.constructFilterBuilder(paramMap, null);
ParamQueryFilter paramQueryFilter = new ParamQueryFilter(query, filter);
int uid = MapUtils.getIntValue(paramMap, "uid", 0);
String udid = MapUtils.getString(paramMap, "udid", "");
int topHitsBucketSize = getTopHitsBucketSize(paramMap);
return new BrandProductRequest(paramQueryFilter, totalViewNum, topHitsBucketSize, uid, udid);
}catch (Exception e) {
}
return null;
}
/**
* 获取每个桶的商品数量
*
* @param paramMap
* @return
*/
private int getTopHitsBucketSize(Map<String, String> paramMap) {
try {
int count = Integer.parseInt(paramMap.get("aggTypeSize"));
return count <= 0 ? 1 : count;
} catch (Exception e) {
return 1;
}
}
}
... ... @@ -16,7 +16,12 @@ import com.yoho.search.common.SearchCommonService;
import com.yoho.search.service.helper.AggCommonHelper;
import com.yoho.search.service.helper.*;
import com.yoho.search.service.recall.beans.cache.SknReturnInfoCacheBean;
import com.yoho.search.service.recall.models.common.ParamQueryFilter;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilders;
... ... @@ -39,6 +44,7 @@ import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.util.*;
import java.util.stream.Collectors;
@Service
public class AggProductListService implements ApplicationEventPublisherAware {
... ... @@ -55,6 +61,12 @@ public class AggProductListService implements ApplicationEventPublisherAware {
private SearchParamHelper searchParamHelper;
@Autowired
private ProductListHelper productListHelper;
@Autowired
private SearchQueryHelper searchServiceHelepr;
@Autowired
private AggBrandProductCacheBean brandProductCacheBean;
@Autowired
private SknReturnInfoCacheBean sknReturnInfoCacheBean;
private ApplicationEventPublisher publisher;
... ... @@ -198,6 +210,53 @@ public class AggProductListService implements ApplicationEventPublisherAware {
}
public SearchApiResult aggProductListByBrandV2(Map<String, String> paramMap) {
logger.info("[func=aggProductListByBrand][param={}][begin={}]", paramMap.toString(), System.currentTimeMillis());
// 1、获取商品总数量参数
int totalViewNum = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum"));
try {
BrandProductRequest brandProductRequest = buildBrandProductRequest(paramMap, totalViewNum);
BrandProductResponse response = brandProductCacheBean.queryBrandProductResult(brandProductRequest);
JSONObject jsonObject = queryProductList(brandProductRequest, response, totalViewNum);
if (jsonObject == null) {
jsonObject = new JSONObject();
}
return new SearchApiResult().setMessage("agg productList by brand list").setData(jsonObject);
} catch (Exception e) {
logger.error("[func=getAggProductListJSONObject exception][param={}][Exception={}]", paramMap.toString(), e);
publisher.publishEvent(new SearchEvent(EventReportEnum.SEARCHCONTROLLER_AGG_PRODUCTLIST.getEventName(), EventReportEnum.SEARCHCONTROLLER_AGG_PRODUCTLIST
.getFunctionName(), EventReportEnum.SEARCHCONTROLLER_AGG_PRODUCTLIST.getMoudleName(), "exception", IgnoreSomeException.filterSomeException(e), null));
return null;
}
}
private JSONObject queryProductList(BrandProductRequest brandProductRequest, BrandProductResponse response, int totalViewNum) {
//3、获取召回结果的skn
List<Integer> productSknList = response.getProductList().stream().map(BrandProduct::getProductSkn).collect(Collectors.toList());
//4、获取商品的返回信息
long begin = System.currentTimeMillis();
List<Map<String, Object>> productList = sknReturnInfoCacheBean.queryProductListBySkn(productSknList, productSknList.size());
logger.info("AggBrandProductListService.queryProductListBySkn,cost is [{}]", System.currentTimeMillis() - begin);
JSONObject result = new JSONObject();
result.put("total", productList.size());
result.put("page", 1);
result.put("page_size", totalViewNum);
result.put("page_total", 1);
result.put("product_list", productList);
return result;
}
private BrandProductRequest buildBrandProductRequest(Map<String, String> paramMap, int totalViewNum) throws Exception{
QueryBuilder query = searchServiceHelepr.constructQueryBuilder(paramMap);
BoolQueryBuilder filter = searchServiceHelepr.constructFilterBuilder(paramMap, null);
ParamQueryFilter paramQueryFilter = new ParamQueryFilter(query, filter);
int uid = MapUtils.getIntValue(paramMap, "uid", 0);
String udid = MapUtils.getString(paramMap, "udid", "");
int aggTypeSize = getTopHitsBucketSize(paramMap);
return new BrandProductRequest(paramQueryFilter, totalViewNum, aggTypeSize, uid, udid);
}
/**
* @param firstAggField
* 【父聚合的字段名称】
... ...
... ... @@ -8,14 +8,15 @@ 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.scene.shopbrand.AggProductResponse;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.util.*;
@Component
public class BatchBrandProductCacheBean extends AbstractCacheBean<RecallRequest, AggProductResponse, BatchBrandProductRequestResponse> {
public class BatchBrandProductCacheBean extends AbstractCacheBean<RecallRequest, BatchBrandProductResponse, BatchBrandProductRequestResponse> {
@Autowired
... ... @@ -45,12 +46,12 @@ public class BatchBrandProductCacheBean extends AbstractCacheBean<RecallRequest,
}
@Override
protected Map<RecallRequest, AggProductResponse> queryMissCacheRequestResults(List<BatchBrandProductRequestResponse> missCacheRequests) {
Map<RecallRequest, AggProductResponse> results = new HashMap<>();
protected Map<RecallRequest, BatchBrandProductResponse> queryMissCacheRequestResults(List<BatchBrandProductRequestResponse> missCacheRequests) {
Map<RecallRequest, BatchBrandProductResponse> results = new HashMap<>();
if (missCacheRequests == null || missCacheRequests.isEmpty()) {
return results;
}
List<String> includeFields = Arrays.asList(ProductIndexEsField.productId,ProductIndexEsField.productSkn,ProductIndexEsField.brandId,ProductIndexEsField.productFeatureFactor,ProductIndexEsField.heatValue);
List<String> includeFields = Arrays.asList(ProductIndexEsField.productSkn,ProductIndexEsField.brandId,ProductIndexEsField.productFeatureFactor,ProductIndexEsField.heatValue);
List<SearchParam> searchParams = new ArrayList<>();
for (BatchBrandProductRequestResponse requestResponse : missCacheRequests) {
SearchParam searchParam = requestResponse.getRequest().searchParam();
... ... @@ -60,11 +61,24 @@ public class BatchBrandProductCacheBean extends AbstractCacheBean<RecallRequest,
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();
AggProductResponse response = new AggProductResponse();
BatchBrandProductResponse response = new BatchBrandProductResponse();
SearchResult searchResult = searchResults.get(i);
response.setProduct_list(searchResult.getResultList());
response.setCount((int)searchResult.getTotal());
if (searchResult != null) {
response.setCount((int)searchResult.getTotal());
if (!CollectionUtils.isEmpty(searchResult.getResultList())) {
List<BrandProduct> productSknList = new ArrayList<>();
for (Map<String, Object> productInfo : searchResult.getResultList()) {
Integer productSkn = MapUtils.getInteger(productInfo, ProductIndexEsField.productSkn, 0);
Integer brandId = MapUtils.getInteger(productInfo, ProductIndexEsField.brandId, 0);
String productFeatureFactor = MapUtils.getString(productInfo, ProductIndexEsField.productFeatureFactor, "");
Integer heatValue = MapUtils.getInteger(productInfo, ProductIndexEsField.heatValue, 0);
productSknList.add(new BrandProduct(productSkn, brandId, productFeatureFactor, heatValue));
}
response.setProductList(productSknList);
}
}
results.put(request, response);
}
return results;
... ...
package com.yoho.search.service.scene.activity;
import com.alibaba.fastjson.JSON;
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.scene.shopbrand.AggProductResponse;
public class BatchBrandProductRequestResponse extends AbstractCacheRequestResponse<RecallRequest, AggProductResponse> {
public class BatchBrandProductRequestResponse extends AbstractCacheRequestResponse<RecallRequest, BatchBrandProductResponse> {
public BatchBrandProductRequestResponse(RecallRequest request) {
super(request);
}
@Override
public Transfer<String, AggProductResponse> getToResponseTransfer() {
return null;
public Transfer<String, BatchBrandProductResponse> getToResponseTransfer() {
return (v) -> JSON.parseObject(v, BatchBrandProductResponse.class);
}
@Override
public Transfer<AggProductResponse, String> getFromResponseTransfer() {
return null;
public Transfer<BatchBrandProductResponse, String> getFromResponseTransfer() {
return (v) -> JSON.toJSONString(v);
}
}
... ...
package com.yoho.search.service.scene.activity;
import java.util.List;
public class BatchBrandProductResponse {
private Integer count;
private List<BrandProduct> productList;
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
public List<BrandProduct> getProductList() {
return productList;
}
public void setProductList(List<BrandProduct> productList) {
this.productList = productList;
}
}
... ...
... ... @@ -12,19 +12,11 @@ import org.elasticsearch.search.sort.SortBuilder;
public class BrandHeatValueStrategy extends CommonHeatValueStrategy {
protected Integer brandId;
public BrandHeatValueStrategy(int size, Integer brandId) {
public BrandHeatValueStrategy(Integer brandId, int size) {
super(size);
this.brandId = brandId;
}
//todo
@Override
public StringBuilder defaultStrategyKey() {
return null;
}
@Override
public StrategyEnum strategtEnum() {
return super.strategtEnum();
... ... @@ -52,9 +44,17 @@ public class BrandHeatValueStrategy extends CommonHeatValueStrategy {
return CacheTimeConstants.CACHE_60_MINUTE;
}
//todo
@Override
public String strategyCacheKey() {
return super.strategyCacheKey();
StringBuilder sb = new StringBuilder();
sb.append("BRAND_HEAT_VALUE");
sb.append(brandId);
sb.append(this.cacheTimeInMinute());
sb.append(this.size());
return sb.toString();
}
public Integer getBrandId() {
return brandId;
}
}
... ...
package com.yoho.search.service.scene.activity;
public class BrandProduct {
private Integer productSkn;
private Integer brandId;
private String productFeatureFactor;
private Integer heatValue;
private Double score;
public BrandProduct() {
}
public BrandProduct(Integer productSkn, Integer brandId, String productFeatureFactor, Integer heatValue) {
this.productSkn = productSkn;
this.brandId = brandId;
this.productFeatureFactor = productFeatureFactor;
this.heatValue = heatValue;
}
public Integer getProductSkn() {
return productSkn;
}
public void setProductSkn(Integer productSkn) {
this.productSkn = productSkn;
}
public Integer getBrandId() {
return brandId;
}
public void setBrandId(Integer brandId) {
this.brandId = brandId;
}
public String getProductFeatureFactor() {
return productFeatureFactor;
}
public void setProductFeatureFactor(String productFeatureFactor) {
this.productFeatureFactor = productFeatureFactor;
}
public Integer getHeatValue() {
return heatValue;
}
public void setHeatValue(Integer heatValue) {
this.heatValue = heatValue;
}
public Double getScore() {
return score;
}
public void setScore(Double score) {
this.score = score;
}
}
... ...
... ... @@ -60,7 +60,7 @@ public class BrandProductRequest implements ICacheRequest {
@Override
public int cacheTimeInMinute() {
return CacheTimeConstants.CACHE_10_MINUTE;
return CacheTimeConstants.CACHE_30_MINUTE;
}
public ParamQueryFilter getParamQueryFilter() {
... ...
... ... @@ -6,7 +6,7 @@ import java.util.Map;
public class BrandProductResponse {
private long total;
private List<BrandProductInfo> brandProductInfos;
private List<BrandProduct> productList;
public long getTotal() {
... ... @@ -17,44 +17,12 @@ public class BrandProductResponse {
this.total = total;
}
public List<BrandProductInfo> getBrandProductInfos() {
return brandProductInfos;
}
public void setBrandProductInfos(List<BrandProductInfo> brandProductInfos) {
this.brandProductInfos = brandProductInfos;
public List<BrandProduct> getProductList() {
return productList;
}
public static class BrandProductInfo {
private Integer brandId;
private List<Integer> productSknList;
private String brandIdType;
public Integer getBrandId() {
return brandId;
}
public void setBrandId(Integer brandId) {
this.brandId = brandId;
}
public List<Integer> getProductSknList() {
return productSknList;
}
public void setProductSknList(List<Integer> productSknList) {
this.productSknList = productSknList;
}
public String getBrandIdType() {
return brandIdType;
}
public void setBrandIdType(String brandIdType) {
this.brandIdType = brandIdType;
}
public void setProductList(List<BrandProduct> productList) {
this.productList = productList;
}
}
... ...
... ... @@ -18,7 +18,7 @@ import java.util.List;
import java.util.Map;
@Service
public class ShopProductCacheBean extends AbstractCacheBean<ShopProductRequest, AggProductResponse, ShopProductRequestResponse> {
public class ShopProductCacheBean extends AbstractCacheBean<ShopProductRequest, ShopProductResponse, ShopProductRequestResponse> {
@Autowired
private SearchCommonService searchCommonService;
... ... @@ -28,13 +28,13 @@ public class ShopProductCacheBean extends AbstractCacheBean<ShopProductRequest,
private ProductListHelper productListHelper;
public Map<String, AggProductResponse> queryShopProductList(List<ShopProductRequest> requests) {
public Map<String, ShopProductResponse> 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, AggProductResponse> responseMap = new HashMap<>();
Map<String, ShopProductResponse> 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, AggProductResponse> queryMissCacheRequestResults(List<ShopProductRequestResponse> missCacheRequests) {
Map<ShopProductRequest, AggProductResponse> results = new HashMap<>();
protected Map<ShopProductRequest, ShopProductResponse> queryMissCacheRequestResults(List<ShopProductRequestResponse> missCacheRequests) {
Map<ShopProductRequest, ShopProductResponse> 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();
AggProductResponse response = new AggProductResponse();
ShopProductResponse response = new ShopProductResponse();
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, AggProductResponse> {
public class ShopProductRequestResponse extends AbstractCacheRequestResponse<ShopProductRequest, ShopProductResponse> {
public ShopProductRequestResponse(ShopProductRequest request) {
super(request);
}
@Override
public Transfer<String, AggProductResponse> getToResponseTransfer() {
return (v) -> JSON.parseObject(v, AggProductResponse.class);
public Transfer<String, ShopProductResponse> getToResponseTransfer() {
return (v) -> JSON.parseObject(v, ShopProductResponse.class);
}
@Override
public Transfer<AggProductResponse, String> getFromResponseTransfer() {
public Transfer<ShopProductResponse, String> getFromResponseTransfer() {
return (v) -> JSON.toJSONString(v);
}
}
... ...
package com.yoho.search.service.scene.shopbrand;
import java.util.List;
import java.util.Map;
public class ShopProductResponse {
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;
}
}
... ...
... ... @@ -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, AggProductResponse> responseMap = shopProductCacheBean.queryShopProductList(requests);
Map<String, ShopProductResponse> responseMap = shopProductCacheBean.queryShopProductList(requests);
// 3、返回生成结果
Integer total = responseMap.values().stream().collect(Collectors.summingInt(AggProductResponse::getCount));
Integer total = responseMap.values().stream().collect(Collectors.summingInt(ShopProductResponse::getCount));
JSONObject realResult = new JSONObject();
realResult.put("total", total);
realResult.put("shops", responseMap);
... ...