Authored by hugufei

代码优化

... ... @@ -42,7 +42,7 @@ class PagePersionalFactorComponent {
}
/**
* 获取链接中的个性化因子
* 获取链接中的个性化因子-品牌+品类
*
* @param paramQueryFilter
* @return
... ... @@ -56,7 +56,6 @@ class PagePersionalFactorComponent {
//2、构造聚合参数
List<AbstractAggregationBuilder<?>> aggregationBuilders = new ArrayList<>();
aggregationBuilders.add(sortPriceAreaAggBuilder());//品类-价格带聚合
aggregationBuilders.add(brandSortAggBuilder());//品类-品牌聚合
searchParam.setAggregationBuilders(aggregationBuilders);
... ... @@ -72,45 +71,22 @@ class PagePersionalFactorComponent {
//5、构造结果
Map<String, Aggregation> aggregationMap = searchResult.getAggMaps();
List<PageSortPriceAreas> sortPriceAreas = this.getSortPriceAreasFromAggregationMap(aggregationMap);
List<PageBrandSorts> sortBrands = this.getBrandSortsFromAggregationMap(aggregationMap);
pagePagePersonalFactor = new PagePersonalFactor(sortPriceAreas,sortBrands);
pagePagePersonalFactor = new PagePersonalFactor(sortBrands);
//6、加入缓存
searchCacheService.addSerializableObjectToCache(searchCache,redisKeyBuilder, pagePagePersonalFactor,true);
return pagePagePersonalFactor;
}
/**
* 品类-价格带聚合
* @return
*/
private TermsAggregationBuilder sortPriceAreaAggBuilder() {
TermsAggregationBuilder middleSortAggBuilder = AggregationBuilders.terms("sortPriceMiddleSortIdAgg").field(ProductIndexEsField.middleSortId).size(200);
TermsAggregationBuilder priceAreaAggBuilder = AggregationBuilders.terms("sortPricePriceAreaAgg").field(ProductIndexEsField.priceArea).size(10);
middleSortAggBuilder.subAggregation(priceAreaAggBuilder);
return middleSortAggBuilder;
}
private List<PageSortPriceAreas> getSortPriceAreasFromAggregationMap(Map<String, Aggregation> aggregationMap) {
Map<Integer,List<Integer>> misort2PriceAreasMap = this.getValueFromAggregationMap(aggregationMap,"sortPriceMiddleSortIdAgg","sortPricePriceAreaAgg");
List<PageSortPriceAreas> sortPrices = new ArrayList<>();
for (Map.Entry<Integer,List<Integer>> entry: misort2PriceAreasMap.entrySet()) {
Integer misort = entry.getKey();
List<Integer> priceAreas = entry.getValue();
sortPrices.add(new PageSortPriceAreas(misort,priceAreas));
}
return sortPrices;
}
/**
* 品类-品牌聚合
*
* @return
*/
private TermsAggregationBuilder brandSortAggBuilder() {
TermsAggregationBuilder middleSortAggBuilder = AggregationBuilders.terms("sortBrandBrandIdAgg").field(ProductIndexEsField.brandId).size(300);
middleSortAggBuilder.subAggregation(AggregationBuilders.terms("sortBrandMiddleSortAgg").field(ProductIndexEsField.middleSortId).size(100));
TermsAggregationBuilder middleSortAggBuilder = AggregationBuilders.terms("sortBrandBrandIdAgg").field(ProductIndexEsField.brandId).size(1000);
middleSortAggBuilder.subAggregation(AggregationBuilders.terms("sortBrandMiddleSortAgg").field(ProductIndexEsField.middleSortId).size(300));
return middleSortAggBuilder;
}
... ...
... ... @@ -41,42 +41,61 @@ public class QueryUserPersionalFactorBean {
*/
public UserPersonalFactor queryPersionalFactorNew(UserRecallRequest userRecallRequest) {
try {
boolean openDetailLog = this.openDetailLog(userRecallRequest);
int uid = userRecallRequest.getUid();
//1、获取页面上的个性化因子
long begin = System.currentTimeMillis();
PagePersonalFactor pageFactor = pageComponent.queryPagePersionalFactor(userRecallRequest.getParamQueryFilter());
RECALL_NEW_LOGGER.info("queryPagePersionalFactor . cost is[{}]", System.currentTimeMillis() - begin);
long cost = System.currentTimeMillis() - begin;
if (!openDetailLog) {
RECALL_NEW_LOGGER.info("QueryUserPersionalFactor[1]:queryPageFactor. uid is[{}], cost is[{}] ",uid, cost);
} else {
RECALL_NEW_LOGGER.info("QueryUserPersionalFactor[1]:queryPageFactor. uid is[{}], cost is[{}], pageFactor is[{}] ",uid, cost, JSON.toJSONString(pageFactor));
}
//2、获取用户的个性化因子
begin = System.currentTimeMillis();
UserPersonalFactorRspNew userFactorNew = userComponent.queryUserPersionalFactorNew(userRecallRequest.getUid(), userRecallRequest.getUdid(),pageFactor);
RECALL_NEW_LOGGER.info("queryUserPersionalFactorNew . cost is[{}]", System.currentTimeMillis() - begin);
UserPersonalFactorRspNew userFactor = userComponent.queryUserPersionalFactor(userRecallRequest.getUid(), userRecallRequest.getUdid(), pageFactor);
cost = System.currentTimeMillis() - begin;
if (!openDetailLog) {
RECALL_NEW_LOGGER.info("QueryUserPersionalFactor[2]:queryUserFactor. uid is[{}], cost is[{}]",uid, cost);
} else {
RECALL_NEW_LOGGER.info("QueryUserPersionalFactor[2]:queryUserFactor. uid is[{}], cost is[{}], userFactor is[{}]",uid, cost, JSON.toJSONString(userFactor));
}
//3、join获取最终的结果
List<SortBrand> sortBrandList = this.getSortBrandListWithSort(pageFactor, userFactorNew);
List<SortPriceAreas> sortPriceAreasList = this.getSortPriceAreasListWithSort(pageFactor, userFactorNew);
List<SortBrand> sortBrandList = this.getSortBrandListWithSort(pageFactor, userFactor);
List<SortPriceAreas> sortPriceAreasList = this.getSortPriceAreasListWithSort(pageFactor, userFactor);
//4、构造UserPersonalFactorRspNew
UserPersonalFactor userPersonalFactor = new UserPersonalFactor(sortBrandList, sortPriceAreasList,userFactorNew.getVector());
if(userRecallRequest.getUid()==13420925){
RECALL_NEW_LOGGER.info("innerJoinFactor . uid is[{}], userPersonalFactor is [{}], ", userRecallRequest.getUid(), JSON.toJSONString(userPersonalFactor));
UserPersonalFactor userPersonalFactor = new UserPersonalFactor(sortBrandList, sortPriceAreasList, userFactor.getVector());
if (!openDetailLog) {
RECALL_NEW_LOGGER.info("QueryUserPersionalFactor[3]:join. uid is[{}], sortBrand size is[{}], sortPriceAreas size is [{}] ", uid, sortBrandList.size(),sortPriceAreasList.size());
} else {
RECALL_NEW_LOGGER.info("QueryUserPersionalFactor[3]:join. uid is[{}], sortBrand size is[{}], sortPriceAreas size is [{}], data is [{}], ", uid, sortBrandList.size(),sortPriceAreasList.size(),JSON.toJSONString(userPersonalFactor));
}
return userPersonalFactor;
} catch (Exception e) {
logger.error(e.getMessage(), e);
return new UserPersonalFactor(new ArrayList<>(),new ArrayList<>(), "");
return new UserPersonalFactor(new ArrayList<>(), new ArrayList<>(), "");
}
}
private boolean openDetailLog(UserRecallRequest userRecallRequest) {
return userRecallRequest.getUid() == 13420925;
}
/**
* 按顺序截取【品类+品牌】
*
* @param pageFactor
* @param userFactorNew
* @return
*/
private List<SortBrand> getSortBrandListWithSort(PagePersonalFactor pageFactor, UserPersonalFactorRspNew userFactorNew){
private List<SortBrand> getSortBrandListWithSort(PagePersonalFactor pageFactor, UserPersonalFactorRspNew userFactorNew) {
//1、返回结果定义
List<SortBrand> results = new ArrayList<>();
//2、构造brand2MiSortIdsMap
List<PageBrandSorts> brandSortsList = pageFactor.getBrandSortsList();
Map<Integer,List<Integer>> brand2MiSortIdsMap = new HashMap<>();
Map<Integer, List<Integer>> brand2MiSortIdsMap = new HashMap<>();
for (PageBrandSorts pageBrandSorts : brandSortsList) {
brand2MiSortIdsMap.put(pageBrandSorts.getBrandId(), pageBrandSorts.getMisorts());
}
... ... @@ -84,15 +103,15 @@ public class QueryUserPersionalFactorBean {
//3、构建SortBrands
int maxJoinSortBrandCount = searchDynamicConfigService.searchPersionalNewStrategyMaxJoinSortBrandCount();
for (SortBrand sortBrand : userFactorNew.getSortBrandList()) {
if(!brand2MiSortIdsMap.containsKey(sortBrand.getBrandId())){
if (!brand2MiSortIdsMap.containsKey(sortBrand.getBrandId())) {
continue;
}
List<Integer> miSortIds = brand2MiSortIdsMap.get(sortBrand.getBrandId());
if(miSortIds==null || !miSortIds.contains(sortBrand.getMisort())){
if (miSortIds == null || !miSortIds.contains(sortBrand.getMisort())) {
continue;
}
results.add(sortBrand);
if (results.size() >= maxJoinSortBrandCount){
if (results.size() >= maxJoinSortBrandCount) {
break;
}
}
... ... @@ -101,13 +120,14 @@ public class QueryUserPersionalFactorBean {
/**
* 【品类+价格带】
*
* @param pageFactor
* @param userFactorNew
* @return
*/
private List<SortPriceAreas> getSortPriceAreasListWithSort(PagePersonalFactor pageFactor, UserPersonalFactorRspNew userFactorNew){
private List<SortPriceAreas> getSortPriceAreasListWithSort(PagePersonalFactor pageFactor, UserPersonalFactorRspNew userFactorNew) {
List<SortPriceAreas> sortPriceAreas = userFactorNew.getSortPriceAreasList();
if(sortPriceAreas==null|| sortPriceAreas.isEmpty()){
if (sortPriceAreas == null || sortPriceAreas.isEmpty()) {
return new ArrayList<>();
}
return sortPriceAreas;
... ...
... ... @@ -7,6 +7,7 @@ import com.yoho.search.core.personalized.models.*;
import com.yoho.search.recall.scene.models.personal.PageBrandSorts;
import com.yoho.search.recall.scene.models.personal.PagePersonalFactor;
import com.yoho.search.recall.scene.models.personal.PageSortPriceAreas;
import com.yoho.search.service.base.SearchDynamicConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
... ... @@ -20,6 +21,9 @@ import java.util.List;
@Component
class UserPersionalFactorComponent {
@Autowired
private SearchDynamicConfigService searchDynamicConfigService;
private static final Logger RECALL_NEW_LOGGER = LoggerFactory.getLogger("RECALL");
private static final String SERVICE_NAME = "bigdata.searchPersonalFactors";
private static final int timeOut = 100;
... ... @@ -33,7 +37,7 @@ class UserPersionalFactorComponent {
* @param udid
* @return
*/
public UserPersonalFactorRspNew queryUserPersionalFactorNew(int uid, String udid, PagePersonalFactor pageFactor) {
public UserPersonalFactorRspNew queryUserPersionalFactor(int uid, String udid, PagePersonalFactor pageFactor) {
try {
UserPersonalFactorReq userPersionalFactorReq = new UserPersonalFactorReq(uid, udid);
JSONObject result = serviceCaller.call(SERVICE_NAME, userPersionalFactorReq, JSONObject.class, timeOut);
... ... @@ -51,11 +55,11 @@ class UserPersionalFactorComponent {
return rsp;
} catch (Exception e) {
RECALL_NEW_LOGGER.error(e.getMessage(), e);
return getDefaultUserPersonalFactorRspNew(pageFactor);
return getDefaultUserPersonalFactorRsp(pageFactor);
}
}
private UserPersonalFactorRspNew getDefaultUserPersonalFactorRspNew(PagePersonalFactor pageFactor) {
private UserPersonalFactorRspNew getDefaultUserPersonalFactorRsp(PagePersonalFactor pageFactor) {
//1、构造sortBrandList
List<SortBrand> sortBrandList = new ArrayList<>();
List<PageBrandSorts> pageBrandSortsList = pageFactor.getBrandSortsList();
... ... @@ -63,20 +67,12 @@ class UserPersionalFactorComponent {
for (int i = 0; i < pageBrandSortsList.size(); i++) {
PageBrandSorts pageBrandSorts = pageBrandSortsList.get(i);
sortBrandList.add(new SortBrand(pageBrandSorts.getMisorts().get(0), pageBrandSorts.getBrandId()));
if (sortBrandList.size() > 50) {
if (sortBrandList.size() > searchDynamicConfigService.searchPersionalNewStrategyMaxJoinSortBrandCount()) {
break;
}
}
//2、构造SortPriceAreas
List<SortPriceAreas> sortPriceAreasList = new ArrayList<>();
List<PageSortPriceAreas> pageSortPriceAreasList = pageFactor.getSortPriceAreasList();
for (int i = 0; i < pageSortPriceAreasList.size(); i++) {
PageSortPriceAreas pageSortPriceAreas = pageSortPriceAreasList.get(i);
sortPriceAreasList.add(new SortPriceAreas(pageSortPriceAreas.getMisort(), pageSortPriceAreas.getPriceAreas()));
if (sortPriceAreasList.size() > 50) {
break;
}
}
return new UserPersonalFactorRspNew(sortBrandList, sortPriceAreasList, "");
}
... ...
... ... @@ -10,25 +10,15 @@ public class PagePersonalFactor implements Serializable{
private static final long serialVersionUID = 89030356435559223L;
private List<PageSortPriceAreas> sortPriceAreasList;
private List<PageBrandSorts> brandSortsList;
public PagePersonalFactor() {
}
public PagePersonalFactor(List<PageSortPriceAreas> sortPriceAreasList, List<PageBrandSorts> brandSortsList){
this.sortPriceAreasList = sortPriceAreasList;
public PagePersonalFactor(List<PageBrandSorts> brandSortsList){
this.brandSortsList = brandSortsList;
}
public List<PageSortPriceAreas> getSortPriceAreasList() {
return sortPriceAreasList;
}
public void setSortPriceAreasList(List<PageSortPriceAreas> sortPriceAreasList) {
this.sortPriceAreasList = sortPriceAreasList;
}
public List<PageBrandSorts> getBrandSortsList() {
return brandSortsList;
}
... ...