|
|
package com.yoho.search.recall.sort.helper;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
import org.apache.commons.collections.MapUtils;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.elasticsearch.index.query.BoolQueryBuilder;
|
|
|
import org.elasticsearch.index.query.QueryBuilder;
|
|
|
import org.elasticsearch.index.query.QueryBuilders;
|
|
|
import org.elasticsearch.search.sort.SortBuilder;
|
|
|
import org.elasticsearch.search.sort.SortBuilders;
|
|
|
import org.elasticsearch.search.sort.SortOrder;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.core.NamedThreadLocal;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import com.yoho.search.base.models.RecallType;
|
|
|
import com.yoho.search.base.utils.CollectionUtils;
|
|
|
import com.yoho.search.base.utils.ISearchConstants;
|
|
|
import com.yoho.search.base.utils.ProductIndexEsField;
|
|
|
import com.yoho.search.common.cache.aop.SearchCacheAble;
|
|
|
import com.yoho.search.core.es.model.SearchParam;
|
|
|
import com.yoho.search.core.es.model.SearchResult;
|
|
|
import com.yoho.search.service.base.SearchCommonService;
|
|
|
import com.yoho.search.service.base.SearchRequestParams;
|
|
|
import com.yoho.search.service.base.index.ProductIndexBaseService;
|
|
|
import com.yoho.search.service.helper.SearchCommonHelper;
|
|
|
import com.yoho.search.service.helper.SearchParamHelper;
|
|
|
import com.yoho.search.service.helper.SearchServiceHelper;
|
|
|
import com.yoho.search.recall.sort.AbstractRecallService;
|
|
|
import com.yoho.search.recall.sort.model.RecallProductInfoList;
|
|
|
import com.yoho.search.recall.sort.model.RecallProductSknList;
|
|
|
import com.yoho.search.recall.sort.model.RecallResult;
|
|
|
import com.yoho.search.recall.sort.model.RecallSearchParam;
|
|
|
import com.yoho.search.recall.sort.model.RecallSearchResult;
|
|
|
import com.yoho.search.recall.sort.strategy.IRecallStrategy;
|
|
|
|
|
|
@Component
|
|
|
public class RecallServiceHelper {
|
|
|
|
|
|
private static final Logger ERROR_LOGGER = LoggerFactory.getLogger(RecallServiceHelper.class);
|
|
|
|
|
|
private static final Logger RECALL_LOGGER = LoggerFactory.getLogger("RECALL");
|
|
|
|
|
|
private static final ThreadLocal<Boolean> logEnableThreadLocal = new NamedThreadLocal<Boolean>("RECALL USER LOG ENABLE");
|
|
|
|
|
|
@Autowired
|
|
|
private SearchCommonHelper searchCommonHelper;
|
|
|
@Autowired
|
|
|
private SearchCommonService searchCommonService;
|
|
|
@Autowired
|
|
|
private SearchServiceHelper searchServiceHelper;
|
|
|
@Autowired
|
|
|
private SearchParamHelper searchParamHelper;
|
|
|
@Autowired
|
|
|
private ProductIndexBaseService productIndexBaseService;
|
|
|
|
|
|
/**
|
|
|
* 按策略召回skn
|
|
|
*
|
|
|
* @param abstractRecallService
|
|
|
* @param paramMap
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
@SearchCacheAble(cacheInMinute = 10, cacheName = "SORT_PAGE_SKNS", returnClass = RecallProductSknList.class, excludeParams = { "page" })
|
|
|
public RecallProductSknList doRecallProductSkns(Map<String, String> paramMap, AbstractRecallService abstractRecallService) throws Exception {
|
|
|
long begin = System.currentTimeMillis();
|
|
|
// 1、获取召回策略
|
|
|
List<IRecallStrategy> recallStrategys = abstractRecallService.getRecallStrategys(paramMap);
|
|
|
this.doLogInfo("[func1=getRecallStrategys][cost={}ms]", System.currentTimeMillis() - begin);
|
|
|
|
|
|
// 2、获取召回参数
|
|
|
begin = System.currentTimeMillis();
|
|
|
List<RecallSearchParam> recallSearchParams = this.getRecallSearchParams(abstractRecallService, paramMap, recallStrategys);
|
|
|
this.doLogInfo("[func2=getRecallSearchParams][cost={}ms]", System.currentTimeMillis() - begin);
|
|
|
|
|
|
// 3、批量查询并获取总数
|
|
|
begin = System.currentTimeMillis();
|
|
|
RecallResult recallResult = this.doMutiQuery(recallSearchParams);
|
|
|
this.doLogInfo("[func3=doMutiQuery][cost={}ms][info is {} ]", System.currentTimeMillis() - begin, recallResult.doGetRecallSearchResultInfo());
|
|
|
long total = this.getTotalFromRecallResult(recallResult);
|
|
|
|
|
|
// 4、粗排-去重
|
|
|
begin = System.currentTimeMillis();
|
|
|
recallResult = abstractRecallService.doSketchyRank(paramMap, recallResult);
|
|
|
this.doLogInfo("[func4=doSketchyRank][cost={}ms][total={}]", System.currentTimeMillis() - begin, recallResult.getProductList().size());
|
|
|
|
|
|
// 5、精排-品牌打散
|
|
|
begin = System.currentTimeMillis();
|
|
|
recallResult = abstractRecallService.doCarefulRank(paramMap, recallResult);
|
|
|
this.doLogInfo("[func5=doCarefulRank][cost={}ms][total={}]", System.currentTimeMillis() - begin, recallResult.getProductList().size());
|
|
|
|
|
|
// 6、重排-处理firstSkn和直通车
|
|
|
begin = System.currentTimeMillis();
|
|
|
recallResult = abstractRecallService.doReRank(paramMap, recallResult);
|
|
|
this.doLogInfo("[func6=doReRank][cost={}ms][total={}]", System.currentTimeMillis() - begin, recallResult.getProductList().size());
|
|
|
|
|
|
// 7、构造返回结果
|
|
|
begin = System.currentTimeMillis();
|
|
|
List<Integer> productSknList = new ArrayList<Integer>();
|
|
|
for (Map<String, Object> product : recallResult.getProductList()) {
|
|
|
productSknList.add(MapUtils.getInteger(product, ProductIndexEsField.productSkn));
|
|
|
}
|
|
|
return new RecallProductSknList(total, productSknList);
|
|
|
}
|
|
|
|
|
|
// 生成召回参数
|
|
|
private List<RecallSearchParam> getRecallSearchParams(AbstractRecallService abstractRecallService, Map<String, String> paramMap, List<IRecallStrategy> recallStrategys) {
|
|
|
try {
|
|
|
// 1、获取参数中的paramFilter和paramQuery
|
|
|
BoolQueryBuilder paramFilter = searchServiceHelper.constructFilterBuilder(paramMap, null);
|
|
|
QueryBuilder paramQuery = searchServiceHelper.constructQueryBuilder(paramMap);
|
|
|
|
|
|
// 2、生成RecallSearchParam
|
|
|
List<RecallSearchParam> recallSearchParams = new ArrayList<RecallSearchParam>();
|
|
|
for (IRecallStrategy recallStrategy : recallStrategys) {
|
|
|
recallSearchParams.add(this.genRecallSearchParam(abstractRecallService, recallStrategy, paramFilter, paramQuery));
|
|
|
}
|
|
|
// 3、返回结果
|
|
|
return recallSearchParams;
|
|
|
} catch (Exception e) {
|
|
|
this.doLogError(e.getMessage(), e);
|
|
|
}
|
|
|
return new ArrayList<RecallSearchParam>();
|
|
|
}
|
|
|
|
|
|
private long getTotalFromRecallResult(RecallResult recallResult) {
|
|
|
for (RecallSearchResult recallSearchResult : recallResult.getRecallSearchResult()) {
|
|
|
if (recallSearchResult.getRecallType().equals(RecallType.COMMON)) {
|
|
|
return recallSearchResult.getTotal();
|
|
|
}
|
|
|
}
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
// 批量查询
|
|
|
private RecallResult doMutiQuery(List<RecallSearchParam> recallSearchParams) throws Exception {
|
|
|
List<SearchParam> searchParams = new ArrayList<SearchParam>();
|
|
|
for (RecallSearchParam recallSearchParam : recallSearchParams) {
|
|
|
searchParams.add(recallSearchParam.getSearchParam());
|
|
|
}
|
|
|
List<SearchResult> searchResults = searchCommonService.doMutiSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParams);
|
|
|
List<RecallSearchResult> recallSearchResults = new ArrayList<RecallSearchResult>();
|
|
|
for (int i = 0; i < recallSearchParams.size(); i++) {
|
|
|
recallSearchResults.add(new RecallSearchResult(recallSearchParams.get(i).getRecallType(), searchResults.get(i)));
|
|
|
}
|
|
|
RecallResult recallResult = new RecallResult(recallSearchResults);
|
|
|
return recallResult;
|
|
|
}
|
|
|
|
|
|
// 生成每种策略的召回参数
|
|
|
private RecallSearchParam genRecallSearchParam(AbstractRecallService abstractRecallService, IRecallStrategy recallStrategy, BoolQueryBuilder paramFilter,
|
|
|
QueryBuilder paramQuery) {
|
|
|
// 1、构造SearchParam
|
|
|
SearchParam searchParam = new SearchParam();
|
|
|
|
|
|
// 2、设置filter
|
|
|
BoolQueryBuilder filter = QueryBuilders.boolQuery();
|
|
|
if (paramFilter != null) {
|
|
|
filter.must(paramFilter);
|
|
|
}
|
|
|
if (recallStrategy != null && recallStrategy.filter() != null) {
|
|
|
filter.must(recallStrategy.filter());
|
|
|
}
|
|
|
searchParam.setFiter(filter);
|
|
|
|
|
|
// 3、设置query
|
|
|
if (paramQuery != null) {
|
|
|
searchParam.setQuery(paramQuery);
|
|
|
}
|
|
|
|
|
|
// 4、设置分页参数
|
|
|
searchParam.setOffset(0);
|
|
|
searchParam.setSize(recallStrategy.size());
|
|
|
|
|
|
// 5、设置排序
|
|
|
List<SortBuilder<?>> sortBuilders = new ArrayList<SortBuilder<?>>();
|
|
|
if (recallStrategy.firstSortBuilder() != null) {
|
|
|
sortBuilders.add(recallStrategy.firstSortBuilder());
|
|
|
}
|
|
|
sortBuilders.add(SortBuilders.fieldSort(ProductIndexEsField.shelveTime).order(SortOrder.DESC));
|
|
|
searchParam.setSortBuilders(sortBuilders);
|
|
|
|
|
|
// 6、设置返回的参数【节省带宽】
|
|
|
searchParam.setIncludeFields(abstractRecallService.getRecallFields());
|
|
|
|
|
|
return new RecallSearchParam(recallStrategy.recallType(), searchParam);
|
|
|
}
|
|
|
|
|
|
public RecallProductInfoList doQueryProductListBySkns(Map<String, String> paramMap, RecallProductSknList recallProductSknList, int page, int pageSize) throws Exception {
|
|
|
// 1、获取SKN列表
|
|
|
List<Integer> recalledSknList = recallProductSknList.getProductSknList();
|
|
|
// 2、判断召回的productSkn是否包含当前页码-保留第一个
|
|
|
int maxPage = recalledSknList.size() / pageSize;
|
|
|
if (maxPage == 0) {
|
|
|
maxPage = 1;
|
|
|
}
|
|
|
List<Map<String, Object>> productInfoList = null;
|
|
|
// 3、获取列表
|
|
|
long begin = System.currentTimeMillis();
|
|
|
if (page <= maxPage) {
|
|
|
productInfoList = this.queryProductListWithSort(recalledSknList, page, pageSize);
|
|
|
this.doLogInfo("[func7=queryProductListWithSort][cost={}ms][resultsize is {}]", System.currentTimeMillis() - begin, productInfoList.size());
|
|
|
} else {
|
|
|
productInfoList = this.queryProductListWithDefault(recalledSknList, paramMap, page - maxPage, pageSize);
|
|
|
this.doLogInfo("[func7=queryProductListWithDefault][cost={}ms][resultsize is {}]", System.currentTimeMillis() - begin, productInfoList.size());
|
|
|
}
|
|
|
// 4、返回结果
|
|
|
return new RecallProductInfoList(recallProductSknList.getTotal(), productInfoList);
|
|
|
}
|
|
|
|
|
|
public List<Map<String, Object>> doFillProductPricePlan(RecallProductInfoList recallProductInfoList) throws Exception {
|
|
|
long begin = System.currentTimeMillis();
|
|
|
List<Map<String, Object>> product_list = productIndexBaseService.getProductListWithPricePlan(recallProductInfoList.getProductInfoList());
|
|
|
this.doLogInfo("[func8=getProductListWithPricePlan][cost={}ms]", System.currentTimeMillis() - begin);
|
|
|
return product_list;
|
|
|
}
|
|
|
|
|
|
public void setLogEnableThreadLocal(Map<String, String> paramMap) {
|
|
|
int uid = searchCommonHelper.getUid(paramMap);
|
|
|
if (uid == 13420925) {
|
|
|
logEnableThreadLocal.set(true);
|
|
|
} else {
|
|
|
logEnableThreadLocal.set(false);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public void removelogEnableThreadLocal() {
|
|
|
logEnableThreadLocal.remove();
|
|
|
}
|
|
|
|
|
|
public void doLogInfo(String format, Object... arguments) {
|
|
|
if (logEnableThreadLocal.get() != null && logEnableThreadLocal.get().booleanValue()) {
|
|
|
RECALL_LOGGER.info(format, arguments);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public void doLogError(String msg, Throwable t) {
|
|
|
ERROR_LOGGER.error(msg, t);
|
|
|
}
|
|
|
|
|
|
public List<String> getFirstProductSkns(Map<String, String> paramMap) {
|
|
|
String firstProductSkns = MapUtils.getString(paramMap, SearchRequestParams.PARAM_SEARCH_FIRST_PRODUCRSKN, "");
|
|
|
String[] firstProductSknArray = firstProductSkns.split(",");
|
|
|
return CollectionUtils.arrayToList(firstProductSknArray);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 查询skn信息并按顺序排序
|
|
|
*
|
|
|
* @param recalledSknList
|
|
|
* @param page
|
|
|
* @param pageSize
|
|
|
* @return
|
|
|
*/
|
|
|
public List<Map<String, Object>> queryProductListWithSort(List<Integer> recalledSknList, int page, int pageSize) {
|
|
|
// 0、skn截取
|
|
|
int fromIndex = (page - 1) * pageSize;
|
|
|
int toIndex = page * pageSize;
|
|
|
if (toIndex > recalledSknList.size()) {
|
|
|
toIndex = recalledSknList.size();
|
|
|
}
|
|
|
List<Integer> querySknList = CollectionUtils.safeSubList(recalledSknList, fromIndex, toIndex);
|
|
|
// 1.构造搜索参数
|
|
|
SearchParam searchParam = new SearchParam();
|
|
|
searchParam.setFiter(QueryBuilders.boolQuery().must(QueryBuilders.termsQuery(ProductIndexEsField.productSkn, querySknList)));
|
|
|
searchParam.setOffset(0);
|
|
|
searchParam.setSize(querySknList.size());
|
|
|
searchParam.setIncludeFields(productIndexBaseService.getProductIndexIncludeFields());
|
|
|
// 2.查询ES
|
|
|
SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
|
|
|
if (searchResult == null) {
|
|
|
return new ArrayList<Map<String, Object>>();
|
|
|
}
|
|
|
// 3、排序截取
|
|
|
List<Map<String, Object>> productList = searchResult.getResultList();
|
|
|
Map<Integer, Map<String, Object>> productMap = new HashMap<Integer, Map<String, Object>>();
|
|
|
for (Map<String, Object> product : productList) {
|
|
|
productMap.put(MapUtils.getInteger(product, "productSkn", 0), product);
|
|
|
}
|
|
|
List<Map<String, Object>> realResults = new ArrayList<Map<String, Object>>();
|
|
|
for (Integer produtSkn : querySknList) {
|
|
|
if (!productMap.containsKey(produtSkn)) {
|
|
|
continue;
|
|
|
}
|
|
|
realResults.add(productMap.get(produtSkn));
|
|
|
}
|
|
|
return realResults;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 按默认方式排序
|
|
|
*
|
|
|
* @param notProductSkns
|
|
|
* @param paramMap
|
|
|
* @param realPage
|
|
|
* @param pageSize
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
private List<Map<String, Object>> queryProductListWithDefault(List<Integer> notProductSkns, Map<String, String> paramMap, int realPage, int pageSize) throws Exception {
|
|
|
// 1.构造filter
|
|
|
BoolQueryBuilder mustFilter = QueryBuilders.boolQuery();
|
|
|
mustFilter.mustNot(QueryBuilders.termsQuery(ProductIndexEsField.productSkn, notProductSkns));
|
|
|
// 2、构造参数
|
|
|
SearchParam searchParam = searchParamHelper.buildWithMustFilter(paramMap, mustFilter);
|
|
|
searchParam.setOffset((realPage - 1) * pageSize);
|
|
|
searchParam.setSize(pageSize);
|
|
|
searchParam.setIncludeFields(productIndexBaseService.getProductIndexIncludeFields());
|
|
|
|
|
|
// 3、构造sort
|
|
|
List<SortBuilder<?>> sortBuilders = new ArrayList<SortBuilder<?>>();
|
|
|
sortBuilders.add(SortBuilders.fieldSort(ProductIndexEsField.sevendayMoney).order(SortOrder.DESC));
|
|
|
sortBuilders.add(SortBuilders.fieldSort(ProductIndexEsField.id).order(SortOrder.DESC));
|
|
|
searchParam.setSortBuilders(sortBuilders);
|
|
|
|
|
|
// 4、查询es
|
|
|
SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
|
|
|
return searchResult.getResultList();
|
|
|
}
|
|
|
|
|
|
} |