|
|
package com.yoho.search.service.service;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.alibaba.fastjson.TypeReference;
|
|
|
import com.yoho.error.event.SearchEvent;
|
|
|
import com.yoho.search.base.utils.EventReportEnum;
|
|
|
import com.yoho.search.base.utils.ISearchConstants;
|
|
|
import com.yoho.search.cache.model.SearchCache;
|
|
|
import com.yoho.search.core.es.agg.IAggregation;
|
|
|
import com.yoho.search.core.es.model.SearchParam;
|
|
|
import com.yoho.search.core.es.model.SearchResult;
|
|
|
import com.yoho.search.core.es.utils.IgnoreSomeException;
|
|
|
import com.yoho.search.service.scene.shopproduct.ShopProductCacheBean;
|
|
|
import com.yoho.search.recall.models.common.ParamQueryFilter;
|
|
|
import com.yoho.search.service.scene.shopproduct.ShopProductRequest;
|
|
|
import com.yoho.search.service.scene.shopproduct.ShopProductResponse;
|
|
|
import com.yoho.search.service.aggregations.impls.AggregationFactoryService;
|
|
|
import com.yoho.search.service.base.SearchCommonService;
|
|
|
import com.yoho.search.service.base.SearchRequestParams;
|
|
|
import com.yoho.search.service.helper.SearchCommonHelper;
|
|
|
import com.yoho.search.service.helper.SearchParamHelper;
|
|
|
import com.yoho.search.cache.beans.AbstractCacheAbleService;
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
|
import org.elasticsearch.index.query.BoolQueryBuilder;
|
|
|
import org.elasticsearch.search.aggregations.Aggregation;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.context.ApplicationEventPublisher;
|
|
|
import org.springframework.context.ApplicationEventPublisherAware;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@Service
|
|
|
public class AggregationService extends AbstractCacheAbleService implements ApplicationEventPublisherAware {
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(AggregationService.class);
|
|
|
|
|
|
@Autowired
|
|
|
private AggregationFactoryService aggregationFactoryService;
|
|
|
@Autowired
|
|
|
private SearchCommonService searchCommonService;
|
|
|
@Autowired
|
|
|
private SearchParamHelper searchParamHelper;
|
|
|
@Autowired
|
|
|
private ShopProductCacheBean shopSknCacheBean;
|
|
|
@Autowired
|
|
|
private SearchCommonHelper searchCommonHelper;
|
|
|
|
|
|
@Override
|
|
|
public SearchCache getSearchCache() {
|
|
|
return searchCacheFactory.getAggregationSearchCache();
|
|
|
}
|
|
|
|
|
|
private ApplicationEventPublisher publisher;
|
|
|
|
|
|
private static final String AggregationServiceIndex = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
|
|
|
|
|
|
@Override
|
|
|
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
|
|
|
this.publisher = applicationEventPublisher;
|
|
|
}
|
|
|
|
|
|
private SearchResult doSearchForAggregation(SearchParam searchParam) {
|
|
|
SearchParam searchParamClone = searchParam.clone();
|
|
|
return searchCommonService.doSearch(AggregationServiceIndex, searchParamClone);
|
|
|
}
|
|
|
|
|
|
public JSONObject getAggNameAndResponse(IAggregation aggregation, SearchParam searchParam) throws Exception {
|
|
|
SearchResult searchResult = this.doSearchForAggregation(searchParam);
|
|
|
Map<String, Aggregation> aggMaps = searchResult.getAggMaps();
|
|
|
JSONObject result = aggregation.getAggNameAndResponse(aggMaps);
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
public JSONObject getAggNameAndResponseWithTotal(IAggregation aggregation, SearchParam searchParam) throws Exception {
|
|
|
SearchResult searchResult = this.doSearchForAggregation(searchParam);
|
|
|
Map<String, Aggregation> aggMaps = searchResult.getAggMaps();
|
|
|
JSONObject result = aggregation.getAggNameAndResponse(aggMaps);
|
|
|
result.put("total", searchResult.getTotal());
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
private JSONObject getAggNameAndResponseWithCache(IAggregation aggregation, SearchParam searchParam) {
|
|
|
try {
|
|
|
JSONObject cacheObject = searchCacheService.getJSONObjectFromCache(this.searchCache, AggregationServiceIndex, searchParam);
|
|
|
if (cacheObject != null) {
|
|
|
cacheObject = aggregation.doSomethingAfterSerialized(cacheObject);
|
|
|
return cacheObject;
|
|
|
}
|
|
|
JSONObject result = this.getAggNameAndResponse(aggregation, searchParam);
|
|
|
if (result != null) {
|
|
|
searchCacheService.addJSONObjectToCache(this.searchCache, AggregationServiceIndex, searchParam, result);
|
|
|
}
|
|
|
return result;
|
|
|
} catch (Exception e) {
|
|
|
logger.error(e.getMessage());
|
|
|
publisher.publishEvent(new SearchEvent(EventReportEnum.AGGREGATIONSERVICE_GETAGGNAMEANDRESPONSE.getEventName(),
|
|
|
EventReportEnum.AGGREGATIONSERVICE_GETAGGNAMEANDRESPONSE.getFunctionName(), EventReportEnum.AGGREGATIONSERVICE_GETAGGNAMEANDRESPONSE.getMoudleName(),
|
|
|
"exception", IgnoreSomeException.filterSomeException(e), null));
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private SearchParam genSearchParamForAgg(IAggregation aggregation, Map<String, String> paramMap, String filterParamName, int cacheSize) throws Exception {
|
|
|
SearchParam searchParam = searchParamHelper.buildWithFilterParam(paramMap, filterParamName);
|
|
|
searchParam.setAggregationBuilders(Arrays.asList(aggregation.getBuilder()));
|
|
|
searchParam.setOffset(cacheSize);// 只是用来做缓存的
|
|
|
return searchParam;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取性别层面的聚合结果
|
|
|
*/
|
|
|
public JSONObject getGenderNewAggregationResult(Map<String, String> paramMap, boolean needPreAggregation) throws Exception {
|
|
|
IAggregation aggregation = aggregationFactoryService.getGenderNewAggregation();
|
|
|
String filterParamName = needPreAggregation ? SearchRequestParams.PARAM_SEARCH_GENDER : null;
|
|
|
SearchParam searchParam = this.genSearchParamForAgg(aggregation, paramMap, filterParamName, 0);
|
|
|
return this.getAggNameAndResponseWithCache(aggregation, searchParam);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取年龄层的聚合结果
|
|
|
*/
|
|
|
public JSONObject getAgeLevelAggregationResult(Map<String, String> paramMap, boolean needPreAggregation) throws Exception {
|
|
|
IAggregation aggregation = aggregationFactoryService.getAgeLevelAggregation();
|
|
|
String filterParamName = needPreAggregation ? SearchRequestParams.PARAM_SEARCH_AGELEVEL : null;
|
|
|
SearchParam searchParam = this.genSearchParamForAgg(aggregation, paramMap, filterParamName, 0);
|
|
|
return this.getAggNameAndResponseWithCache(aggregation, searchParam);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取价格层面的聚合结果[结果经过缓存之后会变成无序的,此时需要手动转一下]
|
|
|
*/
|
|
|
public JSONObject getPriceAggregationResult(Map<String, String> paramMap, boolean needPreAggregation) throws Exception {
|
|
|
IAggregation priceAggregation = aggregationFactoryService.getPriceAggregation();
|
|
|
String filterParamName = needPreAggregation ? SearchRequestParams.PARAM_SEARCH_PRICE : null;
|
|
|
SearchParam searchParam = this.genSearchParamForAgg(priceAggregation, paramMap, filterParamName, 0);
|
|
|
return this.getAggNameAndResponseWithCache(priceAggregation, searchParam);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取颜色层面的聚合结果
|
|
|
*/
|
|
|
public JSONObject getColorAggregationResult(Map<String, String> paramMap, boolean needPreAggregation) throws Exception {
|
|
|
IAggregation colorAggregation = aggregationFactoryService.getColorAggregation(paramMap);
|
|
|
String filterParamName = needPreAggregation ? SearchRequestParams.PARAM_SEARCH_COLOR : null;
|
|
|
SearchParam searchParam = this.genSearchParamForAgg(colorAggregation, paramMap, filterParamName, 0);
|
|
|
return this.getAggNameAndResponseWithCache(colorAggregation, searchParam);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取风格层面的聚合结果
|
|
|
*/
|
|
|
public JSONObject getStyleAggregationResult(Map<String, String> paramMap, boolean needPreAggregation) throws Exception {
|
|
|
IAggregation styleAggregation = aggregationFactoryService.getStyleAggregation(paramMap);
|
|
|
String filterParamName = needPreAggregation ? SearchRequestParams.PARAM_SEARCH_STYLE : null;
|
|
|
SearchParam searchParam = this.genSearchParamForAgg(styleAggregation, paramMap, filterParamName, 0);
|
|
|
return this.getAggNameAndResponseWithCache(styleAggregation, searchParam);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取尺码层面的聚合结果
|
|
|
*/
|
|
|
public JSONObject getSizeAggregationResult(Map<String, String> paramMap, boolean needPreAggregation) throws Exception {
|
|
|
IAggregation sizeAggregation = aggregationFactoryService.getSizeAggregation();
|
|
|
String filterParamName = needPreAggregation ? SearchRequestParams.PARAM_SEARCH_SIZE : null;
|
|
|
SearchParam searchParam = this.genSearchParamForAgg(sizeAggregation, paramMap, filterParamName, 0);
|
|
|
return this.getAggNameAndResponseWithCache(sizeAggregation, searchParam);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取是否新品的聚合结果
|
|
|
*/
|
|
|
public JSONObject getIsNewAggregationResult(Map<String, String> paramMap, boolean needPreAggregation) throws Exception {
|
|
|
IAggregation isNewAggregation = aggregationFactoryService.getIsNewAggregation();
|
|
|
String filterParamName = needPreAggregation ? SearchRequestParams.PARAM_SEARCH_ISNEW : null;
|
|
|
SearchParam searchParam = this.genSearchParamForAgg(isNewAggregation, paramMap, filterParamName, 0);
|
|
|
return this.getAggNameAndResponseWithCache(isNewAggregation, searchParam);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取是否限量的聚合结果
|
|
|
*/
|
|
|
public JSONObject getIsLimitAggregationResult(Map<String, String> paramMap, boolean needPreAggregation) throws Exception {
|
|
|
IAggregation isLimitedAggregation = aggregationFactoryService.getIsLimitedAggregation();
|
|
|
String filterParamName = needPreAggregation ? SearchRequestParams.PARAM_SEARCH_ISLIMITED : null;
|
|
|
SearchParam searchParam = this.genSearchParamForAgg(isLimitedAggregation, paramMap, filterParamName, 0);
|
|
|
return this.getAggNameAndResponseWithCache(isLimitedAggregation, searchParam);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取是否特供的聚合结果
|
|
|
*/
|
|
|
public JSONObject getIsSecialofferAggregationResult(Map<String, String> paramMap, boolean needPreAggregation) throws Exception {
|
|
|
IAggregation isSecialofferAggregation = aggregationFactoryService.getIsSecialofferAggregation();
|
|
|
String filterParamName = needPreAggregation ? SearchRequestParams.PARAM_SEARCH_SPECIALOFFER : null;
|
|
|
SearchParam searchParam = this.genSearchParamForAgg(isSecialofferAggregation, paramMap, filterParamName, 0);
|
|
|
return this.getAggNameAndResponseWithCache(isSecialofferAggregation, searchParam);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取商品关键词的聚合,来源与风格、款式和属性。
|
|
|
*/
|
|
|
public JSONObject getKeywordAggregationResult(Map<String, String> paramMap, int aggCount) throws Exception {
|
|
|
IAggregation keywordAggregation = aggregationFactoryService.getKeywordAggregation(paramMap, aggCount);
|
|
|
String filterParamName = null;
|
|
|
SearchParam searchParam = this.genSearchParamForAgg(keywordAggregation, paramMap, filterParamName, 0);
|
|
|
return this.getAggNameAndResponseWithCache(keywordAggregation, searchParam);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取店铺聚合结果。
|
|
|
*/
|
|
|
public JSONObject getShopAggregationResult(Map<String, String> paramMap, int aggCount) throws Exception {
|
|
|
IAggregation shopAggregation = aggregationFactoryService.getShopAggregation(aggCount);
|
|
|
String filterParamName = null;
|
|
|
SearchParam searchParam = this.genSearchParamForAgg(shopAggregation, paramMap, filterParamName, 0);
|
|
|
return this.getAggNameAndResponseWithCache(shopAggregation, searchParam);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取专区名称面的聚合结果
|
|
|
*/
|
|
|
public JSONObject getPromotionAggregationResult(Map<String, String> paramMap, int aggCount) throws Exception {
|
|
|
IAggregation zqNameAggregation = aggregationFactoryService.getZqNameAggregation(aggCount);
|
|
|
String filterParamName = null;
|
|
|
Integer paramNum = Integer.valueOf(paramMap.getOrDefault(SearchRequestParams.PARAM_SEARCH_PARAMETPERNUM, "5"));
|
|
|
SearchParam searchParam = this.genSearchParamForAgg(zqNameAggregation, paramMap, filterParamName, paramNum);
|
|
|
return this.getAggNameAndResponseWithCache(zqNameAggregation, searchParam);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取规则层面的聚合结果
|
|
|
*/
|
|
|
public JSONObject getStandardAggregationResult(Map<String, String> paramMap) throws Exception {
|
|
|
IAggregation standardAggregation = aggregationFactoryService.getStandardAggregation(paramMap);
|
|
|
String filterParamName = null;
|
|
|
Integer paramNum = Integer.valueOf(paramMap.getOrDefault(SearchRequestParams.PARAM_SEARCH_PARAMETPERNUM, "5"));
|
|
|
SearchParam searchParam = this.genSearchParamForAgg(standardAggregation, paramMap, filterParamName, paramNum);
|
|
|
return this.getAggNameAndResponseWithCache(standardAggregation, searchParam);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取店铺聚合结果。
|
|
|
*/
|
|
|
public JSONObject getShopAndSknAggregationResult(Map<String, String> paramMap, int page, int pageSize, int aggCount) throws Exception {
|
|
|
IAggregation shopAndSknAggregation = aggregationFactoryService.getShopAndSknAggregation(aggCount);
|
|
|
SearchParam searchParam = this.genSearchParamForAgg(shopAndSknAggregation, paramMap, null, 0);
|
|
|
JSONObject aggResult = getAggNameAndResponseWithCache(shopAndSknAggregation, searchParam);
|
|
|
JSONObject dataMap = new JSONObject();
|
|
|
if (aggResult != null) {
|
|
|
List<Integer> shopIds = JSON.parseObject(aggResult.getJSONArray("shopAndSknAgg").toJSONString(), new TypeReference<List<Integer>>() {});
|
|
|
if (CollectionUtils.isNotEmpty(shopIds)) {
|
|
|
dataMap.put("page", page);
|
|
|
dataMap.put("page_size", pageSize);
|
|
|
dataMap.put("total", shopIds.size());
|
|
|
dataMap.put("page_total", searchCommonHelper.getTotalPage(shopIds.size(), pageSize));
|
|
|
dataMap.put("shop_product_list", Collections.emptyList());
|
|
|
List<Integer> subShopIds = subList(shopIds, pageSize, page);
|
|
|
if (CollectionUtils.isNotEmpty(subShopIds)) {
|
|
|
List<ShopProductRequest> shopProductRequests = subShopIds.stream().map(shopId -> {
|
|
|
return new ShopProductRequest(new ParamQueryFilter(searchParam.getQuery(), (BoolQueryBuilder)searchParam.getFiter()), shopId);
|
|
|
}).collect(Collectors.toList());
|
|
|
String hrShopIds = paramMap.get(SearchRequestParams.PARAM_SEARCH_HR_SHOP);
|
|
|
List<ShopProductResponse> responseList = shopSknCacheBean.getShopSknByShopId(shopProductRequests, hrShopIds);
|
|
|
dataMap.put("shop_product_list", responseList);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return dataMap;
|
|
|
}
|
|
|
|
|
|
private List<Integer> subList(List<Integer> sourceList, int pageSize, int page) {
|
|
|
int fromIndex = pageSize * (page - 1);
|
|
|
if (fromIndex >= sourceList.size()) {
|
|
|
return null;
|
|
|
}
|
|
|
int toIndex = pageSize * page;
|
|
|
if (toIndex > sourceList.size()) {
|
|
|
toIndex = sourceList.size();
|
|
|
}
|
|
|
return sourceList.subList(fromIndex, toIndex);
|
|
|
}
|
|
|
|
|
|
} |
|
|
package com.yoho.search.service.aggregations;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.alibaba.fastjson.TypeReference;
|
|
|
import com.yoho.error.event.SearchEvent;
|
|
|
import com.yoho.search.base.utils.EventReportEnum;
|
|
|
import com.yoho.search.base.utils.ISearchConstants;
|
|
|
import com.yoho.search.cache.model.SearchCache;
|
|
|
import com.yoho.search.core.es.agg.IAggregation;
|
|
|
import com.yoho.search.core.es.model.SearchParam;
|
|
|
import com.yoho.search.core.es.model.SearchResult;
|
|
|
import com.yoho.search.core.es.utils.IgnoreSomeException;
|
|
|
import com.yoho.search.service.scene.shopproduct.ShopProductCacheBean;
|
|
|
import com.yoho.search.recall.models.common.ParamQueryFilter;
|
|
|
import com.yoho.search.service.scene.shopproduct.ShopProductRequest;
|
|
|
import com.yoho.search.service.scene.shopproduct.ShopProductResponse;
|
|
|
import com.yoho.search.service.aggregations.impls.AggregationFactoryService;
|
|
|
import com.yoho.search.service.base.SearchCommonService;
|
|
|
import com.yoho.search.service.base.SearchRequestParams;
|
|
|
import com.yoho.search.service.helper.SearchCommonHelper;
|
|
|
import com.yoho.search.service.helper.SearchParamHelper;
|
|
|
import com.yoho.search.cache.beans.AbstractCacheAbleService;
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
|
import org.elasticsearch.index.query.BoolQueryBuilder;
|
|
|
import org.elasticsearch.search.aggregations.Aggregation;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.context.ApplicationEventPublisher;
|
|
|
import org.springframework.context.ApplicationEventPublisherAware;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@Service
|
|
|
public class AggregationService extends AbstractCacheAbleService implements ApplicationEventPublisherAware {
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(AggregationService.class);
|
|
|
|
|
|
@Autowired
|
|
|
private AggregationFactoryService aggregationFactoryService;
|
|
|
@Autowired
|
|
|
private SearchCommonService searchCommonService;
|
|
|
@Autowired
|
|
|
private SearchParamHelper searchParamHelper;
|
|
|
@Autowired
|
|
|
private ShopProductCacheBean shopSknCacheBean;
|
|
|
@Autowired
|
|
|
private SearchCommonHelper searchCommonHelper;
|
|
|
|
|
|
@Override
|
|
|
public SearchCache getSearchCache() {
|
|
|
return searchCacheFactory.getAggregationSearchCache();
|
|
|
}
|
|
|
|
|
|
private ApplicationEventPublisher publisher;
|
|
|
|
|
|
private static final String AggregationServiceIndex = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
|
|
|
|
|
|
@Override
|
|
|
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
|
|
|
this.publisher = applicationEventPublisher;
|
|
|
}
|
|
|
|
|
|
private SearchResult doSearchForAggregation(SearchParam searchParam) {
|
|
|
SearchParam searchParamClone = searchParam.clone();
|
|
|
return searchCommonService.doSearch(AggregationServiceIndex, searchParamClone);
|
|
|
}
|
|
|
|
|
|
public JSONObject getAggNameAndResponse(IAggregation aggregation, SearchParam searchParam) throws Exception {
|
|
|
SearchResult searchResult = this.doSearchForAggregation(searchParam);
|
|
|
Map<String, Aggregation> aggMaps = searchResult.getAggMaps();
|
|
|
JSONObject result = aggregation.getAggNameAndResponse(aggMaps);
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
public JSONObject getAggNameAndResponseWithTotal(IAggregation aggregation, SearchParam searchParam) throws Exception {
|
|
|
SearchResult searchResult = this.doSearchForAggregation(searchParam);
|
|
|
Map<String, Aggregation> aggMaps = searchResult.getAggMaps();
|
|
|
JSONObject result = aggregation.getAggNameAndResponse(aggMaps);
|
|
|
result.put("total", searchResult.getTotal());
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
private JSONObject getAggNameAndResponseWithCache(IAggregation aggregation, SearchParam searchParam) {
|
|
|
try {
|
|
|
JSONObject cacheObject = searchCacheService.getJSONObjectFromCache(this.searchCache, AggregationServiceIndex, searchParam);
|
|
|
if (cacheObject != null) {
|
|
|
cacheObject = aggregation.doSomethingAfterSerialized(cacheObject);
|
|
|
return cacheObject;
|
|
|
}
|
|
|
JSONObject result = this.getAggNameAndResponse(aggregation, searchParam);
|
|
|
if (result != null) {
|
|
|
searchCacheService.addJSONObjectToCache(this.searchCache, AggregationServiceIndex, searchParam, result);
|
|
|
}
|
|
|
return result;
|
|
|
} catch (Exception e) {
|
|
|
logger.error(e.getMessage());
|
|
|
publisher.publishEvent(new SearchEvent(EventReportEnum.AGGREGATIONSERVICE_GETAGGNAMEANDRESPONSE.getEventName(),
|
|
|
EventReportEnum.AGGREGATIONSERVICE_GETAGGNAMEANDRESPONSE.getFunctionName(), EventReportEnum.AGGREGATIONSERVICE_GETAGGNAMEANDRESPONSE.getMoudleName(),
|
|
|
"exception", IgnoreSomeException.filterSomeException(e), null));
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private SearchParam genSearchParamForAgg(IAggregation aggregation, Map<String, String> paramMap, String filterParamName, int cacheSize) throws Exception {
|
|
|
SearchParam searchParam = searchParamHelper.buildWithFilterParam(paramMap, filterParamName);
|
|
|
searchParam.setAggregationBuilders(Arrays.asList(aggregation.getBuilder()));
|
|
|
searchParam.setOffset(cacheSize);// 只是用来做缓存的
|
|
|
return searchParam;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取性别层面的聚合结果
|
|
|
*/
|
|
|
public JSONObject getGenderNewAggregationResult(Map<String, String> paramMap, boolean needPreAggregation) throws Exception {
|
|
|
IAggregation aggregation = aggregationFactoryService.getGenderNewAggregation();
|
|
|
String filterParamName = needPreAggregation ? SearchRequestParams.PARAM_SEARCH_GENDER : null;
|
|
|
SearchParam searchParam = this.genSearchParamForAgg(aggregation, paramMap, filterParamName, 0);
|
|
|
return this.getAggNameAndResponseWithCache(aggregation, searchParam);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取年龄层的聚合结果
|
|
|
*/
|
|
|
public JSONObject getAgeLevelAggregationResult(Map<String, String> paramMap, boolean needPreAggregation) throws Exception {
|
|
|
IAggregation aggregation = aggregationFactoryService.getAgeLevelAggregation();
|
|
|
String filterParamName = needPreAggregation ? SearchRequestParams.PARAM_SEARCH_AGELEVEL : null;
|
|
|
SearchParam searchParam = this.genSearchParamForAgg(aggregation, paramMap, filterParamName, 0);
|
|
|
return this.getAggNameAndResponseWithCache(aggregation, searchParam);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取价格层面的聚合结果[结果经过缓存之后会变成无序的,此时需要手动转一下]
|
|
|
*/
|
|
|
public JSONObject getPriceAggregationResult(Map<String, String> paramMap, boolean needPreAggregation) throws Exception {
|
|
|
IAggregation priceAggregation = aggregationFactoryService.getPriceAggregation();
|
|
|
String filterParamName = needPreAggregation ? SearchRequestParams.PARAM_SEARCH_PRICE : null;
|
|
|
SearchParam searchParam = this.genSearchParamForAgg(priceAggregation, paramMap, filterParamName, 0);
|
|
|
return this.getAggNameAndResponseWithCache(priceAggregation, searchParam);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取颜色层面的聚合结果
|
|
|
*/
|
|
|
public JSONObject getColorAggregationResult(Map<String, String> paramMap, boolean needPreAggregation) throws Exception {
|
|
|
IAggregation colorAggregation = aggregationFactoryService.getColorAggregation(paramMap);
|
|
|
String filterParamName = needPreAggregation ? SearchRequestParams.PARAM_SEARCH_COLOR : null;
|
|
|
SearchParam searchParam = this.genSearchParamForAgg(colorAggregation, paramMap, filterParamName, 0);
|
|
|
return this.getAggNameAndResponseWithCache(colorAggregation, searchParam);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取风格层面的聚合结果
|
|
|
*/
|
|
|
public JSONObject getStyleAggregationResult(Map<String, String> paramMap, boolean needPreAggregation) throws Exception {
|
|
|
IAggregation styleAggregation = aggregationFactoryService.getStyleAggregation(paramMap);
|
|
|
String filterParamName = needPreAggregation ? SearchRequestParams.PARAM_SEARCH_STYLE : null;
|
|
|
SearchParam searchParam = this.genSearchParamForAgg(styleAggregation, paramMap, filterParamName, 0);
|
|
|
return this.getAggNameAndResponseWithCache(styleAggregation, searchParam);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取尺码层面的聚合结果
|
|
|
*/
|
|
|
public JSONObject getSizeAggregationResult(Map<String, String> paramMap, boolean needPreAggregation) throws Exception {
|
|
|
IAggregation sizeAggregation = aggregationFactoryService.getSizeAggregation();
|
|
|
String filterParamName = needPreAggregation ? SearchRequestParams.PARAM_SEARCH_SIZE : null;
|
|
|
SearchParam searchParam = this.genSearchParamForAgg(sizeAggregation, paramMap, filterParamName, 0);
|
|
|
return this.getAggNameAndResponseWithCache(sizeAggregation, searchParam);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取是否新品的聚合结果
|
|
|
*/
|
|
|
public JSONObject getIsNewAggregationResult(Map<String, String> paramMap, boolean needPreAggregation) throws Exception {
|
|
|
IAggregation isNewAggregation = aggregationFactoryService.getIsNewAggregation();
|
|
|
String filterParamName = needPreAggregation ? SearchRequestParams.PARAM_SEARCH_ISNEW : null;
|
|
|
SearchParam searchParam = this.genSearchParamForAgg(isNewAggregation, paramMap, filterParamName, 0);
|
|
|
return this.getAggNameAndResponseWithCache(isNewAggregation, searchParam);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取是否限量的聚合结果
|
|
|
*/
|
|
|
public JSONObject getIsLimitAggregationResult(Map<String, String> paramMap, boolean needPreAggregation) throws Exception {
|
|
|
IAggregation isLimitedAggregation = aggregationFactoryService.getIsLimitedAggregation();
|
|
|
String filterParamName = needPreAggregation ? SearchRequestParams.PARAM_SEARCH_ISLIMITED : null;
|
|
|
SearchParam searchParam = this.genSearchParamForAgg(isLimitedAggregation, paramMap, filterParamName, 0);
|
|
|
return this.getAggNameAndResponseWithCache(isLimitedAggregation, searchParam);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取是否特供的聚合结果
|
|
|
*/
|
|
|
public JSONObject getIsSecialofferAggregationResult(Map<String, String> paramMap, boolean needPreAggregation) throws Exception {
|
|
|
IAggregation isSecialofferAggregation = aggregationFactoryService.getIsSecialofferAggregation();
|
|
|
String filterParamName = needPreAggregation ? SearchRequestParams.PARAM_SEARCH_SPECIALOFFER : null;
|
|
|
SearchParam searchParam = this.genSearchParamForAgg(isSecialofferAggregation, paramMap, filterParamName, 0);
|
|
|
return this.getAggNameAndResponseWithCache(isSecialofferAggregation, searchParam);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取商品关键词的聚合,来源与风格、款式和属性。
|
|
|
*/
|
|
|
public JSONObject getKeywordAggregationResult(Map<String, String> paramMap, int aggCount) throws Exception {
|
|
|
IAggregation keywordAggregation = aggregationFactoryService.getKeywordAggregation(paramMap, aggCount);
|
|
|
String filterParamName = null;
|
|
|
SearchParam searchParam = this.genSearchParamForAgg(keywordAggregation, paramMap, filterParamName, 0);
|
|
|
return this.getAggNameAndResponseWithCache(keywordAggregation, searchParam);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取店铺聚合结果。
|
|
|
*/
|
|
|
public JSONObject getShopAggregationResult(Map<String, String> paramMap, int aggCount) throws Exception {
|
|
|
IAggregation shopAggregation = aggregationFactoryService.getShopAggregation(aggCount);
|
|
|
String filterParamName = null;
|
|
|
SearchParam searchParam = this.genSearchParamForAgg(shopAggregation, paramMap, filterParamName, 0);
|
|
|
return this.getAggNameAndResponseWithCache(shopAggregation, searchParam);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取专区名称面的聚合结果
|
|
|
*/
|
|
|
public JSONObject getPromotionAggregationResult(Map<String, String> paramMap, int aggCount) throws Exception {
|
|
|
IAggregation zqNameAggregation = aggregationFactoryService.getZqNameAggregation(aggCount);
|
|
|
String filterParamName = null;
|
|
|
Integer paramNum = Integer.valueOf(paramMap.getOrDefault(SearchRequestParams.PARAM_SEARCH_PARAMETPERNUM, "5"));
|
|
|
SearchParam searchParam = this.genSearchParamForAgg(zqNameAggregation, paramMap, filterParamName, paramNum);
|
|
|
return this.getAggNameAndResponseWithCache(zqNameAggregation, searchParam);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取规则层面的聚合结果
|
|
|
*/
|
|
|
public JSONObject getStandardAggregationResult(Map<String, String> paramMap) throws Exception {
|
|
|
IAggregation standardAggregation = aggregationFactoryService.getStandardAggregation(paramMap);
|
|
|
String filterParamName = null;
|
|
|
Integer paramNum = Integer.valueOf(paramMap.getOrDefault(SearchRequestParams.PARAM_SEARCH_PARAMETPERNUM, "5"));
|
|
|
SearchParam searchParam = this.genSearchParamForAgg(standardAggregation, paramMap, filterParamName, paramNum);
|
|
|
return this.getAggNameAndResponseWithCache(standardAggregation, searchParam);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取店铺聚合结果。
|
|
|
*/
|
|
|
public JSONObject getShopAndSknAggregationResult(Map<String, String> paramMap, int page, int pageSize, int aggCount) throws Exception {
|
|
|
IAggregation shopAndSknAggregation = aggregationFactoryService.getShopAndSknAggregation(aggCount);
|
|
|
SearchParam searchParam = this.genSearchParamForAgg(shopAndSknAggregation, paramMap, null, 0);
|
|
|
JSONObject aggResult = getAggNameAndResponseWithCache(shopAndSknAggregation, searchParam);
|
|
|
JSONObject dataMap = new JSONObject();
|
|
|
if (aggResult != null) {
|
|
|
List<Integer> shopIds = JSON.parseObject(aggResult.getJSONArray("shopAndSknAgg").toJSONString(), new TypeReference<List<Integer>>() {});
|
|
|
if (CollectionUtils.isNotEmpty(shopIds)) {
|
|
|
dataMap.put("page", page);
|
|
|
dataMap.put("page_size", pageSize);
|
|
|
dataMap.put("total", shopIds.size());
|
|
|
dataMap.put("page_total", searchCommonHelper.getTotalPage(shopIds.size(), pageSize));
|
|
|
dataMap.put("shop_product_list", Collections.emptyList());
|
|
|
List<Integer> subShopIds = subList(shopIds, pageSize, page);
|
|
|
if (CollectionUtils.isNotEmpty(subShopIds)) {
|
|
|
List<ShopProductRequest> shopProductRequests = subShopIds.stream().map(shopId -> {
|
|
|
return new ShopProductRequest(new ParamQueryFilter(searchParam.getQuery(), (BoolQueryBuilder)searchParam.getFiter()), shopId);
|
|
|
}).collect(Collectors.toList());
|
|
|
String hrShopIds = paramMap.get(SearchRequestParams.PARAM_SEARCH_HR_SHOP);
|
|
|
List<ShopProductResponse> responseList = shopSknCacheBean.getShopSknByShopId(shopProductRequests, hrShopIds);
|
|
|
dataMap.put("shop_product_list", responseList);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return dataMap;
|
|
|
}
|
|
|
|
|
|
private List<Integer> subList(List<Integer> sourceList, int pageSize, int page) {
|
|
|
int fromIndex = pageSize * (page - 1);
|
|
|
if (fromIndex >= sourceList.size()) {
|
|
|
return null;
|
|
|
}
|
|
|
int toIndex = pageSize * page;
|
|
|
if (toIndex > sourceList.size()) {
|
|
|
toIndex = sourceList.size();
|
|
|
}
|
|
|
return sourceList.subList(fromIndex, toIndex);
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|