|
|
package com.yoho.search.service.scene;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Collection;
|
|
|
import java.util.Collections;
|
|
|
import java.util.Comparator;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.HashSet;
|
|
|
import java.util.Iterator;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
import org.elasticsearch.search.SearchHit;
|
|
|
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
|
|
|
import org.elasticsearch.search.aggregations.Aggregation;
|
|
|
import org.elasticsearch.search.aggregations.AggregationBuilders;
|
|
|
import org.elasticsearch.search.aggregations.Aggregations;
|
|
|
import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation;
|
|
|
import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket;
|
|
|
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
|
|
|
import org.elasticsearch.search.aggregations.metrics.tophits.TopHits;
|
|
|
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.SearchCacheFactory;
|
|
|
import com.yoho.search.common.cache.model.SearchCache;
|
|
|
import com.yoho.search.common.cache.aop.SearchCacheAble;
|
|
|
import com.yoho.search.common.utils.SearchApiResultUtils;
|
|
|
import com.yoho.search.core.es.agg.IAggregation;
|
|
|
import com.yoho.search.core.es.model.SearchParam;
|
...
|
...
|
@@ -13,7 +37,6 @@ import com.yoho.search.core.es.model.SearchResult; |
|
|
import com.yoho.search.models.SearchApiResult;
|
|
|
import com.yoho.search.models.SizeInfoVO;
|
|
|
import com.yoho.search.models.SortWithSizesVO;
|
|
|
import com.yoho.search.service.base.SearchCacheService;
|
|
|
import com.yoho.search.service.base.SearchCommonService;
|
|
|
import com.yoho.search.service.base.SearchRequestParams;
|
|
|
import com.yoho.search.service.base.index.SizeIndexBaseService;
|
...
|
...
|
@@ -22,23 +45,6 @@ import com.yoho.search.service.helper.SearchParamHelper; |
|
|
import com.yoho.search.service.scene.common.AbstractSceneService;
|
|
|
import com.yoho.search.service.scene.common.CommonSceneProductListService;
|
|
|
import com.yoho.search.service.scene.common.SceneSelectionsService;
|
|
|
import org.elasticsearch.search.SearchHit;
|
|
|
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
|
|
|
import org.elasticsearch.search.aggregations.Aggregation;
|
|
|
import org.elasticsearch.search.aggregations.AggregationBuilders;
|
|
|
import org.elasticsearch.search.aggregations.Aggregations;
|
|
|
import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation;
|
|
|
import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket;
|
|
|
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
|
|
|
import org.elasticsearch.search.aggregations.metrics.tophits.TopHits;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
|
import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@Service
|
|
|
public class BreakSizeSceneService extends AbstractSceneService {
|
...
|
...
|
@@ -52,23 +58,12 @@ public class BreakSizeSceneService extends AbstractSceneService { |
|
|
@Autowired
|
|
|
private SearchCommonHelper searchCommonHelper;
|
|
|
@Autowired
|
|
|
private SearchCacheService searchCacheService;
|
|
|
@Autowired
|
|
|
private SearchCacheFactory searchCacheFactory;
|
|
|
@Autowired
|
|
|
private SearchParamHelper searchParamHelper;
|
|
|
@Autowired
|
|
|
private CommonSceneProductListService sceneProductListService;
|
|
|
@Autowired
|
|
|
private SceneSelectionsService sceneSelectionsService;
|
|
|
|
|
|
private SearchCache aggregationSearchCache;
|
|
|
|
|
|
@PostConstruct
|
|
|
void init() {
|
|
|
aggregationSearchCache = searchCacheFactory.getAggregationSearchCache();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 为断码区添加默认的过滤条件
|
|
|
*
|
...
|
...
|
@@ -105,7 +100,7 @@ public class BreakSizeSceneService extends AbstractSceneService { |
|
|
this.addParamsToParamMap(paramMap);
|
|
|
// 2、返回聚合结果
|
|
|
List<IAggregation> aggregation = sceneSelectionsService.getBreakSizeAggregations(paramMap);
|
|
|
return sceneSelectionsService.aggregations(paramMap, aggregation,true);
|
|
|
return sceneSelectionsService.aggregations(paramMap, aggregation, true);
|
|
|
} catch (Exception e) {
|
|
|
logger.error("[func=Couponaggregations][params=" + paramMap + "]", e);
|
|
|
return SearchApiResultUtils.errorSearchApiResult("Couponaggregations", paramMap, e);
|
...
|
...
|
@@ -118,14 +113,14 @@ public class BreakSizeSceneService extends AbstractSceneService { |
|
|
* @param paramMap
|
|
|
* @return
|
|
|
*/
|
|
|
@SearchCacheAble(cacheName = "SORT_SIZES", cacheInMinute = 10, excludeParams = { "uid", "page", "viewNum", "yh_channel" })
|
|
|
public SearchApiResult sortSizes(Map<String, String> paramMap) {
|
|
|
this.addParamsToParamMap(paramMap);
|
|
|
logger.info("[func=sortSizes][param={}][begin={}]", paramMap.toString(), System.currentTimeMillis());
|
|
|
try {
|
|
|
// 获取带filter和query的SearchParam
|
|
|
this.addParamsToParamMap(paramMap);
|
|
|
// 1、获取带filter和query的SearchParam
|
|
|
SearchParam searchParam = searchParamHelper.buildDefault(paramMap);
|
|
|
|
|
|
// 按品类聚合
|
|
|
// 2、按品类聚合
|
|
|
List<AbstractAggregationBuilder<?>> list = new ArrayList<AbstractAggregationBuilder<?>>();
|
|
|
TermsAggregationBuilder tremsBuilder = AggregationBuilders.terms("sortIdAgg").field(ProductIndexEsField.smallSortId).size(300);// 小分类200多一点
|
|
|
tremsBuilder.subAggregation(AggregationBuilders.topHits("productSkn").size(1));// 只取一个smallSortId对用的hits数据【其实只是为了取middleSortId,maxSortId】
|
...
|
...
|
@@ -133,30 +128,20 @@ public class BreakSizeSceneService extends AbstractSceneService { |
|
|
list.add(tremsBuilder);
|
|
|
searchParam.setAggregationBuilders(list);
|
|
|
|
|
|
// 从缓存中获取没有走es搜索
|
|
|
// 先从缓存中获取,如果能取到,则直接返回
|
|
|
SearchApiResult searchApiResult = new SearchApiResult();
|
|
|
JSONObject cacheObject = searchCacheService.getJSONObjectFromCache(aggregationSearchCache, ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
|
|
|
if (cacheObject != null) {
|
|
|
searchApiResult.setData(cacheObject);
|
|
|
return searchApiResult;
|
|
|
}
|
|
|
|
|
|
// 获取聚合出来的尺码结果
|
|
|
// 3、获取聚合出来的尺码结果
|
|
|
SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
|
|
|
Map<String, Aggregation> aggMaps = searchResult.getAggMaps();
|
|
|
if (!aggMaps.containsKey("sortIdAgg")) {
|
|
|
return searchApiResult;
|
|
|
return new SearchApiResult().setData(new JSONObject());
|
|
|
}
|
|
|
// 构造list的返回结果
|
|
|
// 4、构造list
|
|
|
List<SortWithSizesVO> sortWithSizeList = this.getSortSizesResult(aggMaps);
|
|
|
|
|
|
// 构造返回结果并加入缓存
|
|
|
// 5、构造返回结果
|
|
|
JSONObject data = new JSONObject();
|
|
|
data.put("total", sortWithSizeList.size());
|
|
|
data.put("list", sortWithSizeList);
|
|
|
searchCacheService.addJSONObjectToCache(aggregationSearchCache, ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam, data);
|
|
|
return searchApiResult.setData(data);
|
|
|
return new SearchApiResult().setData(data);
|
|
|
} catch (Exception e) {
|
|
|
logger.error(e.getMessage(), e);
|
|
|
return SearchApiResultUtils.errorSearchApiResult("sortSizes", paramMap, e);
|
...
|
...
|
|