Authored by hugufei

代码优化

@@ -42,7 +42,7 @@ class PagePersionalFactorComponent { @@ -42,7 +42,7 @@ class PagePersionalFactorComponent {
42 } 42 }
43 43
44 /** 44 /**
45 - * 获取链接中的个性化因子 45 + * 获取链接中的个性化因子-品牌+品类
46 * 46 *
47 * @param paramQueryFilter 47 * @param paramQueryFilter
48 * @return 48 * @return
@@ -56,7 +56,6 @@ class PagePersionalFactorComponent { @@ -56,7 +56,6 @@ class PagePersionalFactorComponent {
56 56
57 //2、构造聚合参数 57 //2、构造聚合参数
58 List<AbstractAggregationBuilder<?>> aggregationBuilders = new ArrayList<>(); 58 List<AbstractAggregationBuilder<?>> aggregationBuilders = new ArrayList<>();
59 - aggregationBuilders.add(sortPriceAreaAggBuilder());//品类-价格带聚合  
60 aggregationBuilders.add(brandSortAggBuilder());//品类-品牌聚合 59 aggregationBuilders.add(brandSortAggBuilder());//品类-品牌聚合
61 searchParam.setAggregationBuilders(aggregationBuilders); 60 searchParam.setAggregationBuilders(aggregationBuilders);
62 61
@@ -72,45 +71,22 @@ class PagePersionalFactorComponent { @@ -72,45 +71,22 @@ class PagePersionalFactorComponent {
72 71
73 //5、构造结果 72 //5、构造结果
74 Map<String, Aggregation> aggregationMap = searchResult.getAggMaps(); 73 Map<String, Aggregation> aggregationMap = searchResult.getAggMaps();
75 - List<PageSortPriceAreas> sortPriceAreas = this.getSortPriceAreasFromAggregationMap(aggregationMap);  
76 List<PageBrandSorts> sortBrands = this.getBrandSortsFromAggregationMap(aggregationMap); 74 List<PageBrandSorts> sortBrands = this.getBrandSortsFromAggregationMap(aggregationMap);
77 75
78 - pagePagePersonalFactor = new PagePersonalFactor(sortPriceAreas,sortBrands); 76 + pagePagePersonalFactor = new PagePersonalFactor(sortBrands);
79 //6、加入缓存 77 //6、加入缓存
80 searchCacheService.addSerializableObjectToCache(searchCache,redisKeyBuilder, pagePagePersonalFactor,true); 78 searchCacheService.addSerializableObjectToCache(searchCache,redisKeyBuilder, pagePagePersonalFactor,true);
81 return pagePagePersonalFactor; 79 return pagePagePersonalFactor;
82 } 80 }
83 81
84 /** 82 /**
85 - * 品类-价格带聚合  
86 - * @return  
87 - */  
88 - private TermsAggregationBuilder sortPriceAreaAggBuilder() {  
89 - TermsAggregationBuilder middleSortAggBuilder = AggregationBuilders.terms("sortPriceMiddleSortIdAgg").field(ProductIndexEsField.middleSortId).size(200);  
90 - TermsAggregationBuilder priceAreaAggBuilder = AggregationBuilders.terms("sortPricePriceAreaAgg").field(ProductIndexEsField.priceArea).size(10);  
91 - middleSortAggBuilder.subAggregation(priceAreaAggBuilder);  
92 - return middleSortAggBuilder;  
93 - }  
94 -  
95 - private List<PageSortPriceAreas> getSortPriceAreasFromAggregationMap(Map<String, Aggregation> aggregationMap) {  
96 - Map<Integer,List<Integer>> misort2PriceAreasMap = this.getValueFromAggregationMap(aggregationMap,"sortPriceMiddleSortIdAgg","sortPricePriceAreaAgg");  
97 - List<PageSortPriceAreas> sortPrices = new ArrayList<>();  
98 - for (Map.Entry<Integer,List<Integer>> entry: misort2PriceAreasMap.entrySet()) {  
99 - Integer misort = entry.getKey();  
100 - List<Integer> priceAreas = entry.getValue();  
101 - sortPrices.add(new PageSortPriceAreas(misort,priceAreas));  
102 - }  
103 - return sortPrices;  
104 - }  
105 -  
106 - /**  
107 * 品类-品牌聚合 83 * 品类-品牌聚合
108 * 84 *
109 * @return 85 * @return
110 */ 86 */
111 private TermsAggregationBuilder brandSortAggBuilder() { 87 private TermsAggregationBuilder brandSortAggBuilder() {
112 - TermsAggregationBuilder middleSortAggBuilder = AggregationBuilders.terms("sortBrandBrandIdAgg").field(ProductIndexEsField.brandId).size(300);  
113 - middleSortAggBuilder.subAggregation(AggregationBuilders.terms("sortBrandMiddleSortAgg").field(ProductIndexEsField.middleSortId).size(100)); 88 + TermsAggregationBuilder middleSortAggBuilder = AggregationBuilders.terms("sortBrandBrandIdAgg").field(ProductIndexEsField.brandId).size(1000);
  89 + middleSortAggBuilder.subAggregation(AggregationBuilders.terms("sortBrandMiddleSortAgg").field(ProductIndexEsField.middleSortId).size(300));
114 return middleSortAggBuilder; 90 return middleSortAggBuilder;
115 } 91 }
116 92
@@ -41,42 +41,61 @@ public class QueryUserPersionalFactorBean { @@ -41,42 +41,61 @@ public class QueryUserPersionalFactorBean {
41 */ 41 */
42 public UserPersonalFactor queryPersionalFactorNew(UserRecallRequest userRecallRequest) { 42 public UserPersonalFactor queryPersionalFactorNew(UserRecallRequest userRecallRequest) {
43 try { 43 try {
  44 + boolean openDetailLog = this.openDetailLog(userRecallRequest);
  45 + int uid = userRecallRequest.getUid();
44 //1、获取页面上的个性化因子 46 //1、获取页面上的个性化因子
45 long begin = System.currentTimeMillis(); 47 long begin = System.currentTimeMillis();
46 PagePersonalFactor pageFactor = pageComponent.queryPagePersionalFactor(userRecallRequest.getParamQueryFilter()); 48 PagePersonalFactor pageFactor = pageComponent.queryPagePersionalFactor(userRecallRequest.getParamQueryFilter());
47 - RECALL_NEW_LOGGER.info("queryPagePersionalFactor . cost is[{}]", System.currentTimeMillis() - begin); 49 + long cost = System.currentTimeMillis() - begin;
  50 + if (!openDetailLog) {
  51 + RECALL_NEW_LOGGER.info("QueryUserPersionalFactor[1]:queryPageFactor. uid is[{}], cost is[{}] ",uid, cost);
  52 + } else {
  53 + RECALL_NEW_LOGGER.info("QueryUserPersionalFactor[1]:queryPageFactor. uid is[{}], cost is[{}], pageFactor is[{}] ",uid, cost, JSON.toJSONString(pageFactor));
  54 + }
48 //2、获取用户的个性化因子 55 //2、获取用户的个性化因子
49 begin = System.currentTimeMillis(); 56 begin = System.currentTimeMillis();
50 - UserPersonalFactorRspNew userFactorNew = userComponent.queryUserPersionalFactorNew(userRecallRequest.getUid(), userRecallRequest.getUdid(),pageFactor);  
51 - RECALL_NEW_LOGGER.info("queryUserPersionalFactorNew . cost is[{}]", System.currentTimeMillis() - begin); 57 + UserPersonalFactorRspNew userFactor = userComponent.queryUserPersionalFactor(userRecallRequest.getUid(), userRecallRequest.getUdid(), pageFactor);
  58 + cost = System.currentTimeMillis() - begin;
  59 + if (!openDetailLog) {
  60 + RECALL_NEW_LOGGER.info("QueryUserPersionalFactor[2]:queryUserFactor. uid is[{}], cost is[{}]",uid, cost);
  61 + } else {
  62 + RECALL_NEW_LOGGER.info("QueryUserPersionalFactor[2]:queryUserFactor. uid is[{}], cost is[{}], userFactor is[{}]",uid, cost, JSON.toJSONString(userFactor));
  63 + }
52 //3、join获取最终的结果 64 //3、join获取最终的结果
53 - List<SortBrand> sortBrandList = this.getSortBrandListWithSort(pageFactor, userFactorNew);  
54 - List<SortPriceAreas> sortPriceAreasList = this.getSortPriceAreasListWithSort(pageFactor, userFactorNew); 65 + List<SortBrand> sortBrandList = this.getSortBrandListWithSort(pageFactor, userFactor);
  66 + List<SortPriceAreas> sortPriceAreasList = this.getSortPriceAreasListWithSort(pageFactor, userFactor);
55 //4、构造UserPersonalFactorRspNew 67 //4、构造UserPersonalFactorRspNew
56 - UserPersonalFactor userPersonalFactor = new UserPersonalFactor(sortBrandList, sortPriceAreasList,userFactorNew.getVector());  
57 - if(userRecallRequest.getUid()==13420925){  
58 - RECALL_NEW_LOGGER.info("innerJoinFactor . uid is[{}], userPersonalFactor is [{}], ", userRecallRequest.getUid(), JSON.toJSONString(userPersonalFactor)); 68 + UserPersonalFactor userPersonalFactor = new UserPersonalFactor(sortBrandList, sortPriceAreasList, userFactor.getVector());
  69 + if (!openDetailLog) {
  70 + RECALL_NEW_LOGGER.info("QueryUserPersionalFactor[3]:join. uid is[{}], sortBrand size is[{}], sortPriceAreas size is [{}] ", uid, sortBrandList.size(),sortPriceAreasList.size());
  71 + } else {
  72 + 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));
59 } 73 }
60 return userPersonalFactor; 74 return userPersonalFactor;
61 } catch (Exception e) { 75 } catch (Exception e) {
62 logger.error(e.getMessage(), e); 76 logger.error(e.getMessage(), e);
63 - return new UserPersonalFactor(new ArrayList<>(),new ArrayList<>(), ""); 77 + return new UserPersonalFactor(new ArrayList<>(), new ArrayList<>(), "");
64 } 78 }
65 } 79 }
66 80
  81 + private boolean openDetailLog(UserRecallRequest userRecallRequest) {
  82 + return userRecallRequest.getUid() == 13420925;
  83 + }
  84 +
67 /** 85 /**
68 * 按顺序截取【品类+品牌】 86 * 按顺序截取【品类+品牌】
  87 + *
69 * @param pageFactor 88 * @param pageFactor
70 * @param userFactorNew 89 * @param userFactorNew
71 * @return 90 * @return
72 */ 91 */
73 - private List<SortBrand> getSortBrandListWithSort(PagePersonalFactor pageFactor, UserPersonalFactorRspNew userFactorNew){ 92 + private List<SortBrand> getSortBrandListWithSort(PagePersonalFactor pageFactor, UserPersonalFactorRspNew userFactorNew) {
74 //1、返回结果定义 93 //1、返回结果定义
75 List<SortBrand> results = new ArrayList<>(); 94 List<SortBrand> results = new ArrayList<>();
76 95
77 //2、构造brand2MiSortIdsMap 96 //2、构造brand2MiSortIdsMap
78 List<PageBrandSorts> brandSortsList = pageFactor.getBrandSortsList(); 97 List<PageBrandSorts> brandSortsList = pageFactor.getBrandSortsList();
79 - Map<Integer,List<Integer>> brand2MiSortIdsMap = new HashMap<>(); 98 + Map<Integer, List<Integer>> brand2MiSortIdsMap = new HashMap<>();
80 for (PageBrandSorts pageBrandSorts : brandSortsList) { 99 for (PageBrandSorts pageBrandSorts : brandSortsList) {
81 brand2MiSortIdsMap.put(pageBrandSorts.getBrandId(), pageBrandSorts.getMisorts()); 100 brand2MiSortIdsMap.put(pageBrandSorts.getBrandId(), pageBrandSorts.getMisorts());
82 } 101 }
@@ -84,15 +103,15 @@ public class QueryUserPersionalFactorBean { @@ -84,15 +103,15 @@ public class QueryUserPersionalFactorBean {
84 //3、构建SortBrands 103 //3、构建SortBrands
85 int maxJoinSortBrandCount = searchDynamicConfigService.searchPersionalNewStrategyMaxJoinSortBrandCount(); 104 int maxJoinSortBrandCount = searchDynamicConfigService.searchPersionalNewStrategyMaxJoinSortBrandCount();
86 for (SortBrand sortBrand : userFactorNew.getSortBrandList()) { 105 for (SortBrand sortBrand : userFactorNew.getSortBrandList()) {
87 - if(!brand2MiSortIdsMap.containsKey(sortBrand.getBrandId())){ 106 + if (!brand2MiSortIdsMap.containsKey(sortBrand.getBrandId())) {
88 continue; 107 continue;
89 } 108 }
90 List<Integer> miSortIds = brand2MiSortIdsMap.get(sortBrand.getBrandId()); 109 List<Integer> miSortIds = brand2MiSortIdsMap.get(sortBrand.getBrandId());
91 - if(miSortIds==null || !miSortIds.contains(sortBrand.getMisort())){ 110 + if (miSortIds == null || !miSortIds.contains(sortBrand.getMisort())) {
92 continue; 111 continue;
93 } 112 }
94 results.add(sortBrand); 113 results.add(sortBrand);
95 - if (results.size() >= maxJoinSortBrandCount){ 114 + if (results.size() >= maxJoinSortBrandCount) {
96 break; 115 break;
97 } 116 }
98 } 117 }
@@ -101,13 +120,14 @@ public class QueryUserPersionalFactorBean { @@ -101,13 +120,14 @@ public class QueryUserPersionalFactorBean {
101 120
102 /** 121 /**
103 * 【品类+价格带】 122 * 【品类+价格带】
  123 + *
104 * @param pageFactor 124 * @param pageFactor
105 * @param userFactorNew 125 * @param userFactorNew
106 * @return 126 * @return
107 */ 127 */
108 - private List<SortPriceAreas> getSortPriceAreasListWithSort(PagePersonalFactor pageFactor, UserPersonalFactorRspNew userFactorNew){ 128 + private List<SortPriceAreas> getSortPriceAreasListWithSort(PagePersonalFactor pageFactor, UserPersonalFactorRspNew userFactorNew) {
109 List<SortPriceAreas> sortPriceAreas = userFactorNew.getSortPriceAreasList(); 129 List<SortPriceAreas> sortPriceAreas = userFactorNew.getSortPriceAreasList();
110 - if(sortPriceAreas==null|| sortPriceAreas.isEmpty()){ 130 + if (sortPriceAreas == null || sortPriceAreas.isEmpty()) {
111 return new ArrayList<>(); 131 return new ArrayList<>();
112 } 132 }
113 return sortPriceAreas; 133 return sortPriceAreas;
@@ -7,6 +7,7 @@ import com.yoho.search.core.personalized.models.*; @@ -7,6 +7,7 @@ import com.yoho.search.core.personalized.models.*;
7 import com.yoho.search.recall.scene.models.personal.PageBrandSorts; 7 import com.yoho.search.recall.scene.models.personal.PageBrandSorts;
8 import com.yoho.search.recall.scene.models.personal.PagePersonalFactor; 8 import com.yoho.search.recall.scene.models.personal.PagePersonalFactor;
9 import com.yoho.search.recall.scene.models.personal.PageSortPriceAreas; 9 import com.yoho.search.recall.scene.models.personal.PageSortPriceAreas;
  10 +import com.yoho.search.service.base.SearchDynamicConfigService;
10 import org.slf4j.Logger; 11 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory; 12 import org.slf4j.LoggerFactory;
12 import org.springframework.beans.factory.annotation.Autowired; 13 import org.springframework.beans.factory.annotation.Autowired;
@@ -20,6 +21,9 @@ import java.util.List; @@ -20,6 +21,9 @@ import java.util.List;
20 @Component 21 @Component
21 class UserPersionalFactorComponent { 22 class UserPersionalFactorComponent {
22 23
  24 + @Autowired
  25 + private SearchDynamicConfigService searchDynamicConfigService;
  26 +
23 private static final Logger RECALL_NEW_LOGGER = LoggerFactory.getLogger("RECALL"); 27 private static final Logger RECALL_NEW_LOGGER = LoggerFactory.getLogger("RECALL");
24 private static final String SERVICE_NAME = "bigdata.searchPersonalFactors"; 28 private static final String SERVICE_NAME = "bigdata.searchPersonalFactors";
25 private static final int timeOut = 100; 29 private static final int timeOut = 100;
@@ -33,7 +37,7 @@ class UserPersionalFactorComponent { @@ -33,7 +37,7 @@ class UserPersionalFactorComponent {
33 * @param udid 37 * @param udid
34 * @return 38 * @return
35 */ 39 */
36 - public UserPersonalFactorRspNew queryUserPersionalFactorNew(int uid, String udid, PagePersonalFactor pageFactor) { 40 + public UserPersonalFactorRspNew queryUserPersionalFactor(int uid, String udid, PagePersonalFactor pageFactor) {
37 try { 41 try {
38 UserPersonalFactorReq userPersionalFactorReq = new UserPersonalFactorReq(uid, udid); 42 UserPersonalFactorReq userPersionalFactorReq = new UserPersonalFactorReq(uid, udid);
39 JSONObject result = serviceCaller.call(SERVICE_NAME, userPersionalFactorReq, JSONObject.class, timeOut); 43 JSONObject result = serviceCaller.call(SERVICE_NAME, userPersionalFactorReq, JSONObject.class, timeOut);
@@ -51,11 +55,11 @@ class UserPersionalFactorComponent { @@ -51,11 +55,11 @@ class UserPersionalFactorComponent {
51 return rsp; 55 return rsp;
52 } catch (Exception e) { 56 } catch (Exception e) {
53 RECALL_NEW_LOGGER.error(e.getMessage(), e); 57 RECALL_NEW_LOGGER.error(e.getMessage(), e);
54 - return getDefaultUserPersonalFactorRspNew(pageFactor); 58 + return getDefaultUserPersonalFactorRsp(pageFactor);
55 } 59 }
56 } 60 }
57 61
58 - private UserPersonalFactorRspNew getDefaultUserPersonalFactorRspNew(PagePersonalFactor pageFactor) { 62 + private UserPersonalFactorRspNew getDefaultUserPersonalFactorRsp(PagePersonalFactor pageFactor) {
59 //1、构造sortBrandList 63 //1、构造sortBrandList
60 List<SortBrand> sortBrandList = new ArrayList<>(); 64 List<SortBrand> sortBrandList = new ArrayList<>();
61 List<PageBrandSorts> pageBrandSortsList = pageFactor.getBrandSortsList(); 65 List<PageBrandSorts> pageBrandSortsList = pageFactor.getBrandSortsList();
@@ -63,20 +67,12 @@ class UserPersionalFactorComponent { @@ -63,20 +67,12 @@ class UserPersionalFactorComponent {
63 for (int i = 0; i < pageBrandSortsList.size(); i++) { 67 for (int i = 0; i < pageBrandSortsList.size(); i++) {
64 PageBrandSorts pageBrandSorts = pageBrandSortsList.get(i); 68 PageBrandSorts pageBrandSorts = pageBrandSortsList.get(i);
65 sortBrandList.add(new SortBrand(pageBrandSorts.getMisorts().get(0), pageBrandSorts.getBrandId())); 69 sortBrandList.add(new SortBrand(pageBrandSorts.getMisorts().get(0), pageBrandSorts.getBrandId()));
66 - if (sortBrandList.size() > 50) { 70 + if (sortBrandList.size() > searchDynamicConfigService.searchPersionalNewStrategyMaxJoinSortBrandCount()) {
67 break; 71 break;
68 } 72 }
69 } 73 }
70 //2、构造SortPriceAreas 74 //2、构造SortPriceAreas
71 List<SortPriceAreas> sortPriceAreasList = new ArrayList<>(); 75 List<SortPriceAreas> sortPriceAreasList = new ArrayList<>();
72 - List<PageSortPriceAreas> pageSortPriceAreasList = pageFactor.getSortPriceAreasList();  
73 - for (int i = 0; i < pageSortPriceAreasList.size(); i++) {  
74 - PageSortPriceAreas pageSortPriceAreas = pageSortPriceAreasList.get(i);  
75 - sortPriceAreasList.add(new SortPriceAreas(pageSortPriceAreas.getMisort(), pageSortPriceAreas.getPriceAreas()));  
76 - if (sortPriceAreasList.size() > 50) {  
77 - break;  
78 - }  
79 - }  
80 return new UserPersonalFactorRspNew(sortBrandList, sortPriceAreasList, ""); 76 return new UserPersonalFactorRspNew(sortBrandList, sortPriceAreasList, "");
81 } 77 }
82 78
@@ -10,25 +10,15 @@ public class PagePersonalFactor implements Serializable{ @@ -10,25 +10,15 @@ public class PagePersonalFactor implements Serializable{
10 10
11 private static final long serialVersionUID = 89030356435559223L; 11 private static final long serialVersionUID = 89030356435559223L;
12 12
13 - private List<PageSortPriceAreas> sortPriceAreasList;  
14 private List<PageBrandSorts> brandSortsList; 13 private List<PageBrandSorts> brandSortsList;
15 14
16 public PagePersonalFactor() { 15 public PagePersonalFactor() {
17 } 16 }
18 17
19 - public PagePersonalFactor(List<PageSortPriceAreas> sortPriceAreasList, List<PageBrandSorts> brandSortsList){  
20 - this.sortPriceAreasList = sortPriceAreasList; 18 + public PagePersonalFactor(List<PageBrandSorts> brandSortsList){
21 this.brandSortsList = brandSortsList; 19 this.brandSortsList = brandSortsList;
22 } 20 }
23 21
24 - public List<PageSortPriceAreas> getSortPriceAreasList() {  
25 - return sortPriceAreasList;  
26 - }  
27 -  
28 - public void setSortPriceAreasList(List<PageSortPriceAreas> sortPriceAreasList) {  
29 - this.sortPriceAreasList = sortPriceAreasList;  
30 - }  
31 -  
32 public List<PageBrandSorts> getBrandSortsList() { 22 public List<PageBrandSorts> getBrandSortsList() {
33 return brandSortsList; 23 return brandSortsList;
34 } 24 }