Authored by 胡古飞

更改product_pool按id正序排序的导入方式

package com.yoho.search.service.servicenew.impl;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
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.stereotype.Service;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.core.es.model.SearchResult;
import com.yoho.search.service.cache.LocalCacheService;
import com.yoho.search.service.personalized.model.SearchFeature;
import com.yoho.search.service.service.SearchCommonService;
import com.yoho.search.service.service.helper.SearchServiceHelper;
import com.yoho.search.service.service.helper.SearchSortHelper;
import com.yoho.search.service.servicenew.IProductListService;
import com.yoho.search.service.servicenew.IProductPoolService;
import com.yoho.search.service.utils.LocalCacheKeyUtils;
import com.yoho.search.service.utils.SearchApiResultUtils;
import com.yoho.search.service.vo.SearchApiResult;
... ... @@ -40,17 +19,9 @@ public class ProductPoolServiceImpl implements IProductPoolService {
@Autowired
private IProductListService productListService;
@Autowired
private SearchServiceHelper searchServiceHelper;
@Autowired
private LocalCacheService localCacheService;
@Autowired
private SearchCommonService searchCommonService;
@Autowired
private SearchSortHelper searchSortHelper;
// discount_score:desc --> product_pool_detail_id:asc
// discount_score:desc --> pools.id:asc
@Override
public SearchApiResult productPool(Map<String, String> paramMap) {
try {
... ... @@ -71,86 +42,4 @@ public class ProductPoolServiceImpl implements IProductPoolService {
return SearchApiResultUtils.errorSearchApiResult("productPool", paramMap, e);
}
}
/**
* 按id顺寻获取商品池商品
*
* @param paramMap
* @return
* @throws Exception
*/
private JSONObject getProductPoolSknListOrderByDefault(Map<String, String> paramMap, int page, int pageSize) {
try {
// 检测分页参数
// 解析参数转化为检索条件--SearchParam
SearchParam searchParam = new SearchParam();
// 设置查询条数
searchParam.setPage(page);
searchParam.setOffset((page - 1) * pageSize);
searchParam.setSize(pageSize);
// 构建filter
searchParam.setFiter(searchServiceHelper.constructFilterBuilder(paramMap, null));
// 构建排序对象
List<SortBuilder> sortBuilders = new ArrayList<SortBuilder>();
sortBuilders.add(SortBuilders.fieldSort("id").order(SortOrder.ASC));
searchParam.setSortBuilders(sortBuilders);
// 对productpool索引检索
final String indexName = ISearchConstants.INDEX_NAME_PRODUCT_POOL;
String localCacheKey = LocalCacheKeyUtils.guavaCacheKey(indexName, searchParam);
JSONObject result = localCacheService.getOrAddToLocalCache(localCacheKey, new Callable<JSONObject>() {
@Override
public JSONObject call() throws Exception {
SearchResult searchResult = searchCommonService.doSearch(indexName, searchParam);
// 将searchResult转化为map返回--需要把aggregation转化为需要的结构
if (searchResult == null) {
return new JSONObject();
}
JSONObject result = new JSONObject();
List<Integer> productSknList = new ArrayList<>();
for (Map<String, Object> map : searchResult.getResultList()) {
Object productSkn = map.get("productSkn");
if (productSkn != null) {
productSknList.add(Integer.valueOf(productSkn.toString()));
}
}
result.put("total", searchResult.getTotal());
result.put("page", searchResult.getPage());
result.put("page_total", searchResult.getTotalPage());
result.put("productSknList", productSknList);
return result;
}
});
return result;
} catch (Exception e) {
logger.error(e.getMessage(), e);
return new JSONObject();
}
}
/**
* 构建按skn排序的QueryBuilder
*
* @param paramMap
* @return
*/
private QueryBuilder constructQueryBuilderWithPoolId(Map<String, String> paramMap, JSONArray productSknList) {
QueryBuilder queryBuilder = QueryBuilders.matchAllQuery();
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(queryBuilder);
List<SearchFeature> searchFeatures = new ArrayList<>();
int size = productSknList.size();
for (int i = 0; i < productSknList.size(); i++) {
String productSkn = productSknList.get(i).toString();
int boost = size - i;
SearchFeature searchFeature = new SearchFeature("productSkn.productSkn_ansj", productSkn, boost * boost * boost);
searchFeatures.add(searchFeature);
}
for (SearchFeature entry : searchFeatures) {
boolQueryBuilder.should(QueryBuilders.termQuery(entry.getTargetParam(), entry.getParamValues()).boost(entry.getBoost()));
}
return boolQueryBuilder;
}
}
... ...