Authored by hugufei

品类品牌召回代码优化

1 package com.yoho.search.service.recall; 1 package com.yoho.search.service.recall;
2 2
  3 +import com.yoho.search.models.recall.*;
3 import com.yoho.search.service.base.BatchRecallBaseService; 4 import com.yoho.search.service.base.BatchRecallBaseService;
4 import com.yoho.search.service.recall.beans.requests.CommonRecallRequestBuilder; 5 import com.yoho.search.service.recall.beans.requests.CommonRecallRequestBuilder;
5 -import com.yoho.search.service.recall.beans.requests.SortBrandRecallRequestBuilder;  
6 import com.yoho.search.service.recall.beans.requests.SknRecallRecallRequestResponseBuilder; 6 import com.yoho.search.service.recall.beans.requests.SknRecallRecallRequestResponseBuilder;
7 -import com.yoho.search.models.recall.PagePersonalFactor;  
8 -import com.yoho.search.models.recall.UserPersonalFactor;  
9 -import com.yoho.search.models.recall.RecallRequest;  
10 -import com.yoho.search.models.recall.RecallRequestResponse;  
11 -import com.yoho.search.models.recall.UserRecallRequest;  
12 -import com.yoho.search.service.recall.strategy.SortBrandType; 7 +import com.yoho.search.service.recall.beans.requests.SortBrandRecallRequestBuilder;
13 import org.slf4j.Logger; 8 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory; 9 import org.slf4j.LoggerFactory;
15 import org.springframework.beans.factory.annotation.Autowired; 10 import org.springframework.beans.factory.annotation.Autowired;
@@ -54,27 +49,13 @@ class BatchRequestResponseEntrace { @@ -54,27 +49,13 @@ class BatchRequestResponseEntrace {
54 CompletableFuture<List<RecallRequestResponse>> commonCompletableFuture = this.doRecallCommon(userRecallRequest, pagePersonalFactor); 49 CompletableFuture<List<RecallRequestResponse>> commonCompletableFuture = this.doRecallCommon(userRecallRequest, pagePersonalFactor);
55 50
56 //3、处理实时推荐的品类品牌的召回 51 //3、处理实时推荐的品类品牌的召回
57 - CompletableFuture<List<RecallRequestResponse>> realTimeSortBrandCompletableFuture = this.doRecallRealTimeSortBrand(userRecallRequest, userPersonalFactor);  
58 -  
59 - //4、处理基于W2V向量生成的品类品牌的召回  
60 - CompletableFuture<List<RecallRequestResponse>> vectorW2vSortBrandCompletableFuture = this.doRecallVectorW2vSortBrand(userRecallRequest, userPersonalFactor); 52 + CompletableFuture<List<RecallRequestResponse>> sortBrandCompletableFuture = this.doRecallSortBrand(userRecallRequest, userPersonalFactor);
61 53
62 - //5、处理基于RNN向量生成的品类品牌的召回  
63 - CompletableFuture<List<RecallRequestResponse>> vectorRnnSortBrandCompletableFuture = this.doRecallVectorRnnSortBrand(userRecallRequest, userPersonalFactor);  
64 -  
65 - //6、处理基于RNN向量生成的品类品牌的召回  
66 - CompletableFuture<List<RecallRequestResponse>> configSortBrandCompletableFuture = this.doRecallConfigSortBrand(userRecallRequest, userPersonalFactor);  
67 -  
68 - //7、构造最终返回结果投入额 54 + //4、构造最终返回结果投入额
69 List<RecallRequestResponse> batchRequestResults = new ArrayList<>(); 55 List<RecallRequestResponse> batchRequestResults = new ArrayList<>();
70 -  
71 batchRequestResults.addAll(this.getResultFromCompletableFuture(sknListCompletableFuture));//按skn召回放在第一个,不然merger的时候可能会无序 56 batchRequestResults.addAll(this.getResultFromCompletableFuture(sknListCompletableFuture));//按skn召回放在第一个,不然merger的时候可能会无序
72 batchRequestResults.addAll(this.getResultFromCompletableFuture(commonCompletableFuture)); 57 batchRequestResults.addAll(this.getResultFromCompletableFuture(commonCompletableFuture));
73 - batchRequestResults.addAll(this.getResultFromCompletableFuture(realTimeSortBrandCompletableFuture));  
74 - batchRequestResults.addAll(this.getResultFromCompletableFuture(vectorW2vSortBrandCompletableFuture));  
75 - batchRequestResults.addAll(this.getResultFromCompletableFuture(vectorRnnSortBrandCompletableFuture));  
76 - batchRequestResults.addAll(this.getResultFromCompletableFuture(configSortBrandCompletableFuture));  
77 - 58 + batchRequestResults.addAll(this.getResultFromCompletableFuture(sortBrandCompletableFuture));
78 return batchRequestResults; 59 return batchRequestResults;
79 } 60 }
80 61
@@ -96,15 +77,15 @@ class BatchRequestResponseEntrace { @@ -96,15 +77,15 @@ class BatchRequestResponseEntrace {
96 private CompletableFuture<List<RecallRequestResponse>> doRecallCommon(UserRecallRequest userRecallRequest, PagePersonalFactor pagePersonalFactor) { 77 private CompletableFuture<List<RecallRequestResponse>> doRecallCommon(UserRecallRequest userRecallRequest, PagePersonalFactor pagePersonalFactor) {
97 return CompletableFuture.supplyAsync(() -> { 78 return CompletableFuture.supplyAsync(() -> {
98 long begin = System.currentTimeMillis(); 79 long begin = System.currentTimeMillis();
99 - List<RecallRequest> commonRequests = commonRequestBuilder.buildCommonRecallRequests(userRecallRequest, pagePersonalFactor);  
100 - List<RecallRequestResponse> commonRequestResponses = batchRecallBaseService.batchRecallAndCache(commonRequests);  
101 - RECALL_NEW_LOGGER.info("UserRecallCacheBean[2.1]-doRecallCommon,requestCount is [{}], cost is [{}]", commonRequests.size(), System.currentTimeMillis() - begin);  
102 - return commonRequestResponses; 80 + List<RecallRequest> requests = commonRequestBuilder.buildCommonRecallRequests(userRecallRequest, pagePersonalFactor);
  81 + List<RecallRequestResponse> requestResponses = batchRecallBaseService.batchRecallAndCache(requests);
  82 + RECALL_NEW_LOGGER.info("batchRecall[2.1]-doRecallCommon,request size is [{}],cost is [{}]", requests.size(), System.currentTimeMillis() - begin);
  83 + return requestResponses;
103 }, recallExecutorService); 84 }, recallExecutorService);
104 } 85 }
105 86
106 /** 87 /**
107 - * 执行实时推荐SKN的召回 88 + * 执行推荐SKN的召回
108 * 89 *
109 * @param userRecallRequest 90 * @param userRecallRequest
110 * @param userPersonalFactor 91 * @param userPersonalFactor
@@ -113,79 +94,27 @@ class BatchRequestResponseEntrace { @@ -113,79 +94,27 @@ class BatchRequestResponseEntrace {
113 private CompletableFuture<List<RecallRequestResponse>> doRecallSknList(UserRecallRequest userRecallRequest, UserPersonalFactor userPersonalFactor) { 94 private CompletableFuture<List<RecallRequestResponse>> doRecallSknList(UserRecallRequest userRecallRequest, UserPersonalFactor userPersonalFactor) {
114 return CompletableFuture.supplyAsync(() -> { 95 return CompletableFuture.supplyAsync(() -> {
115 long begin = System.currentTimeMillis(); 96 long begin = System.currentTimeMillis();
116 - List<RecallRequestResponse> recommendSknRequestResponses = sknRecallRecallRequestResponseBuilder.batchRecallBySknList(userRecallRequest, userPersonalFactor);  
117 - RECALL_NEW_LOGGER.info("UserRecallCacheBean[2.2]-doRecallRecommendSkn,recommendSknCount is [{}],cost is [{}]", recommendSknRequestResponses.size(), System.currentTimeMillis() - begin);  
118 - return recommendSknRequestResponses;  
119 - }, recallExecutorService);  
120 - }  
121 -  
122 - /**  
123 - * 执行实时【品牌+品类】的召回  
124 - *  
125 - * @param userRecallRequest  
126 - * @param userPersonalFactor  
127 - * @return  
128 - */  
129 - private CompletableFuture<List<RecallRequestResponse>> doRecallRealTimeSortBrand(UserRecallRequest userRecallRequest, UserPersonalFactor userPersonalFactor) {  
130 - return CompletableFuture.supplyAsync(() -> {  
131 - long begin = System.currentTimeMillis();  
132 - List<RecallRequest> realTimeSortBrandRequests = sortBrandRecallRequestBuilder.buildSortBrandRecallRequests(userRecallRequest, userPersonalFactor.getRealTimeSortBrandList(), SortBrandType.REC_SORT_BRAND);  
133 - List<RecallRequestResponse> realTimeSortBrandRequestResponses = batchRecallBaseService.batchRecallAndCache(realTimeSortBrandRequests);  
134 - RECALL_NEW_LOGGER.info("UserRecallCacheBean[2.3]-doRecallRealTimeSortBrand,requestCount is [{}], cost is [{}]", realTimeSortBrandRequests.size(), System.currentTimeMillis() - begin);  
135 - return realTimeSortBrandRequestResponses;  
136 - }, recallExecutorService);  
137 - }  
138 -  
139 -  
140 - /**  
141 - * 执行RNN向量【品牌+品类】的召回  
142 - *  
143 - * @param userRecallRequest  
144 - * @param userPersonalFactor  
145 - * @return  
146 - */  
147 - private CompletableFuture<List<RecallRequestResponse>> doRecallVectorRnnSortBrand(UserRecallRequest userRecallRequest, UserPersonalFactor userPersonalFactor) {  
148 - return CompletableFuture.supplyAsync(() -> {  
149 - long begin = System.currentTimeMillis();  
150 - List<RecallRequest> vectorRnnSortBrandRequests = sortBrandRecallRequestBuilder.buildSortBrandRecallRequests(userRecallRequest, userPersonalFactor.getVectorRnnSortBrandList(), SortBrandType.VEC_RNN_SORT_BRAND);  
151 - List<RecallRequestResponse> vecSortBrandRequestsResponses = batchRecallBaseService.batchRecallAndCache(vectorRnnSortBrandRequests);  
152 - RECALL_NEW_LOGGER.info("UserRecallCacheBean[2.4]-doRecallVectorRnnSortBrand,requestCount is [{}], cost is [{}]", vectorRnnSortBrandRequests.size(), System.currentTimeMillis() - begin);  
153 - return vecSortBrandRequestsResponses; 97 + List<RecallRequestResponse> requestResponses = sknRecallRecallRequestResponseBuilder.batchRecallBySknList(userRecallRequest, userPersonalFactor);
  98 + RECALL_NEW_LOGGER.info("batchRecall[2.1]-doRecallCommon,request size is [{}],cost is [{}]", requestResponses.size(), System.currentTimeMillis() - begin);
  99 + return requestResponses;
154 }, recallExecutorService); 100 }, recallExecutorService);
155 } 101 }
156 102
157 /** 103 /**
158 - * 执行w2v向量【品牌+品类】的召回 104 + * 执行推荐【品牌+品类】的召回
159 * 105 *
160 * @param userRecallRequest 106 * @param userRecallRequest
161 * @param userPersonalFactor 107 * @param userPersonalFactor
162 * @return 108 * @return
163 */ 109 */
164 - private CompletableFuture<List<RecallRequestResponse>> doRecallVectorW2vSortBrand(UserRecallRequest userRecallRequest, UserPersonalFactor userPersonalFactor) { 110 + private CompletableFuture<List<RecallRequestResponse>> doRecallSortBrand(UserRecallRequest userRecallRequest, UserPersonalFactor userPersonalFactor) {
165 return CompletableFuture.supplyAsync(() -> { 111 return CompletableFuture.supplyAsync(() -> {
166 long begin = System.currentTimeMillis(); 112 long begin = System.currentTimeMillis();
167 - List<RecallRequest> vectorW2vSortBrandRequests = sortBrandRecallRequestBuilder.buildSortBrandRecallRequests(userRecallRequest, userPersonalFactor.getVectorW2vSortBrandList(), SortBrandType.VEC_W2V_SORT_BRAND);  
168 - List<RecallRequestResponse> vecSortBrandRequestsResponses = batchRecallBaseService.batchRecallAndCache(vectorW2vSortBrandRequests);  
169 - RECALL_NEW_LOGGER.info("UserRecallCacheBean[2.5]-doRecallVectorW2vSortBrand,requestCount is [{}], cost is [{}]", vectorW2vSortBrandRequests.size(), System.currentTimeMillis() - begin); 113 + List<RecallRequest> requests = sortBrandRecallRequestBuilder.buildSortBrandRequests(userRecallRequest, userPersonalFactor);
  114 + List<RecallRequestResponse> vecSortBrandRequestsResponses = batchRecallBaseService.batchRecallAndCache(requests);
  115 + RECALL_NEW_LOGGER.info("batchRecall[2.1]-doRecallCommon,request size is [{}],cost is [{}]", requests.size(), System.currentTimeMillis() - begin);
170 return vecSortBrandRequestsResponses; 116 return vecSortBrandRequestsResponses;
171 }, recallExecutorService); 117 }, recallExecutorService);
172 } 118 }
173 119
174 - /**  
175 - * 执行配置的【品牌+品类】的召回  
176 - *  
177 - * @param userRecallRequest  
178 - * @param userPersonalFactor  
179 - * @return  
180 - */  
181 - private CompletableFuture<List<RecallRequestResponse>> doRecallConfigSortBrand(UserRecallRequest userRecallRequest, UserPersonalFactor userPersonalFactor) {  
182 - return CompletableFuture.supplyAsync(() -> {  
183 - long begin = System.currentTimeMillis();  
184 - List<RecallRequest> configSortBrandRequests = sortBrandRecallRequestBuilder.buildSortBrandRecallRequests(userRecallRequest, userPersonalFactor.getConfigSortBrandList(), SortBrandType.CONFIG_SORT_BRAND);  
185 - List<RecallRequestResponse> configBrandRequestsResponses = batchRecallBaseService.batchRecallAndCache(configSortBrandRequests);  
186 - RECALL_NEW_LOGGER.info("UserRecallCacheBean[2.6]-doRecallConfigSortBrand,requestCount is [{}], cost is [{}]", configSortBrandRequests.size(), System.currentTimeMillis() - begin);  
187 - return configBrandRequestsResponses;  
188 - }, recallExecutorService);  
189 - }  
190 -  
191 } 120 }
1 package com.yoho.search.service.recall.beans.requests; 1 package com.yoho.search.service.recall.beans.requests;
2 2
3 import com.yoho.search.core.personalized.models.SortBrand; 3 import com.yoho.search.core.personalized.models.SortBrand;
  4 +import com.yoho.search.models.recall.*;
4 import com.yoho.search.service.recall.beans.RecallConfigService; 5 import com.yoho.search.service.recall.beans.RecallConfigService;
5 import com.yoho.search.service.recall.strategy.IStrategy; 6 import com.yoho.search.service.recall.strategy.IStrategy;
6 import com.yoho.search.service.recall.strategy.SortBrandType; 7 import com.yoho.search.service.recall.strategy.SortBrandType;
7 -import com.yoho.search.models.recall.SortBrandSknCount;  
8 -import com.yoho.search.models.recall.ParamQueryFilter;  
9 -import com.yoho.search.models.recall.RecallRequest;  
10 -import com.yoho.search.models.recall.UserRecallRequest;  
11 import com.yoho.search.service.recall.strategy.impls.*; 8 import com.yoho.search.service.recall.strategy.impls.*;
12 import org.springframework.beans.factory.annotation.Autowired; 9 import org.springframework.beans.factory.annotation.Autowired;
13 import org.springframework.stereotype.Component; 10 import org.springframework.stereotype.Component;
@@ -21,6 +18,31 @@ public class SortBrandRecallRequestBuilder { @@ -21,6 +18,31 @@ public class SortBrandRecallRequestBuilder {
21 @Autowired 18 @Autowired
22 private RecallConfigService recallConfigService; 19 private RecallConfigService recallConfigService;
23 20
  21 + public List<RecallRequest> buildSortBrandRequests(UserRecallRequest userRecallRequest, UserPersonalFactor userPersonalFactor) {
  22 + List<RecallRequest> requests = new ArrayList<>();
  23 + requests.addAll(this.buildRealTimeSortBrandRequests(userRecallRequest, userPersonalFactor));
  24 + requests.addAll(this.buildVectorW2vSortBrandRequests(userRecallRequest, userPersonalFactor));
  25 + requests.addAll(this.buildVectorRnnSortBrandRequests(userRecallRequest, userPersonalFactor));
  26 + requests.addAll(this.buildConfigSortBrandRequests(userRecallRequest, userPersonalFactor));
  27 + return requests;
  28 + }
  29 +
  30 + private List<RecallRequest> buildRealTimeSortBrandRequests(UserRecallRequest userRecallRequest, UserPersonalFactor userPersonalFactor) {
  31 + return this.buildSortBrandRecallRequests(userRecallRequest, userPersonalFactor.getRealTimeSortBrandList(), SortBrandType.REC_SORT_BRAND);
  32 + }
  33 +
  34 + private List<RecallRequest> buildVectorW2vSortBrandRequests(UserRecallRequest userRecallRequest, UserPersonalFactor userPersonalFactor) {
  35 + return this.buildSortBrandRecallRequests(userRecallRequest,userPersonalFactor.getVectorW2vSortBrandList(), SortBrandType.VEC_W2V_SORT_BRAND);
  36 + }
  37 +
  38 + private List<RecallRequest> buildVectorRnnSortBrandRequests(UserRecallRequest userRecallRequest, UserPersonalFactor userPersonalFactor) {
  39 + return this.buildSortBrandRecallRequests(userRecallRequest, userPersonalFactor.getVectorRnnSortBrandList(), SortBrandType.VEC_RNN_SORT_BRAND);
  40 + }
  41 +
  42 + private List<RecallRequest> buildConfigSortBrandRequests(UserRecallRequest userRecallRequest, UserPersonalFactor userPersonalFactor) {
  43 + return this.buildSortBrandRecallRequests(userRecallRequest, userPersonalFactor.getConfigSortBrandList(), SortBrandType.CONFIG_SORT_BRAND);
  44 + }
  45 +
24 /** 46 /**
25 * 构造品牌+品类的召回请求 47 * 构造品牌+品类的召回请求
26 * 48 *
@@ -28,7 +50,7 @@ public class SortBrandRecallRequestBuilder { @@ -28,7 +50,7 @@ public class SortBrandRecallRequestBuilder {
28 * @param sortBrands 50 * @param sortBrands
29 * @return 51 * @return
30 */ 52 */
31 - public List<RecallRequest> buildSortBrandRecallRequests(UserRecallRequest userRecallRequest, List<SortBrand> sortBrands, SortBrandType sortBrandType) { 53 + private List<RecallRequest> buildSortBrandRecallRequests(UserRecallRequest userRecallRequest, List<SortBrand> sortBrands, SortBrandType sortBrandType) {
32 List<RecallRequest> requests = new ArrayList<>(); 54 List<RecallRequest> requests = new ArrayList<>();
33 // 1)、参数判断 55 // 1)、参数判断
34 if (sortBrands == null || sortBrands.isEmpty()) { 56 if (sortBrands == null || sortBrands.isEmpty()) {
@@ -39,7 +61,7 @@ public class SortBrandRecallRequestBuilder { @@ -39,7 +61,7 @@ public class SortBrandRecallRequestBuilder {
39 61
40 // 3)、参数构造 62 // 3)、参数构造
41 for (SortBrand sortBrand : sortBrands) { 63 for (SortBrand sortBrand : sortBrands) {
42 - SortBrandSknCount sortBrandSknCount = recallConfigService.queryRecallSknCount(userRecallRequest, sortBrandType,sortBrand); 64 + SortBrandSknCount sortBrandSknCount = recallConfigService.queryRecallSknCount(userRecallRequest, sortBrandType, sortBrand);
43 if (sortBrandSknCount == null) { 65 if (sortBrandSknCount == null) {
44 continue; 66 continue;
45 } 67 }