|
|
package com.yoho.search.service.scene;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
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.JSONObject;
|
|
|
import com.yoho.search.base.utils.ISearchConstants;
|
|
|
import com.yoho.search.base.utils.ProductIndexEsField;
|
|
|
import com.yoho.search.base.utils.SearchPageIdDefine;
|
|
|
import com.yoho.search.common.cache.SearchCacheMatchLogger;
|
|
|
import com.yoho.search.common.cache.model.SearchCache;
|
|
|
import com.yoho.search.core.es.model.SearchParam;
|
|
|
import com.yoho.search.core.es.model.SearchResult;
|
|
|
import com.yoho.search.models.SearchApiResult;
|
|
|
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.SearchParamHelper;
|
|
|
import com.yoho.search.service.helper.SearchSortHelper;
|
|
|
import com.yoho.search.service.scene.common.AbstractCacheAbleService;
|
|
|
|
|
|
@Service
|
|
|
public class HotSaleRankSceneService extends AbstractCacheAbleService {
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(HotSaleRankSceneService.class);
|
|
|
|
|
|
@Autowired
|
|
|
private SearchCommonService searchCommonService;
|
|
|
@Autowired
|
|
|
private ProductIndexBaseService productIndexBaseService;
|
|
|
@Autowired
|
|
|
private SearchParamHelper searchParamHelper;
|
|
|
@Autowired
|
|
|
private SearchSortHelper searchSortHelper;
|
|
|
|
|
|
@Override
|
|
|
public SearchCache getSearchCache() {
|
|
|
return searchCacheFactory.getHotSaleRankSearchCache();
|
|
|
}
|
|
|
|
|
|
private void addDefaultParam(Map<String, String> paramMap) {
|
|
|
paramMap.put(SearchRequestParams.PARAM_SEARCH_PAGEID, SearchPageIdDefine.PAGE_ID_HOTSALERANK);// 根据场景划分的页面id
|
|
|
paramMap.put(SearchRequestParams.PARAM_SEARCH_GLOBAL_FILTER_BRAND, "Y");// 页面屏蔽
|
|
|
paramMap.put(SearchRequestParams.PARAM_SEARCH_STATUS, "1");// 上架
|
|
|
paramMap.put(SearchRequestParams.PARAM_SEARCH_STOCKNUM, "1");// 有库存
|
|
|
paramMap.put(SearchRequestParams.PARAM_SEARCH_ISOUTLETS, "2");// 非奥莱
|
|
|
paramMap.put(SearchRequestParams.PARAM_SEARCH_ATTRIBUTE_NOT, "2");// 非赠品
|
|
|
paramMap.put(SearchRequestParams.PARAM_SEARCH_BREAKING, "0");// 非断码
|
|
|
//paramMap.put(SearchRequestParams.PARAM_SEARCH_SHOWSOLDOUT, "1");// 显示延期显示的商品
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 热卖排行榜
|
|
|
*
|
|
|
* @param paramMap
|
|
|
* @return
|
|
|
*/
|
|
|
public SearchApiResult hotSaleRankProductList(Map<String, String> paramMap) {
|
|
|
try {
|
|
|
// 0)
|
|
|
this.addDefaultParam(paramMap);
|
|
|
|
|
|
// 1)构造搜索参数
|
|
|
SearchParam searchParam = this.buildProductListSearchParam(paramMap);
|
|
|
|
|
|
// 2)从缓存中获取数据
|
|
|
final String indexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
|
|
|
JSONObject cacheObject = searchCacheService.getJSONObjectFromCache(this.searchCache, indexName, searchParam);
|
|
|
if (cacheObject != null) {
|
|
|
SearchCacheMatchLogger.doSearchCacheMatchLog("/hotSaleRank/productList.json", paramMap);
|
|
|
return new SearchApiResult().setData(cacheObject);
|
|
|
}
|
|
|
|
|
|
// 3)查询ES
|
|
|
SearchResult searchResult = searchCommonService.doSearch(indexName, searchParam);
|
|
|
if (searchResult == null) {
|
|
|
return new SearchApiResult().setCode(500).setMessage("execption");
|
|
|
}
|
|
|
|
|
|
// 4)构造返回结果
|
|
|
JSONObject dataMap = new JSONObject();
|
|
|
dataMap.put("total", searchResult.getTotal());
|
|
|
dataMap.put("page", searchResult.getPage());
|
|
|
dataMap.put("page_size", searchParam.getSize());
|
|
|
dataMap.put("page_total", searchResult.getTotalPage());
|
|
|
dataMap.put("product_list", productIndexBaseService.getProductListWithPricePlan(searchResult.getResultList()));
|
|
|
|
|
|
// 5)将结果存进缓存
|
|
|
searchCacheService.addJSONObjectToCache(this.searchCache, indexName, searchParam, dataMap);
|
|
|
return new SearchApiResult().setData(dataMap);
|
|
|
} catch (Exception e) {
|
|
|
logger.error(e.getMessage(), e);
|
|
|
return new SearchApiResult().setData(null).setCode(500).setMessage("Exception");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private SearchParam buildProductListSearchParam(Map<String, String> paramMap) throws Exception {
|
|
|
// 1)验证查询条数
|
|
|
int pageSize = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum"));
|
|
|
int page = StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page"));
|
|
|
if (page < 1 || pageSize < 0) {
|
|
|
throw new IllegalArgumentException("分页参数不合法");
|
|
|
}
|
|
|
if (pageSize > 100) {
|
|
|
pageSize = 100;
|
|
|
}
|
|
|
// 2)构建基本查询参数
|
|
|
SearchParam searchParam = searchParamHelper.buildWithPersional(paramMap, false);
|
|
|
searchParam.setAggregationBuilders(null);
|
|
|
searchParam.setOffset((page - 1) * pageSize);
|
|
|
searchParam.setSize(pageSize);
|
|
|
// 3)设置排序字段
|
|
|
List<SortBuilder> sortBuilderS = new ArrayList<SortBuilder>();
|
|
|
sortBuilderS.add(SortBuilders.fieldSort(ProductIndexEsField.sevendayMoney).order(SortOrder.DESC));
|
|
|
sortBuilderS.add(SortBuilders.fieldSort(ProductIndexEsField.salesNum).order(SortOrder.DESC));
|
|
|
sortBuilderS.add(SortBuilders.fieldSort(ProductIndexEsField.id).order(SortOrder.DESC));
|
|
|
searchParam.setSortBuilders(sortBuilderS);
|
|
|
// 4)设置不返回的参数【节省带宽】
|
|
|
List<String> excludeFields = productIndexBaseService.getProductIndexExcludeFields();
|
|
|
searchParam.setExcludeFields(excludeFields);
|
|
|
return searchParam;
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|