Authored by hugufei

类重命名

package com.yoho.search.recall.scene;
import com.alibaba.fastjson.JSONObject;
import com.yoho.search.base.utils.CollectionUtils;
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.beans.cache.QueryRecallResultCacheBean;
import com.yoho.search.recall.scene.beans.cache.QueryProductInfoCacheBean;
import com.yoho.search.recall.scene.beans.helper.SortBuilderHelper;
import com.yoho.search.recall.scene.models.*;
import com.yoho.search.recall.scene.beans.builder.RecallParamsBuilder;
import com.yoho.search.recall.scene.models.RecallParams;
import com.yoho.search.service.base.SearchCommonService;
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.Arrays;
import java.util.List;
import java.util.Map;
@Component
public class NewSceneRecallService {
private static final Logger logger = LoggerFactory.getLogger(NewSceneRecallService.class);
@Autowired
private RecallParamsBuilder recallParamsBuilder;
@Autowired
private SearchCommonHelper searchCommonHelper;
@Autowired
private SearchCommonService searchCommonService;
@Autowired
private QueryRecallResultCacheBean queryRecallResultCacheBean;
@Autowired
private QueryProductInfoCacheBean queryProductInfoCacheBean;
public SearchApiResult sceneRecall(Map<String, String> paramMap) {
try {
//1、分页参数验证
int page = MapUtils.getIntValue(paramMap, "page", 1);
int pageSize = MapUtils.getIntValue(paramMap, "viewNum", 20);
if (page < 1 || pageSize < 0 || page * pageSize > 1000000) {
return new SearchApiResult().setCode(400).setMessage("分页参数不合法");
}
//2、构造召回相关参数
RecallParams recallParams = recallParamsBuilder.buildRecallParams(paramMap,pageSize);
//3、执行召回
RecallResult recallResult = queryRecallResultCacheBean.queryRecallResult(recallParams);
//4、根据召回结果查询商品信息
List<Map<String, Object>> productList = this.queryProductList(recallParams, recallResult,page,pageSize);
//5、构造返回结果
JSONObject dataMap = new JSONObject();
dataMap.put("total", recallResult.getTotal());
dataMap.put("page", page);
dataMap.put("page_size", recallParams.getPageSize());
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);
return new SearchApiResult().setData(null).setCode(500).setMessage("Exception");
}
}
private List<Map<String, Object>> queryProductList(RecallParams recallParams, RecallResult recallResult, int page, int pageSize) {
//1、判断当前页码是否在召回的页码里
int recallTotalPage = recallResult.getRecallTotalPage();
//2、在召回结果中则直接根据skn去查询
List<Integer> productSkns = null;
if(page<=recallTotalPage){
productSkns = CollectionUtils.safeSubList(recallResult.getSknList(),(page-1)*pageSize,page*pageSize);
}else{
//3、过滤召回的skn,修改真实页码,执行查询
productSkns = this.queryProductSknByFilterSkn(recallParams, recallResult.getSknList(), recallTotalPage-page, pageSize);
}
return queryProductInfoCacheBean.queryProductListBySkn(productSkns,productSkns.size());
}
/**
* 过滤掉已召回的skn,并按人气排序
* @param notProductSkns
* @param realPage
* @param pageSize
* @return
*/
private List<Integer> queryProductSknByFilterSkn(RecallParams recallParams, List<Integer> notProductSkns, int realPage, int pageSize){
SearchParam searchParam = new SearchParam();
//1、设置query
searchParam.setQuery(recallParams.getParamQueryFilter().getParamQuery());
//2、设置filter
BoolQueryBuilder filter = recallParams.getParamQueryFilter().getParamFilter();
filter.mustNot(QueryBuilders.termsQuery(ProductIndexEsField.productSkn,notProductSkns));
searchParam.setFiter(filter);
//3、设置分页参数
searchParam.setOffset((realPage-1)*pageSize);
searchParam.setSize(pageSize);
//4、设置IncludeFields
searchParam.setIncludeFields(Arrays.asList(ProductIndexEsField.productSkn));
//5、设置排序
List<SortBuilder<?>> sortBuilders = new ArrayList<>();
sortBuilders.add(SortBuilderHelper.getSevendayMoneyDescSort());
sortBuilders.add(SortBuilderHelper.getIdDescSort());
searchParam.setSortBuilders(sortBuilders);
SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_PRODUCT_BASE_INDEX,searchParam);
List<Integer> results = new ArrayList<>();
for (Map<String, Object> product : searchResult.getResultList()) {
results.add(MapUtils.getIntValue(product,ProductIndexEsField.productSkn,0));
}
return results;
}
}
... ...
package com.yoho.search.recall.scene.beans.builder;
import com.yoho.search.base.utils.CollectionUtils;
import com.yoho.search.core.personalized.models.UserPersionalFactorRsp;
import com.yoho.search.core.personalized.models.UserPersonalFactorRsp;
import com.yoho.search.recall.scene.models.RecallParams;
import com.yoho.search.recall.scene.models.RecallMergerResult;
import com.yoho.search.recall.scene.models.RecallResult;
import com.yoho.search.recall.scene.models.PagePersionalFactor;
import com.yoho.search.recall.scene.beans.strategy.StrategyNameEnum;
import com.yoho.search.service.base.ProductListSortKey;
import com.yoho.search.service.base.ProductListSortService;
... ... @@ -23,7 +22,7 @@ public class RecallResultBuilder {
@Autowired
private ProductListSortService productListSortService;
public RecallResult builderRecallResult(RecallMergerResult recallMergerResult, RecallParams param, UserPersionalFactorRsp userPersionalFactorRsp){
public RecallResult builderRecallResult(RecallMergerResult recallMergerResult, RecallParams param, UserPersonalFactorRsp userPersonalFactorRsp){
//1、构造结果中的分页信息
final long total = recallMergerResult.getTotal();
... ... @@ -31,7 +30,7 @@ public class RecallResultBuilder {
List<RecallMergerResult.SknResult> sknResultList = recallMergerResult.getSknList();
//3、计算得分
this.doCalScore(sknResultList, userPersionalFactorRsp);
this.doCalScore(sknResultList, userPersonalFactorRsp);
//4、排序
Collections.sort(sknResultList, new Comparator<RecallMergerResult.SknResult>() {
... ... @@ -72,8 +71,8 @@ public class RecallResultBuilder {
}
private void doCalScore(List<RecallMergerResult.SknResult> sknResultList, UserPersionalFactorRsp userPersionalFactorRsp) {
String vector = userPersionalFactorRsp.getVector();
private void doCalScore(List<RecallMergerResult.SknResult> sknResultList, UserPersonalFactorRsp userPersonalFactorRsp) {
String vector = userPersonalFactorRsp.getVector();
for (RecallMergerResult.SknResult sknResult : sknResultList) {
if (sknResult.getRequestTypes().contains(StrategyNameEnum.FIRST_PRODUCT_SKN.name())) {
sknResult.setScore(10000d);// firstSkn排第一个
... ...
package com.yoho.search.recall.scene.beans.cache;
import com.yoho.search.core.personalized.models.UserPersionalFactorRsp;
import com.yoho.search.core.personalized.models.UserPersonalFactorRsp;
import com.yoho.search.recall.scene.beans.builder.*;
import com.yoho.search.recall.scene.beans.persional.QueryUserPersionalFactorBean;
import com.yoho.search.recall.scene.models.*;
... ... @@ -68,35 +68,35 @@ public class QueryRecallResultCacheBean extends AbstractCacheBean<RecallParams,R
*/
private RecallResult doRealRecall(RecallParams param) {
//1、获取个性化因子
UserPersionalFactorRsp userPersionalFactorRsp = queryUserPersionalFactorBean.queryPersionalFactor(param);
UserPersonalFactorRsp userPersonalFactorRsp = queryUserPersionalFactorBean.queryPersionalFactor(param);
//2、构造请求
List<RecallRequest> batchRequests = this.buildBatchRequests(param, userPersionalFactorRsp);
List<RecallRequest> batchRequests = this.buildBatchRequests(param, userPersonalFactorRsp);
//3、批量召回
List<CacheRecallRequestRecallResponse> requestResponses = batchRecallCacheBean.batchRecallAndCache(batchRequests);
//4、获取skn列表[去重]
RecallMergerResult recallMergerResult = recallMergerResultBuilder.buildRecallResponseBatch(requestResponses);
//5、构造真实结果[排序,截取skn]
RecallResult recallResult = recallResultBuilder.builderRecallResult(recallMergerResult, param, userPersionalFactorRsp);
RecallResult recallResult = recallResultBuilder.builderRecallResult(recallMergerResult, param, userPersonalFactorRsp);
return recallResult;
}
/**
* 批量构造请求
* @param param
* @param userPersionalFactorRsp
* @param userPersonalFactorRsp
* @return
*/
private List<RecallRequest> buildBatchRequests(RecallParams param, UserPersionalFactorRsp userPersionalFactorRsp) {
private List<RecallRequest> buildBatchRequests(RecallParams param, UserPersonalFactorRsp userPersonalFactorRsp) {
//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(), userPersionalFactorRsp.getBrandIds());
List<RecallRequest> brandRequests = brandRequestBuilder.buildBrandRecallRequests(param.getParamQueryFilter(), userPersonalFactorRsp.getBrandIds());
allRequests.addAll(brandRequests);
//5、构建个性化品牌的召回请求
List<RecallRequest> sortPriceRequests = sortPriceRequestBuilder.buildSortPriceRecallRequests(param.getParamQueryFilter(), userPersionalFactorRsp.getSortPriceAreas());
List<RecallRequest> sortPriceRequests = sortPriceRequestBuilder.buildSortPriceRecallRequests(param.getParamQueryFilter(), userPersonalFactorRsp.getSortPriceAreas());
allRequests.addAll(sortPriceRequests);
return allRequests;
}
... ...
... ... @@ -7,7 +7,6 @@ import com.yoho.search.common.cache.model.SearchCache;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.core.es.model.SearchResult;
import com.yoho.search.core.personalized.models.SortPriceArea;
import com.yoho.search.core.personalized.models.UserPersionalFactorRsp;
import com.yoho.search.recall.scene.models.ParamQueryFilter;
import com.yoho.search.recall.scene.models.PagePersionalFactor;
import com.yoho.search.service.base.SearchCacheService;
... ...
... ... @@ -3,7 +3,7 @@ package com.yoho.search.recall.scene.beans.persional;
import com.yoho.search.base.utils.CollectionUtils;
import com.yoho.search.base.utils.Transfer;
import com.yoho.search.core.personalized.models.SortPriceArea;
import com.yoho.search.core.personalized.models.UserPersionalFactorRsp;
import com.yoho.search.core.personalized.models.UserPersonalFactorRsp;
import com.yoho.search.recall.scene.models.PagePersionalFactor;
import com.yoho.search.recall.scene.models.RecallParams;
import org.slf4j.Logger;
... ... @@ -31,19 +31,19 @@ public class QueryUserPersionalFactorBean {
* @param recallParams
* @return
*/
public UserPersionalFactorRsp queryPersionalFactor(RecallParams recallParams) {
public UserPersonalFactorRsp queryPersionalFactor(RecallParams recallParams) {
try {
//1、获取页面上的个性化因子
PagePersionalFactor pageFactor = pageComponent.queryPagePersionalFactor(recallParams.getParamQueryFilter());
//2、获取用户的个性化因子
UserPersionalFactorRsp userFactor = userComponent.queryUserPersionalFactor(recallParams.getUid(), recallParams.getUdid());
UserPersonalFactorRsp userFactor = userComponent.queryUserPersionalFactor(recallParams.getUid(), recallParams.getUdid());
//3、join获取最终的结果
List<Integer> brandIds = this.innerJoin(pageFactor.getBrandIds(),userFactor.getBrandIds(),brandIdMapkeyTransfer,10);
List<SortPriceArea> sortPriceAreas = this.innerJoin(pageFactor.getSortPriceAreas(),userFactor.getSortPriceAreas(),sortPriceMapkeyTransfer,5);
return new UserPersionalFactorRsp(brandIds, sortPriceAreas,userFactor.getVector());
return new UserPersonalFactorRsp(brandIds, sortPriceAreas,userFactor.getVector());
}catch (Exception e){
logger.error(e.getMessage());
return new UserPersionalFactorRsp(new ArrayList<>(),new ArrayList<>(),"");
return new UserPersonalFactorRsp(new ArrayList<>(),new ArrayList<>(),"");
}
}
... ...
package com.yoho.search.recall.scene.beans.persional;
import com.yoho.search.core.personalized.models.SortPriceArea;
import com.yoho.search.core.personalized.models.UserPersionalFactorRsp;
import com.yoho.search.core.personalized.models.UserPersonalFactorRsp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
... ... @@ -29,11 +29,11 @@ class UserPersionalFactorComponent {
* @param udid
* @return
*/
public UserPersionalFactorRsp queryUserPersionalFactor(int uid, String udid) {
public UserPersonalFactorRsp queryUserPersionalFactor(int uid, String udid) {
//1、调大数据服务查询用户个性化因子
UserPersionalFactorRsp userPersionalFactorRsp = this.queryUserPersionalFactorRsp(uid,udid);
if(userPersionalFactorRsp!=null){
return userPersionalFactorRsp;
UserPersonalFactorRsp userPersonalFactorRsp = this.queryUserPersionalFactorRsp(uid,udid);
if(userPersonalFactorRsp !=null){
return userPersonalFactorRsp;
}
//2、查询失败则随机生成-测试用
List<Integer> brandIds = new ArrayList<Integer>();
... ... @@ -45,10 +45,10 @@ class UserPersionalFactorComponent {
sortPriceArea.add(new SortPriceArea((int) (Math.random() * 1000), (int) (Math.random() *10)));
}
String factor = "";
return new UserPersionalFactorRsp(brandIds, sortPriceArea,factor);
return new UserPersonalFactorRsp(brandIds, sortPriceArea,factor);
}
private UserPersionalFactorRsp queryUserPersionalFactorRsp(int uid, String udid){
private UserPersonalFactorRsp queryUserPersionalFactorRsp(int uid, String udid){
// try {
// UserPersionalFactorReq userPersionalFactorReq = new UserPersionalFactorReq(uid,udid);
// return serviceCaller.call(SERVICE_NAME,userPersionalFactorReq,UserPersionalFactorRsp.class,timeOut);
... ...
package com.yoho.search.recall.scene.models;
import com.yoho.search.core.personalized.models.SortPriceArea;
import com.yoho.search.core.personalized.models.UserPersionalFactorRsp;
import java.io.Serializable;
import java.util.List;
... ...
... ... @@ -2,7 +2,7 @@ package com.yoho.search.restapi;
import com.yoho.search.common.utils.HttpServletRequestUtils;
import com.yoho.search.models.SearchApiResult;
import com.yoho.search.recall.scene.SceneRecallService;
import com.yoho.search.recall.scene.NewSceneRecallService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
... ... @@ -16,7 +16,7 @@ import java.util.Map;
public class SceneRecallTestController {
@Autowired
private SceneRecallService sceneRecallService;
private NewSceneRecallService newSceneRecallService;
/**
* 获取品牌列表[不包含全球购]
... ... @@ -28,6 +28,6 @@ public class SceneRecallTestController {
@ResponseBody
public SearchApiResult sceneRecall(HttpServletRequest request) {
Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
return sceneRecallService.sceneRecall(paramMap);
return newSceneRecallService.sceneRecall(paramMap);
}
}
... ...
... ... @@ -17,7 +17,7 @@ import com.alibaba.fastjson.JSONObject;
import com.yoho.search.common.downgrade.persional.PersionalRateLimit;
import com.yoho.search.models.PromotionConditions;
import com.yoho.search.models.SearchApiResult;
import com.yoho.search.service.scene.PromotionSceneService;
import com.yoho.search.service.scene.promotion.PromotionSceneService;
/**
* 商品详情页的促销列表接口
... ...
... ... @@ -2,6 +2,7 @@ package com.yoho.search.service.scene.common;
import java.util.Map;
import com.yoho.search.recall.scene.NewSceneRecallService;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
... ... @@ -18,6 +19,8 @@ public class CommonSceneProductListService {
private SearchDynamicConfigService searchDynamicConfigService;
@Autowired
private ProductListServiceHelper productListServiceHelper;
@Autowired
private NewSceneRecallService newSceneRecallService;
/**
* 各商品列表的入口
... ...
package com.yoho.search.service.scene.promotion;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
import org.elasticsearch.search.aggregations.Aggregation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yoho.search.base.utils.ConvertUtils;
import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.base.utils.ProductIndexEsField;
import com.yoho.search.common.cache.SearchCacheMatchLogger;
import com.yoho.search.common.cache.model.SearchCache;
import com.yoho.search.core.es.agg.IAggregation;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.core.es.model.SearchResult;
import com.yoho.search.models.PromotionConditions;
import com.yoho.search.models.PromotionParam;
import com.yoho.search.models.SearchApiResult;
import com.yoho.search.service.aggregations.impls.AggregationFactoryService;
import com.yoho.search.service.base.SearchCommonService;
import com.yoho.search.service.base.index.ProductIndexBaseService;
import com.yoho.search.service.helper.SearchAfterCacheHelper;
import com.yoho.search.service.helper.SearchParamHelper;
import com.yoho.search.service.helper.SearchSortHelper;
import com.yoho.search.service.scene.common.AbstractCacheAbleService;
import com.yoho.search.service.scene.common.SceneSelectionsService;
import com.yoho.search.service.service.IBrandService;
import com.yoho.search.service.service.IDiscountService;
import com.yoho.search.service.service.ISearchSortGroupService;
import com.yoho.search.service.service.ISelectionsForApp;
import com.yoho.search.service.service.ISelectionsForPc;
@Service
public class PromotionSceneService extends AbstractCacheAbleService {
private static Logger logger = LoggerFactory.getLogger(PromotionSceneService.class);
@Autowired
private SearchSortHelper searchSortHelper;
@Autowired
private SearchCommonService searchCommonService;
@Autowired
private AggregationFactoryService aggregationFactoryService;
@Autowired
private ISelectionsForApp selectionsWithOutAdvanceService;
@Autowired
private SearchAfterCacheHelper searchAfterCacheService;
@Autowired
private ISelectionsForPc selectionsForPc;
@Autowired
private IBrandService brandService;
@Autowired
private ISearchSortGroupService searchSortGroupService;
@Autowired
private IDiscountService discountService;
@Autowired
private ProductIndexBaseService productIndexBaseService;
@Autowired
private SearchParamHelper searchParamHelper;
@Autowired
private SceneSelectionsService sceneSelectionsService;
@Override
public SearchCache getSearchCache() {
return searchCacheFactory.getPromotionSearchCache();
}
private BoolQueryBuilder genConditionFilter(PromotionParam param, boolean isAnd) {
BoolQueryBuilder conditionFilter = QueryBuilders.boolQuery();
// 判断品牌
if (StringUtils.isNotBlank(param.getBrand())) {
int[] brandids = ConvertUtils.stringToIntArray(param.getBrand(), ",");
BoolQueryBuilder brandFilter = QueryBuilders.boolQuery().must(QueryBuilders.termsQuery(ProductIndexEsField.brandId, brandids));
if (isAnd) {
conditionFilter.must(brandFilter);
} else {
conditionFilter.should(brandFilter);
}
}
// 判断不包含品牌
if (StringUtils.isNotBlank(param.getNotBrandId())) {
int[] notBrandids = ConvertUtils.stringToIntArray(param.getNotBrandId(), ",");
BoolQueryBuilder notBrandFilter = QueryBuilders.boolQuery();
notBrandFilter.mustNot(QueryBuilders.termsQuery(ProductIndexEsField.brandId, notBrandids));
if (isAnd) {
conditionFilter.must(notBrandFilter);
} else {
conditionFilter.should(notBrandFilter);
}
}
// 判断品类
if (StringUtils.isNotBlank(param.getSort())) {
int[] sortIds = ConvertUtils.stringToIntArray(param.getSort(), ",");
BoolQueryBuilder sortFilter = QueryBuilders.boolQuery();
sortFilter.should(QueryBuilders.termsQuery(ProductIndexEsField.maxSortId, sortIds));
sortFilter.should(QueryBuilders.termsQuery(ProductIndexEsField.middleSortId, sortIds));
sortFilter.should(QueryBuilders.termsQuery(ProductIndexEsField.smallSortId, sortIds));
if (isAnd) {
conditionFilter.must(QueryBuilders.boolQuery().must(sortFilter));
} else {
conditionFilter.should(QueryBuilders.boolQuery().must(sortFilter));
}
}
// 判断非品类
if (StringUtils.isNotBlank(param.getNotSort())) {
int[] notSortIds = ConvertUtils.stringToIntArray(param.getNotSort(), ",");
BoolQueryBuilder notSortFilter = QueryBuilders.boolQuery();
notSortFilter.mustNot(QueryBuilders.termsQuery(ProductIndexEsField.maxSortId, notSortIds));
notSortFilter.mustNot(QueryBuilders.termsQuery(ProductIndexEsField.middleSortId, notSortIds));
notSortFilter.mustNot(QueryBuilders.termsQuery(ProductIndexEsField.smallSortId, notSortIds));
if (isAnd) {
conditionFilter.must(notSortFilter);
} else {
conditionFilter.should(notSortFilter);
}
}
// 判断promotion
if (StringUtils.isNotBlank(param.getPromotion())) {
int[] promotionIds = ConvertUtils.stringToIntArray(param.getPromotion(), ",");
BoolQueryBuilder promotionFilter = QueryBuilders.boolQuery();
promotionFilter.must(QueryBuilders.termsQuery(ProductIndexEsField.ispromotion, promotionIds));
if (isAnd) {
conditionFilter.must(promotionFilter);
} else {
conditionFilter.should(promotionFilter);
}
}
// 判断非promotion
if (StringUtils.isNotBlank(param.getNotPromotion())) {
int[] notPromotionIds = ConvertUtils.stringToIntArray(param.getNotPromotion(), ",");
BoolQueryBuilder notPromotionFilter = QueryBuilders.boolQuery();
notPromotionFilter.mustNot(QueryBuilders.termsQuery(ProductIndexEsField.ispromotion, notPromotionIds));
if (isAnd) {
conditionFilter.must(notPromotionFilter);
} else {
conditionFilter.should(notPromotionFilter);
}
}
// 判断skn
if (StringUtils.isNotBlank(param.getProductSknStr())) {
BoolQueryBuilder sknFilter = QueryBuilders.boolQuery();
sknFilter.must(QueryBuilders.termsQuery(ProductIndexEsField.productSkn, param.getProductSknStr().split(",")));
if (isAnd) {
conditionFilter.must(sknFilter);
} else {
conditionFilter.should(sknFilter);
}
}
// 判断 非skn
if (StringUtils.isNotBlank(param.getNotProductSkn())) {
BoolQueryBuilder sknFilter = QueryBuilders.boolQuery();
sknFilter.mustNot(QueryBuilders.termsQuery(ProductIndexEsField.productSkn, param.getNotProductSkn().split(",")));
if (isAnd) {
conditionFilter.must(sknFilter);
} else {
conditionFilter.should(sknFilter);
}
}
return conditionFilter;
}
/**
* 根据促销列表参数生成过滤条件
*
* @return
*/
public BoolQueryBuilder getMustFilterByPromotion(PromotionConditions promotionConditions) {
if (promotionConditions == null) {
return null;
}
// 生成每种condition的filter列表
List<BoolQueryBuilder> conditionsFilter = new ArrayList<BoolQueryBuilder>();
List<PromotionParam> promotionParams = promotionConditions.getPromotionParams();
for (PromotionParam promotionParam : promotionParams) {
boolean isAnd = "any".equals(promotionParam.getAggregator()) ? false : true;
BoolQueryBuilder conditionFilter = this.genConditionFilter(promotionParam, isAnd);
conditionsFilter.add(conditionFilter);
}
// 处理总结果
String aggregator = promotionConditions.getAggregator();
boolean isTotalAnd = "any".equals(aggregator) ? false : true;
BoolQueryBuilder allBuilder = QueryBuilders.boolQuery();
for (BoolQueryBuilder conditionFilter : conditionsFilter) {
if (isTotalAnd) {
allBuilder.must(conditionFilter);
} else {
allBuilder.should(conditionFilter);
}
}
return allBuilder;
}
public SearchApiResult list(PromotionConditions promotionConditions, Map<String, String> paramMap) throws Exception {
// 1、获取mustFilterByPromotion
BoolQueryBuilder mustFilterByPromotion = this.getMustFilterByPromotion(promotionConditions);
// 2、构造带filter和Query的SearchParam
SearchParam searchParam = searchParamHelper.buildWithPersional(paramMap, true, mustFilterByPromotion);
// 3、设置聚合条件
searchParam.setAggregationBuilders(null);
// 4、设置分页参数
int pageSize = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum"));
int page = StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page"));
if (page < 1 || pageSize < 0) {
throw new IllegalArgumentException("分页参数不合法");
}
if (pageSize > 100) {
pageSize = 100;
}
searchParam.setOffset((page - 1) * pageSize);
searchParam.setSize(pageSize);
// 5)设置排序字段
searchParam.setSortBuilders(searchSortHelper.buildSortList(paramMap));
// 6)从缓存中获取数据
final String productIndexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
JSONObject cacheObject = searchCacheService.getJSONObjectFromCache(this.searchCache, productIndexName, searchParam);
if (cacheObject != null) {
SearchCacheMatchLogger.doSearchCacheMatchLog("/promotion/list.json", paramMap);
return new SearchApiResult().setData(cacheObject);
}
// 7)查询ES
SearchResult searchResult = searchCommonService.doSearch(productIndexName, searchParam);
if (searchResult == null) {
return new SearchApiResult().setCode(500).setMessage("execption");
}
// 8)构造返回结果
JSONObject dataMap = new JSONObject();
dataMap.put("total", searchResult.getTotal());
dataMap.put("page", searchResult.getPage());
dataMap.put("page_size", searchParam.getSize());
dataMap.put("page_total", searchResult.getTotalPage());
dataMap.put("product_list", productIndexBaseService.getProductListWithPricePlan(searchResult.getResultList()));
// 10)将结果存进缓存
searchCacheService.addJSONObjectToCache(this.searchCache, productIndexName, searchParam, dataMap);
return new SearchApiResult().setData(dataMap);
}
public SearchApiResult selectionsForApp(PromotionConditions promotionConditions, Map<String, String> paramMap) throws Exception {
// 1、获取mustFilterByPromotion
BoolQueryBuilder mustFilterByPromotion = this.getMustFilterByPromotion(promotionConditions);
// 2、构造带filter和Query的SearchParam
SearchParam searchParam = searchParamHelper.buildWithMustFilter(paramMap, mustFilterByPromotion);
// 3、构造聚合条件【7种】
List<AbstractAggregationBuilder<?>> aggregationBuilders = new ArrayList<AbstractAggregationBuilder<?>>();
searchParam.setAggregationBuilders(aggregationBuilders);
// 3.1获取通用的聚合场景
aggregationBuilders.addAll(selectionsWithOutAdvanceService.getAllAggregationBuilders(paramMap));
// 3.2加上折扣聚合场景
IAggregation discountAggregation = aggregationFactoryService.getDiscountAggregation();
aggregationBuilders.add(discountAggregation.getBuilder());
// 3.3加上品类聚合场景
IAggregation sortGroupAggregation = aggregationFactoryService.getSortGroupAggregation(paramMap);
aggregationBuilders.add(sortGroupAggregation.getBuilder());
// 4、设置分页参数
searchParam.setOffset(0);
searchParam.setSize(0);
final String productIndexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
// 5、从缓存中取
JSONObject cacheObject = searchCacheService.getJSONObjectFromCache(this.searchCache, productIndexName, searchParam);
if (cacheObject != null) {
Object newCacheObject = searchAfterCacheService.dealFilterCacheObject(cacheObject);
newCacheObject = searchAfterCacheService.dealDiscountCacheObject((JSONObject) newCacheObject);
SearchCacheMatchLogger.doSearchCacheMatchLog("/promotion/selections.json", paramMap);
return new SearchApiResult().setData(newCacheObject);
}
// 6)查询ES
SearchResult searchResult = searchCommonService.doSearch(productIndexName, searchParam);
if (searchResult == null) {
return new SearchApiResult().setCode(500).setMessage("execption");
}
// 7)构造并返回聚合结果
Map<String, Aggregation> aggMaps = searchResult.getAggMaps();
if (aggMaps == null) {
return null;
}
// 8)先拿通用的聚合结果
Map<String, Object> commonAggResult = selectionsWithOutAdvanceService.getAggResultMap(paramMap, aggMaps);
// 9)获取折扣的聚合结果
Object discountAggResult = discountAggregation.getAggregationResponseMap(aggMaps);
// 10)获取品类的聚合结果
Object sortGroupAggResult = sortGroupAggregation.getAggregationResponseMap(aggMaps);
// 11)构造返回对象
JSONObject result = new JSONObject();
result.put("filter", commonAggResult);
result.put("discount", discountAggResult);
result.put("sort", sortGroupAggResult);
// 12)将结果加入缓存
searchCacheService.addJSONObjectToCache(this.searchCache, productIndexName, searchParam, result);
return new SearchApiResult().setData(result);
}
public SearchApiResult selectionsForPc(PromotionConditions promotionConditions, Map<String, String> paramMap) throws Exception {
// 1、获取默认的mustFilterByPromotion
BoolQueryBuilder mustFilterByPromotion = this.getMustFilterByPromotion(promotionConditions);
// 2、调通用的PC聚合逻辑
SearchApiResult apiResult = selectionsForPc.getSelectionsForPc(paramMap, mustFilterByPromotion);
if (apiResult == null || apiResult.getData() == null) {
return apiResult;
}
JSONObject result = (JSONObject) apiResult.getData();
// 3、加入品类聚合结果
JSONArray jsonArray = searchSortGroupService.sortGroup(paramMap, mustFilterByPromotion);
result.put("sort", jsonArray);
// 4、加入折扣聚合结果
JSONObject discount = discountService.discount(paramMap, mustFilterByPromotion);
result.put("dicount", discount);
return new SearchApiResult().setData(result);
}
public SearchApiResult aggPromotionBrands(PromotionConditions promotionConditions, Map<String, String> paramMap) throws Exception {
logger.info("[func=aggPromotionBrands][param={}][begin={}]", paramMap.toString(), System.currentTimeMillis());
// 1、设置必须满足的过滤条件
BoolQueryBuilder mustFilterByPromotion = this.getMustFilterByPromotion(promotionConditions);
// 2、调聚合品牌的方法
SearchApiResult searchApiResult = brandService.aggBrand(paramMap, mustFilterByPromotion);
return searchApiResult;
}
/**
* 促销列表新版筛选项
*
* @param promotionConditions
* @param paramMap
* @return
*/
public SearchApiResult aggregations(PromotionConditions promotionConditions, Map<String, String> paramMap) {
try {
// 0、解析promotion的限制条件
BoolQueryBuilder mustFilter = this.getMustFilterByPromotion(promotionConditions);
return sceneSelectionsService.aggregations(paramMap, mustFilter);
} catch (Exception e) {
logger.error(e.getMessage(), e);
return new SearchApiResult().setData(null).setMessage("PromotionAggregations Exception").setCode(500);
}
}
}
... ...