Authored by hugufei

品类品牌召回代码优化

package com.yoho.search.service.recall;
import com.yoho.search.models.recall.*;
import com.yoho.search.service.base.BatchRecallBaseService;
import com.yoho.search.service.recall.beans.requests.CommonRecallRequestBuilder;
import com.yoho.search.service.recall.beans.requests.SortBrandRecallRequestBuilder;
import com.yoho.search.service.recall.beans.requests.SknRecallRecallRequestResponseBuilder;
import com.yoho.search.models.recall.PagePersonalFactor;
import com.yoho.search.models.recall.UserPersonalFactor;
import com.yoho.search.models.recall.RecallRequest;
import com.yoho.search.models.recall.RecallRequestResponse;
import com.yoho.search.models.recall.UserRecallRequest;
import com.yoho.search.service.recall.strategy.SortBrandType;
import com.yoho.search.service.recall.beans.requests.SortBrandRecallRequestBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
... ... @@ -54,27 +49,13 @@ class BatchRequestResponseEntrace {
CompletableFuture<List<RecallRequestResponse>> commonCompletableFuture = this.doRecallCommon(userRecallRequest, pagePersonalFactor);
//3、处理实时推荐的品类品牌的召回
CompletableFuture<List<RecallRequestResponse>> realTimeSortBrandCompletableFuture = this.doRecallRealTimeSortBrand(userRecallRequest, userPersonalFactor);
//4、处理基于W2V向量生成的品类品牌的召回
CompletableFuture<List<RecallRequestResponse>> vectorW2vSortBrandCompletableFuture = this.doRecallVectorW2vSortBrand(userRecallRequest, userPersonalFactor);
CompletableFuture<List<RecallRequestResponse>> sortBrandCompletableFuture = this.doRecallSortBrand(userRecallRequest, userPersonalFactor);
//5、处理基于RNN向量生成的品类品牌的召回
CompletableFuture<List<RecallRequestResponse>> vectorRnnSortBrandCompletableFuture = this.doRecallVectorRnnSortBrand(userRecallRequest, userPersonalFactor);
//6、处理基于RNN向量生成的品类品牌的召回
CompletableFuture<List<RecallRequestResponse>> configSortBrandCompletableFuture = this.doRecallConfigSortBrand(userRecallRequest, userPersonalFactor);
//7、构造最终返回结果投入额
//4、构造最终返回结果投入额
List<RecallRequestResponse> batchRequestResults = new ArrayList<>();
batchRequestResults.addAll(this.getResultFromCompletableFuture(sknListCompletableFuture));//按skn召回放在第一个,不然merger的时候可能会无序
batchRequestResults.addAll(this.getResultFromCompletableFuture(commonCompletableFuture));
batchRequestResults.addAll(this.getResultFromCompletableFuture(realTimeSortBrandCompletableFuture));
batchRequestResults.addAll(this.getResultFromCompletableFuture(vectorW2vSortBrandCompletableFuture));
batchRequestResults.addAll(this.getResultFromCompletableFuture(vectorRnnSortBrandCompletableFuture));
batchRequestResults.addAll(this.getResultFromCompletableFuture(configSortBrandCompletableFuture));
batchRequestResults.addAll(this.getResultFromCompletableFuture(sortBrandCompletableFuture));
return batchRequestResults;
}
... ... @@ -96,15 +77,15 @@ class BatchRequestResponseEntrace {
private CompletableFuture<List<RecallRequestResponse>> doRecallCommon(UserRecallRequest userRecallRequest, PagePersonalFactor pagePersonalFactor) {
return CompletableFuture.supplyAsync(() -> {
long begin = System.currentTimeMillis();
List<RecallRequest> commonRequests = commonRequestBuilder.buildCommonRecallRequests(userRecallRequest, pagePersonalFactor);
List<RecallRequestResponse> commonRequestResponses = batchRecallBaseService.batchRecallAndCache(commonRequests);
RECALL_NEW_LOGGER.info("UserRecallCacheBean[2.1]-doRecallCommon,requestCount is [{}], cost is [{}]", commonRequests.size(), System.currentTimeMillis() - begin);
return commonRequestResponses;
List<RecallRequest> requests = commonRequestBuilder.buildCommonRecallRequests(userRecallRequest, pagePersonalFactor);
List<RecallRequestResponse> requestResponses = batchRecallBaseService.batchRecallAndCache(requests);
RECALL_NEW_LOGGER.info("batchRecall[2.1]-doRecallCommon,request size is [{}],cost is [{}]", requests.size(), System.currentTimeMillis() - begin);
return requestResponses;
}, recallExecutorService);
}
/**
* 执行实时推荐SKN的召回
* 执行推荐SKN的召回
*
* @param userRecallRequest
* @param userPersonalFactor
... ... @@ -113,79 +94,27 @@ class BatchRequestResponseEntrace {
private CompletableFuture<List<RecallRequestResponse>> doRecallSknList(UserRecallRequest userRecallRequest, UserPersonalFactor userPersonalFactor) {
return CompletableFuture.supplyAsync(() -> {
long begin = System.currentTimeMillis();
List<RecallRequestResponse> recommendSknRequestResponses = sknRecallRecallRequestResponseBuilder.batchRecallBySknList(userRecallRequest, userPersonalFactor);
RECALL_NEW_LOGGER.info("UserRecallCacheBean[2.2]-doRecallRecommendSkn,recommendSknCount is [{}],cost is [{}]", recommendSknRequestResponses.size(), System.currentTimeMillis() - begin);
return recommendSknRequestResponses;
}, recallExecutorService);
}
/**
* 执行实时【品牌+品类】的召回
*
* @param userRecallRequest
* @param userPersonalFactor
* @return
*/
private CompletableFuture<List<RecallRequestResponse>> doRecallRealTimeSortBrand(UserRecallRequest userRecallRequest, UserPersonalFactor userPersonalFactor) {
return CompletableFuture.supplyAsync(() -> {
long begin = System.currentTimeMillis();
List<RecallRequest> realTimeSortBrandRequests = sortBrandRecallRequestBuilder.buildSortBrandRecallRequests(userRecallRequest, userPersonalFactor.getRealTimeSortBrandList(), SortBrandType.REC_SORT_BRAND);
List<RecallRequestResponse> realTimeSortBrandRequestResponses = batchRecallBaseService.batchRecallAndCache(realTimeSortBrandRequests);
RECALL_NEW_LOGGER.info("UserRecallCacheBean[2.3]-doRecallRealTimeSortBrand,requestCount is [{}], cost is [{}]", realTimeSortBrandRequests.size(), System.currentTimeMillis() - begin);
return realTimeSortBrandRequestResponses;
}, recallExecutorService);
}
/**
* 执行RNN向量【品牌+品类】的召回
*
* @param userRecallRequest
* @param userPersonalFactor
* @return
*/
private CompletableFuture<List<RecallRequestResponse>> doRecallVectorRnnSortBrand(UserRecallRequest userRecallRequest, UserPersonalFactor userPersonalFactor) {
return CompletableFuture.supplyAsync(() -> {
long begin = System.currentTimeMillis();
List<RecallRequest> vectorRnnSortBrandRequests = sortBrandRecallRequestBuilder.buildSortBrandRecallRequests(userRecallRequest, userPersonalFactor.getVectorRnnSortBrandList(), SortBrandType.VEC_RNN_SORT_BRAND);
List<RecallRequestResponse> vecSortBrandRequestsResponses = batchRecallBaseService.batchRecallAndCache(vectorRnnSortBrandRequests);
RECALL_NEW_LOGGER.info("UserRecallCacheBean[2.4]-doRecallVectorRnnSortBrand,requestCount is [{}], cost is [{}]", vectorRnnSortBrandRequests.size(), System.currentTimeMillis() - begin);
return vecSortBrandRequestsResponses;
List<RecallRequestResponse> requestResponses = sknRecallRecallRequestResponseBuilder.batchRecallBySknList(userRecallRequest, userPersonalFactor);
RECALL_NEW_LOGGER.info("batchRecall[2.1]-doRecallCommon,request size is [{}],cost is [{}]", requestResponses.size(), System.currentTimeMillis() - begin);
return requestResponses;
}, recallExecutorService);
}
/**
* 执行w2v向量【品牌+品类】的召回
* 执行推荐【品牌+品类】的召回
*
* @param userRecallRequest
* @param userPersonalFactor
* @return
*/
private CompletableFuture<List<RecallRequestResponse>> doRecallVectorW2vSortBrand(UserRecallRequest userRecallRequest, UserPersonalFactor userPersonalFactor) {
private CompletableFuture<List<RecallRequestResponse>> doRecallSortBrand(UserRecallRequest userRecallRequest, UserPersonalFactor userPersonalFactor) {
return CompletableFuture.supplyAsync(() -> {
long begin = System.currentTimeMillis();
List<RecallRequest> vectorW2vSortBrandRequests = sortBrandRecallRequestBuilder.buildSortBrandRecallRequests(userRecallRequest, userPersonalFactor.getVectorW2vSortBrandList(), SortBrandType.VEC_W2V_SORT_BRAND);
List<RecallRequestResponse> vecSortBrandRequestsResponses = batchRecallBaseService.batchRecallAndCache(vectorW2vSortBrandRequests);
RECALL_NEW_LOGGER.info("UserRecallCacheBean[2.5]-doRecallVectorW2vSortBrand,requestCount is [{}], cost is [{}]", vectorW2vSortBrandRequests.size(), System.currentTimeMillis() - begin);
List<RecallRequest> requests = sortBrandRecallRequestBuilder.buildSortBrandRequests(userRecallRequest, userPersonalFactor);
List<RecallRequestResponse> vecSortBrandRequestsResponses = batchRecallBaseService.batchRecallAndCache(requests);
RECALL_NEW_LOGGER.info("batchRecall[2.1]-doRecallCommon,request size is [{}],cost is [{}]", requests.size(), System.currentTimeMillis() - begin);
return vecSortBrandRequestsResponses;
}, recallExecutorService);
}
/**
* 执行配置的【品牌+品类】的召回
*
* @param userRecallRequest
* @param userPersonalFactor
* @return
*/
private CompletableFuture<List<RecallRequestResponse>> doRecallConfigSortBrand(UserRecallRequest userRecallRequest, UserPersonalFactor userPersonalFactor) {
return CompletableFuture.supplyAsync(() -> {
long begin = System.currentTimeMillis();
List<RecallRequest> configSortBrandRequests = sortBrandRecallRequestBuilder.buildSortBrandRecallRequests(userRecallRequest, userPersonalFactor.getConfigSortBrandList(), SortBrandType.CONFIG_SORT_BRAND);
List<RecallRequestResponse> configBrandRequestsResponses = batchRecallBaseService.batchRecallAndCache(configSortBrandRequests);
RECALL_NEW_LOGGER.info("UserRecallCacheBean[2.6]-doRecallConfigSortBrand,requestCount is [{}], cost is [{}]", configSortBrandRequests.size(), System.currentTimeMillis() - begin);
return configBrandRequestsResponses;
}, recallExecutorService);
}
}
... ...
package com.yoho.search.service.recall.beans.requests;
import com.yoho.search.core.personalized.models.SortBrand;
import com.yoho.search.models.recall.*;
import com.yoho.search.service.recall.beans.RecallConfigService;
import com.yoho.search.service.recall.strategy.IStrategy;
import com.yoho.search.service.recall.strategy.SortBrandType;
import com.yoho.search.models.recall.SortBrandSknCount;
import com.yoho.search.models.recall.ParamQueryFilter;
import com.yoho.search.models.recall.RecallRequest;
import com.yoho.search.models.recall.UserRecallRequest;
import com.yoho.search.service.recall.strategy.impls.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
... ... @@ -21,6 +18,31 @@ public class SortBrandRecallRequestBuilder {
@Autowired
private RecallConfigService recallConfigService;
public List<RecallRequest> buildSortBrandRequests(UserRecallRequest userRecallRequest, UserPersonalFactor userPersonalFactor) {
List<RecallRequest> requests = new ArrayList<>();
requests.addAll(this.buildRealTimeSortBrandRequests(userRecallRequest, userPersonalFactor));
requests.addAll(this.buildVectorW2vSortBrandRequests(userRecallRequest, userPersonalFactor));
requests.addAll(this.buildVectorRnnSortBrandRequests(userRecallRequest, userPersonalFactor));
requests.addAll(this.buildConfigSortBrandRequests(userRecallRequest, userPersonalFactor));
return requests;
}
private List<RecallRequest> buildRealTimeSortBrandRequests(UserRecallRequest userRecallRequest, UserPersonalFactor userPersonalFactor) {
return this.buildSortBrandRecallRequests(userRecallRequest, userPersonalFactor.getRealTimeSortBrandList(), SortBrandType.REC_SORT_BRAND);
}
private List<RecallRequest> buildVectorW2vSortBrandRequests(UserRecallRequest userRecallRequest, UserPersonalFactor userPersonalFactor) {
return this.buildSortBrandRecallRequests(userRecallRequest,userPersonalFactor.getVectorW2vSortBrandList(), SortBrandType.VEC_W2V_SORT_BRAND);
}
private List<RecallRequest> buildVectorRnnSortBrandRequests(UserRecallRequest userRecallRequest, UserPersonalFactor userPersonalFactor) {
return this.buildSortBrandRecallRequests(userRecallRequest, userPersonalFactor.getVectorRnnSortBrandList(), SortBrandType.VEC_RNN_SORT_BRAND);
}
private List<RecallRequest> buildConfigSortBrandRequests(UserRecallRequest userRecallRequest, UserPersonalFactor userPersonalFactor) {
return this.buildSortBrandRecallRequests(userRecallRequest, userPersonalFactor.getConfigSortBrandList(), SortBrandType.CONFIG_SORT_BRAND);
}
/**
* 构造品牌+品类的召回请求
*
... ... @@ -28,7 +50,7 @@ public class SortBrandRecallRequestBuilder {
* @param sortBrands
* @return
*/
public List<RecallRequest> buildSortBrandRecallRequests(UserRecallRequest userRecallRequest, List<SortBrand> sortBrands, SortBrandType sortBrandType) {
private List<RecallRequest> buildSortBrandRecallRequests(UserRecallRequest userRecallRequest, List<SortBrand> sortBrands, SortBrandType sortBrandType) {
List<RecallRequest> requests = new ArrayList<>();
// 1)、参数判断
if (sortBrands == null || sortBrands.isEmpty()) {
... ... @@ -39,7 +61,7 @@ public class SortBrandRecallRequestBuilder {
// 3)、参数构造
for (SortBrand sortBrand : sortBrands) {
SortBrandSknCount sortBrandSknCount = recallConfigService.queryRecallSknCount(userRecallRequest, sortBrandType,sortBrand);
SortBrandSknCount sortBrandSknCount = recallConfigService.queryRecallSknCount(userRecallRequest, sortBrandType, sortBrand);
if (sortBrandSknCount == null) {
continue;
}
... ...