|
|
package com.yoho.search.recall.scene.beans.cache;
|
|
|
|
|
|
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.recall.scene.beans.helper.RecallResponseHelper;
|
|
|
import com.yoho.search.recall.scene.beans.strategy.impls.RecommendProductStrategy;
|
|
|
import com.yoho.search.recall.scene.models.common.IRecallRequest;
|
|
|
import com.yoho.search.recall.scene.models.req.*;
|
|
|
import com.yoho.search.service.base.SearchCommonService;
|
|
|
import org.apache.commons.collections.MapUtils;
|
|
|
import org.elasticsearch.index.query.QueryBuilders;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
@Component
|
|
|
public class RecommendProductRecallCacheBean extends AbstractCacheBean<RecommendProductRequest, RecallResponse, RecommendProductRequestResponse> {
|
|
|
|
|
|
@Autowired
|
|
|
private SearchCommonService searchCommonService;
|
|
|
|
|
|
/**
|
|
|
* 按productId召回的入口
|
|
|
* @param productIds
|
|
|
* @return
|
|
|
*/
|
|
|
public List<RecallRequestResponse> batchRecallAndCache(List<Integer> productIds) {
|
|
|
//1、参数判断
|
|
|
if(productIds==null || productIds.isEmpty()){
|
|
|
return new ArrayList<>();
|
|
|
}
|
|
|
|
|
|
//2、执行查询
|
|
|
List<RecommendProductRequestResponse> responses = this.batchQuery(productIds);
|
|
|
|
|
|
//3、数据转换
|
|
|
List<RecallRequestResponse> results = new ArrayList<>();
|
|
|
for (RecommendProductRequestResponse recommendProductRequestResponse: responses){
|
|
|
RecommendProductRequest request = recommendProductRequestResponse.getRequest();
|
|
|
RecallResponse response = recommendProductRequestResponse.getResponse();
|
|
|
if(request==null || response==null){
|
|
|
continue;
|
|
|
}
|
|
|
Integer productId = request.getProductId();
|
|
|
RecallRequest recallRequest = new RecallRequest(new RecommendProductStrategy(productId));
|
|
|
results.add(new RecallRequestResponse(recallRequest,response));
|
|
|
}
|
|
|
return results;
|
|
|
}
|
|
|
|
|
|
private List<RecommendProductRequestResponse> batchQuery(List<Integer> productIds){
|
|
|
//1、构造结果
|
|
|
final List<RecommendProductRequestResponse> results = new ArrayList<>();
|
|
|
for (Integer productId : productIds) {
|
|
|
results.add(new RecommendProductRequestResponse(new RecommendProductRequest(productId)));
|
|
|
}
|
|
|
//2、调父类方法
|
|
|
super.bacthFillResponseWithCache(results,productIds.size());
|
|
|
//3、返回结果
|
|
|
return results;
|
|
|
}
|
|
|
|
|
|
public void batchAddProductRecallInfoToCache(Map<Integer,RecallResponse> productVectors){
|
|
|
List<RecommendProductRequestResponse> results = new ArrayList<>();
|
|
|
for (Map.Entry<Integer,RecallResponse> entry: productVectors.entrySet()) {
|
|
|
RecommendProductRequestResponse result = new RecommendProductRequestResponse(new RecommendProductRequest(entry.getKey()));
|
|
|
result.setResponse(entry.getValue(),true);
|
|
|
results.add(result);
|
|
|
}
|
|
|
super.batchAddResponseToCache(results);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected boolean useEhCache() {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected Map<RecommendProductRequest, RecallResponse> queryMissCacheRequestResults(List<RecommendProductRequestResponse> missCacheRequests) {
|
|
|
//1、合法性判断
|
|
|
Map<RecommendProductRequest,RecallResponse> results = new HashMap<>();
|
|
|
if(missCacheRequests==null||missCacheRequests.isEmpty()){
|
|
|
return results;
|
|
|
}
|
|
|
|
|
|
//2、获取productId
|
|
|
List<Integer> productIds = new ArrayList<>();
|
|
|
for (RecommendProductRequestResponse recommendProductRequestResponse : missCacheRequests) {
|
|
|
productIds.add(recommendProductRequestResponse.getRequest().getProductId());
|
|
|
}
|
|
|
|
|
|
//3、构建SearchParam并查询
|
|
|
SearchParam searchParam = new SearchParam();
|
|
|
searchParam.setOffset(0);
|
|
|
searchParam.setSize(productIds.size());
|
|
|
searchParam.setFiter(QueryBuilders.termsQuery(ProductIndexEsField.productId, productIds));
|
|
|
searchParam.setIncludeFields( missCacheRequests.get(0).getRequest().includeFields());
|
|
|
SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
|
|
|
|
|
|
//4、构建基于ProductId的临时结果
|
|
|
Map<Integer,RecallResponse> productTempMap = new HashMap<>();
|
|
|
for (Map<String, Object> productInfo: searchResult.getResultList()){
|
|
|
Integer productId = MapUtils.getIntValue(productInfo,ProductIndexEsField.productId,0);
|
|
|
RecallResponse.RecallSkn recallSkn = RecallResponseHelper.buildRecallSkn(productInfo);
|
|
|
RecallResponse recallResponse = RecallResponseHelper.buildRecallResponse(recallSkn);
|
|
|
productTempMap.put(productId,recallResponse);
|
|
|
}
|
|
|
//5、构造最终结果
|
|
|
for (RecommendProductRequestResponse recommendProductRequestResponse :missCacheRequests ) {
|
|
|
results.put(recommendProductRequestResponse.getRequest(),productTempMap.get(recommendProductRequestResponse.getRequest().getProductId()));
|
|
|
}
|
|
|
return results;
|
|
|
}
|
|
|
|
|
|
} |
|
|
package com.yoho.search.recall.scene.beans.cache;
|
|
|
|
|
|
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.recall.scene.beans.helper.RecallResponseHelper;
|
|
|
import com.yoho.search.recall.scene.beans.strategy.impls.RecommendProductStrategy;
|
|
|
import com.yoho.search.recall.scene.models.common.IRecallRequest;
|
|
|
import com.yoho.search.recall.scene.models.req.*;
|
|
|
import com.yoho.search.service.base.SearchCommonService;
|
|
|
import org.apache.commons.collections.MapUtils;
|
|
|
import org.elasticsearch.index.query.QueryBuilders;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
@Component
|
|
|
public class RecommendProductRecallCacheBean extends AbstractCacheBean<RecommendProductRequest, RecallResponse, RecommendProductRequestResponse> {
|
|
|
|
|
|
@Autowired
|
|
|
private SearchCommonService searchCommonService;
|
|
|
|
|
|
/**
|
|
|
* 按productId召回的入口
|
|
|
* @param productIds
|
|
|
* @return
|
|
|
*/
|
|
|
public List<RecallRequestResponse> batchRecallAndCache(List<Integer> productIds) {
|
|
|
//1、参数判断
|
|
|
if(productIds==null || productIds.isEmpty()){
|
|
|
return new ArrayList<>();
|
|
|
}
|
|
|
|
|
|
//2、执行查询
|
|
|
List<RecommendProductRequestResponse> responses = this.batchQuery(productIds);
|
|
|
|
|
|
//3、数据转换
|
|
|
List<RecallRequestResponse> results = new ArrayList<>();
|
|
|
for (RecommendProductRequestResponse recommendProductRequestResponse: responses){
|
|
|
RecommendProductRequest request = recommendProductRequestResponse.getRequest();
|
|
|
RecallResponse response = recommendProductRequestResponse.getResponse();
|
|
|
if(request==null || response==null){
|
|
|
continue;
|
|
|
}
|
|
|
Integer productId = request.getProductId();
|
|
|
RecallRequest recallRequest = new RecallRequest(new RecommendProductStrategy(productId));
|
|
|
results.add(new RecallRequestResponse(recallRequest,response));
|
|
|
}
|
|
|
return results;
|
|
|
}
|
|
|
|
|
|
private List<RecommendProductRequestResponse> batchQuery(List<Integer> productIds){
|
|
|
//1、构造结果
|
|
|
final List<RecommendProductRequestResponse> results = new ArrayList<>();
|
|
|
for (Integer productId : productIds) {
|
|
|
results.add(new RecommendProductRequestResponse(new RecommendProductRequest(productId)));
|
|
|
}
|
|
|
//2、调父类方法
|
|
|
super.bacthFillResponseWithCache(results,productIds.size());
|
|
|
//3、返回结果
|
|
|
return results;
|
|
|
}
|
|
|
|
|
|
public void batchAddProductRecallInfoToCache(Map<Integer,RecallResponse> productVectors){
|
|
|
List<RecommendProductRequestResponse> results = new ArrayList<>();
|
|
|
for (Map.Entry<Integer,RecallResponse> entry: productVectors.entrySet()) {
|
|
|
RecommendProductRequestResponse result = new RecommendProductRequestResponse(new RecommendProductRequest(entry.getKey()));
|
|
|
result.setResponse(entry.getValue(),true);
|
|
|
results.add(result);
|
|
|
}
|
|
|
super.batchAddResponseToCache(results);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected boolean useEhCache() {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected Map<RecommendProductRequest, RecallResponse> queryMissCacheRequestResults(List<RecommendProductRequestResponse> missCacheRequests) {
|
|
|
//1、合法性判断
|
|
|
Map<RecommendProductRequest,RecallResponse> results = new HashMap<>();
|
|
|
if(missCacheRequests==null||missCacheRequests.isEmpty()){
|
|
|
return results;
|
|
|
}
|
|
|
|
|
|
//2、获取productId
|
|
|
List<Integer> productIds = new ArrayList<>();
|
|
|
for (RecommendProductRequestResponse recommendProductRequestResponse : missCacheRequests) {
|
|
|
productIds.add(recommendProductRequestResponse.getRequest().getProductId());
|
|
|
}
|
|
|
|
|
|
//3、构建SearchParam并查询
|
|
|
SearchParam searchParam = new SearchParam();
|
|
|
searchParam.setOffset(0);
|
|
|
searchParam.setSize(productIds.size());
|
|
|
searchParam.setFiter(QueryBuilders.termsQuery(ProductIndexEsField.productId, productIds));
|
|
|
searchParam.setIncludeFields( missCacheRequests.get(0).getRequest().includeFields());
|
|
|
SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
|
|
|
|
|
|
//4、构建基于ProductId的临时结果
|
|
|
Map<Integer,RecallResponse> productTempMap = new HashMap<>();
|
|
|
for (Map<String, Object> productInfo: searchResult.getResultList()){
|
|
|
Integer productId = MapUtils.getIntValue(productInfo,ProductIndexEsField.productId,0);
|
|
|
RecallResponse.RecallSkn recallSkn = RecallResponseHelper.buildRecallSkn(productInfo);
|
|
|
RecallResponse recallResponse = RecallResponseHelper.buildRecallResponse(recallSkn);
|
|
|
productTempMap.put(productId,recallResponse);
|
|
|
}
|
|
|
//5、构造最终结果
|
|
|
for (RecommendProductRequestResponse recommendProductRequestResponse :missCacheRequests ) {
|
|
|
results.put(recommendProductRequestResponse.getRequest(),productTempMap.get(recommendProductRequestResponse.getRequest().getProductId()));
|
|
|
}
|
|
|
return results;
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|