|
|
package com.yoho.search.service.scene.common;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.yoho.search.base.utils.ISearchConstants;
|
|
|
import com.yoho.search.cache.log.SearchCacheMatchLogger;
|
|
|
import com.yoho.search.cache.model.SearchCache;
|
|
|
import com.yoho.search.cache.beans.AbstractCacheAbleService;
|
|
|
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.models.SearchApiResult;
|
|
|
import com.yoho.search.service.scene.aggregations.impls.AggregationFactory;
|
|
|
import com.yoho.search.common.SearchCommonService;
|
|
|
import com.yoho.search.service.helper.SearchParamHelper;
|
|
|
import org.elasticsearch.index.query.BoolQueryBuilder;
|
|
|
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
|
|
|
import org.elasticsearch.search.aggregations.Aggregation;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
@Service
|
|
|
public class SceneSelectionsService extends AbstractCacheAbleService {
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(SceneSelectionsService.class);
|
|
|
|
|
|
@Autowired
|
|
|
private SearchParamHelper searchParamHelper;
|
|
|
@Autowired
|
|
|
private SearchCommonService searchCommonService;
|
|
|
@Autowired
|
|
|
private AggregationFactory aggregationFactory;
|
|
|
|
|
|
@Override
|
|
|
public SearchCache getSearchCache() {
|
|
|
return searchCacheFactory.getSceneAggregationsCache();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @新版筛选器的聚合选项:
|
|
|
* @1、全球购
|
|
|
* @2、促销
|
|
|
* @3、全部的品牌 +喜欢的品牌【个性化的】
|
|
|
* @4、人群
|
|
|
* @5、品类
|
|
|
* @6、性别
|
|
|
* @7、价格
|
|
|
* @8、折扣
|
|
|
* @9、颜色
|
|
|
* @10、尺码
|
|
|
* @11、风格
|
|
|
*/
|
|
|
|
|
|
public List<IAggregation> getCommonAggregations(Map<String, String> paramMap) {
|
|
|
List<IAggregation> aggregations = new ArrayList<>();
|
|
|
// 1)专区名称
|
|
|
aggregations.add(aggregationFactory.getZqNameAggregation(1000));
|
|
|
// 2)全球购
|
|
|
aggregations.add(aggregationFactory.getIsGlobalAggregation());
|
|
|
// 3)品牌
|
|
|
aggregations.add(aggregationFactory.getBrandAggregation(paramMap));
|
|
|
// 4)人群
|
|
|
aggregations.add(aggregationFactory.getAgeLevelAggregation());
|
|
|
// 5)品类
|
|
|
aggregations.add(aggregationFactory.getSortGroupAggregation(paramMap));
|
|
|
// 6)性别
|
|
|
aggregations.add(aggregationFactory.getGenderNewAggregation());
|
|
|
// 7)价格
|
|
|
aggregations.add(aggregationFactory.getPriceAggregation());
|
|
|
// 8)折扣
|
|
|
aggregations.add(aggregationFactory.getDiscountAggregation());
|
|
|
// 9)颜色
|
|
|
aggregations.add(aggregationFactory.getColorAggregation(paramMap));
|
|
|
// 10)尺码
|
|
|
aggregations.add(aggregationFactory.getSizeAggregation());
|
|
|
// 11)风格
|
|
|
aggregations.add(aggregationFactory.getStyleAggregation(paramMap));
|
|
|
return aggregations;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @断码区的聚合选项:
|
|
|
* @比其他的场景少了品类和尺码的筛选项
|
|
|
*/
|
|
|
public List<IAggregation> getBreakSizeAggregations(Map<String, String> paramMap) {
|
|
|
List<IAggregation> aggregations = new ArrayList<>();
|
|
|
// 1)专区名称
|
|
|
aggregations.add(aggregationFactory.getZqNameAggregation(1000));
|
|
|
// 2)全球购
|
|
|
aggregations.add(aggregationFactory.getIsGlobalAggregation());
|
|
|
// 3)品牌
|
|
|
aggregations.add(aggregationFactory.getBrandAggregation(paramMap));
|
|
|
// 4)人群
|
|
|
aggregations.add(aggregationFactory.getAgeLevelAggregation());
|
|
|
// 5)性别
|
|
|
aggregations.add(aggregationFactory.getGenderNewAggregation());
|
|
|
// 6)价格
|
|
|
aggregations.add(aggregationFactory.getPriceAggregation());
|
|
|
// 7)折扣
|
|
|
aggregations.add(aggregationFactory.getDiscountAggregation());
|
|
|
// 8)颜色
|
|
|
aggregations.add(aggregationFactory.getColorAggregation(paramMap));
|
|
|
// 9)风格
|
|
|
aggregations.add(aggregationFactory.getStyleAggregation(paramMap));
|
|
|
return aggregations;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 最新降价的筛选项
|
|
|
*
|
|
|
* @param paramMap
|
|
|
* @return
|
|
|
*/
|
|
|
public List<IAggregation> getReducePriceAggregations(Map<String, String> paramMap) {
|
|
|
List<IAggregation> aggregations = new ArrayList<>();
|
|
|
// 1)性别
|
|
|
aggregations.add(aggregationFactory.getGenderNewAggregation());
|
|
|
// 2)品牌
|
|
|
aggregations.add(aggregationFactory.getBrandAggregation(paramMap));
|
|
|
// 3)品类
|
|
|
aggregations.add(aggregationFactory.getSortGroupAggregation(paramMap));
|
|
|
// 4)颜色
|
|
|
aggregations.add(aggregationFactory.getColorAggregation(paramMap));
|
|
|
// 5)尺码
|
|
|
aggregations.add(aggregationFactory.getSizeAggregation());
|
|
|
// 6)价格
|
|
|
aggregations.add(aggregationFactory.getPriceAggregation());
|
|
|
// 7)折扣
|
|
|
aggregations.add(aggregationFactory.getDiscountAggregation());
|
|
|
// 8)风格
|
|
|
aggregations.add(aggregationFactory.getStyleAggregation(paramMap));
|
|
|
return aggregations;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 老的店铺的筛选项
|
|
|
*
|
|
|
* @param paramMap
|
|
|
* @return
|
|
|
*/
|
|
|
public List<IAggregation> getShopOldAggregations(Map<String, String> paramMap) {
|
|
|
List<IAggregation> aggregations = new ArrayList<>();
|
|
|
// 1)性别
|
|
|
aggregations.add(aggregationFactory.getGenderNewAggregation());
|
|
|
// 2)品牌
|
|
|
aggregations.add(aggregationFactory.getBrandAggregation(paramMap));
|
|
|
// 3)品类
|
|
|
aggregations.add(aggregationFactory.getSortGroupAggregation(paramMap));
|
|
|
// 4)颜色
|
|
|
aggregations.add(aggregationFactory.getColorAggregation(paramMap));
|
|
|
// 5)尺码
|
|
|
aggregations.add(aggregationFactory.getSizeAggregation());
|
|
|
// 6)价格
|
|
|
aggregations.add(aggregationFactory.getPriceAggregation());
|
|
|
// 7)折扣
|
|
|
aggregations.add(aggregationFactory.getDiscountAggregation());
|
|
|
// 8)风格
|
|
|
aggregations.add(aggregationFactory.getStyleAggregation(paramMap));
|
|
|
return aggregations;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 老的新品到着的筛选项
|
|
|
*
|
|
|
* @param paramMap
|
|
|
* @return
|
|
|
*/
|
|
|
public List<IAggregation> getNewArrivalOldAggregations(Map<String, String> paramMap) {
|
|
|
List<IAggregation> aggregations = new ArrayList<>();
|
|
|
// 1)性别
|
|
|
aggregations.add(aggregationFactory.getGenderNewAggregation());
|
|
|
// 2)品牌
|
|
|
aggregations.add(aggregationFactory.getBrandAggregation(paramMap));
|
|
|
// 3)品类
|
|
|
aggregations.add(aggregationFactory.getSortGroupAggregation(paramMap));
|
|
|
// 4)颜色
|
|
|
aggregations.add(aggregationFactory.getColorAggregation(paramMap));
|
|
|
// 5)尺码
|
|
|
aggregations.add(aggregationFactory.getSizeAggregation());
|
|
|
// 6)价格
|
|
|
aggregations.add(aggregationFactory.getPriceAggregation());
|
|
|
// 7)折扣
|
|
|
aggregations.add(aggregationFactory.getDiscountAggregation());
|
|
|
// 8)风格
|
|
|
aggregations.add(aggregationFactory.getStyleAggregation(paramMap));
|
|
|
return aggregations;
|
|
|
}
|
|
|
|
|
|
private List<AbstractAggregationBuilder<?>> getAggregationBuilders(List<IAggregation> aggregations) {
|
|
|
List<AbstractAggregationBuilder<?>> builders = new ArrayList<>();
|
|
|
for (IAggregation aggregation : aggregations) {
|
|
|
builders.add(aggregation.getBuilder());
|
|
|
}
|
|
|
return builders;
|
|
|
}
|
|
|
|
|
|
private JSONObject genFilterResult(List<IAggregation> aggregations, Map<String, Aggregation> aggMaps) {
|
|
|
JSONObject filter = new JSONObject();
|
|
|
for (IAggregation aggregation : aggregations) {
|
|
|
Object response = aggregation.getAggregationResponseMap(aggMaps);
|
|
|
if (response == null) {
|
|
|
continue;
|
|
|
}
|
|
|
filter.put(aggregation.filterName(), response);
|
|
|
}
|
|
|
return filter;
|
|
|
}
|
|
|
|
|
|
private JSONObject getFiltersResults(Map<String, String> paramMap, SearchParam searchParam, List<IAggregation> aggregations) {
|
|
|
try {
|
|
|
// 1)构造聚合报文
|
|
|
searchParam.setAggregationBuilders(this.getAggregationBuilders(aggregations));
|
|
|
// 2)构造其他参数
|
|
|
final String productIndexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
|
|
|
searchParam.setOffset(100);// just for cache
|
|
|
searchParam.setSize(0);
|
|
|
|
|
|
// 先从缓存里取,取到则处理下缓存结果并直接返回
|
|
|
JSONObject cacheObject = searchCacheService.getJSONObjectFromCache(this.searchCache, productIndexName, searchParam);
|
|
|
if (cacheObject != null) {
|
|
|
SearchCacheMatchLogger.doSearchCacheMatchLog("/scene/aggregations.json", paramMap);
|
|
|
return cacheObject;
|
|
|
}
|
|
|
|
|
|
// 调用ES
|
|
|
SearchResult searchResult = searchCommonService.doSearch(productIndexName, searchParam);
|
|
|
Map<String, Aggregation> aggMaps = searchResult.getAggMaps();
|
|
|
if (aggMaps == null) {
|
|
|
return null;
|
|
|
}
|
|
|
// 构造返回结果
|
|
|
JSONObject dataMap = this.genFilterResult(aggregations, aggMaps);
|
|
|
searchCacheService.addJSONObjectToCache(this.searchCache, productIndexName, searchParam, dataMap);
|
|
|
return dataMap;
|
|
|
} catch (Exception e) {
|
|
|
logger.error("[func=getFiltersResults][params=" + paramMap + "]", e);
|
|
|
return new JSONObject();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private JSONObject getFiltersResult(Map<String, String> paramMap, List<IAggregation> aggregations, BoolQueryBuilder mustFilter) {
|
|
|
try {
|
|
|
Map<String, String> newParamMap = new HashMap<String, String>(paramMap);
|
|
|
SearchParam searchParam = searchParamHelper.buildWithMustFilter(newParamMap, mustFilter);
|
|
|
return this.getFiltersResults(paramMap, searchParam, aggregations);
|
|
|
} catch (Exception e) {
|
|
|
logger.error("[func=SceneSelectionsService][params=" + paramMap + "]", e);
|
|
|
return new JSONObject();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public SearchApiResult aggregations(Map<String, String> paramMap) throws Exception {
|
|
|
List<IAggregation> commonAggregations = this.getCommonAggregations(paramMap);
|
|
|
return this.aggregations(paramMap, commonAggregations, null);
|
|
|
}
|
|
|
|
|
|
public SearchApiResult aggregations(Map<String, String> paramMap, List<IAggregation> aggregations) throws Exception {
|
|
|
return this.aggregations(paramMap, aggregations, null);
|
|
|
}
|
|
|
|
|
|
public SearchApiResult aggregations(Map<String, String> paramMap, BoolQueryBuilder mustFilter) throws Exception {
|
|
|
List<IAggregation> commonAggregations = this.getCommonAggregations(paramMap);
|
|
|
return this.aggregations(paramMap, commonAggregations, mustFilter);
|
|
|
}
|
|
|
|
|
|
private SearchApiResult aggregations(Map<String, String> paramMap, List<IAggregation> aggregations, BoolQueryBuilder mustFilter) throws Exception {
|
|
|
// 1、获取通用筛选项
|
|
|
JSONObject commonFilters = this.getFiltersResult(paramMap, aggregations, mustFilter);
|
|
|
Map<String, Object> result = new JSONObject();
|
|
|
result.put("filter", commonFilters);
|
|
|
return new SearchApiResult().setData(result);
|
|
|
}
|
|
|
|
|
|
} |
|
|
package com.yoho.search.service.scene.aggregations;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.yoho.search.base.utils.ISearchConstants;
|
|
|
import com.yoho.search.cache.log.SearchCacheMatchLogger;
|
|
|
import com.yoho.search.cache.model.SearchCache;
|
|
|
import com.yoho.search.cache.beans.AbstractCacheAbleService;
|
|
|
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.models.SearchApiResult;
|
|
|
import com.yoho.search.service.scene.aggregations.impls.AggregationFactory;
|
|
|
import com.yoho.search.common.SearchCommonService;
|
|
|
import com.yoho.search.service.helper.SearchParamHelper;
|
|
|
import org.elasticsearch.index.query.BoolQueryBuilder;
|
|
|
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
|
|
|
import org.elasticsearch.search.aggregations.Aggregation;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
@Service
|
|
|
public class SceneSelectionsService extends AbstractCacheAbleService {
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(SceneSelectionsService.class);
|
|
|
|
|
|
@Autowired
|
|
|
private SearchParamHelper searchParamHelper;
|
|
|
@Autowired
|
|
|
private SearchCommonService searchCommonService;
|
|
|
@Autowired
|
|
|
private AggregationFactory aggregationFactory;
|
|
|
|
|
|
@Override
|
|
|
public SearchCache getSearchCache() {
|
|
|
return searchCacheFactory.getSceneAggregationsCache();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @新版筛选器的聚合选项:
|
|
|
* @1、全球购
|
|
|
* @2、促销
|
|
|
* @3、全部的品牌 +喜欢的品牌【个性化的】
|
|
|
* @4、人群
|
|
|
* @5、品类
|
|
|
* @6、性别
|
|
|
* @7、价格
|
|
|
* @8、折扣
|
|
|
* @9、颜色
|
|
|
* @10、尺码
|
|
|
* @11、风格
|
|
|
*/
|
|
|
|
|
|
public List<IAggregation> getCommonAggregations(Map<String, String> paramMap) {
|
|
|
List<IAggregation> aggregations = new ArrayList<>();
|
|
|
// 1)专区名称
|
|
|
aggregations.add(aggregationFactory.getZqNameAggregation(1000));
|
|
|
// 2)全球购
|
|
|
aggregations.add(aggregationFactory.getIsGlobalAggregation());
|
|
|
// 3)品牌
|
|
|
aggregations.add(aggregationFactory.getBrandAggregation(paramMap));
|
|
|
// 4)人群
|
|
|
aggregations.add(aggregationFactory.getAgeLevelAggregation());
|
|
|
// 5)品类
|
|
|
aggregations.add(aggregationFactory.getSortGroupAggregation(paramMap));
|
|
|
// 6)性别
|
|
|
aggregations.add(aggregationFactory.getGenderNewAggregation());
|
|
|
// 7)价格
|
|
|
aggregations.add(aggregationFactory.getPriceAggregation());
|
|
|
// 8)折扣
|
|
|
aggregations.add(aggregationFactory.getDiscountAggregation());
|
|
|
// 9)颜色
|
|
|
aggregations.add(aggregationFactory.getColorAggregation(paramMap));
|
|
|
// 10)尺码
|
|
|
aggregations.add(aggregationFactory.getSizeAggregation());
|
|
|
// 11)风格
|
|
|
aggregations.add(aggregationFactory.getStyleAggregation(paramMap));
|
|
|
return aggregations;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @断码区的聚合选项:
|
|
|
* @比其他的场景少了品类和尺码的筛选项
|
|
|
*/
|
|
|
public List<IAggregation> getBreakSizeAggregations(Map<String, String> paramMap) {
|
|
|
List<IAggregation> aggregations = new ArrayList<>();
|
|
|
// 1)专区名称
|
|
|
aggregations.add(aggregationFactory.getZqNameAggregation(1000));
|
|
|
// 2)全球购
|
|
|
aggregations.add(aggregationFactory.getIsGlobalAggregation());
|
|
|
// 3)品牌
|
|
|
aggregations.add(aggregationFactory.getBrandAggregation(paramMap));
|
|
|
// 4)人群
|
|
|
aggregations.add(aggregationFactory.getAgeLevelAggregation());
|
|
|
// 5)性别
|
|
|
aggregations.add(aggregationFactory.getGenderNewAggregation());
|
|
|
// 6)价格
|
|
|
aggregations.add(aggregationFactory.getPriceAggregation());
|
|
|
// 7)折扣
|
|
|
aggregations.add(aggregationFactory.getDiscountAggregation());
|
|
|
// 8)颜色
|
|
|
aggregations.add(aggregationFactory.getColorAggregation(paramMap));
|
|
|
// 9)风格
|
|
|
aggregations.add(aggregationFactory.getStyleAggregation(paramMap));
|
|
|
return aggregations;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 最新降价的筛选项
|
|
|
*
|
|
|
* @param paramMap
|
|
|
* @return
|
|
|
*/
|
|
|
public List<IAggregation> getReducePriceAggregations(Map<String, String> paramMap) {
|
|
|
List<IAggregation> aggregations = new ArrayList<>();
|
|
|
// 1)性别
|
|
|
aggregations.add(aggregationFactory.getGenderNewAggregation());
|
|
|
// 2)品牌
|
|
|
aggregations.add(aggregationFactory.getBrandAggregation(paramMap));
|
|
|
// 3)品类
|
|
|
aggregations.add(aggregationFactory.getSortGroupAggregation(paramMap));
|
|
|
// 4)颜色
|
|
|
aggregations.add(aggregationFactory.getColorAggregation(paramMap));
|
|
|
// 5)尺码
|
|
|
aggregations.add(aggregationFactory.getSizeAggregation());
|
|
|
// 6)价格
|
|
|
aggregations.add(aggregationFactory.getPriceAggregation());
|
|
|
// 7)折扣
|
|
|
aggregations.add(aggregationFactory.getDiscountAggregation());
|
|
|
// 8)风格
|
|
|
aggregations.add(aggregationFactory.getStyleAggregation(paramMap));
|
|
|
return aggregations;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 老的店铺的筛选项
|
|
|
*
|
|
|
* @param paramMap
|
|
|
* @return
|
|
|
*/
|
|
|
public List<IAggregation> getShopOldAggregations(Map<String, String> paramMap) {
|
|
|
List<IAggregation> aggregations = new ArrayList<>();
|
|
|
// 1)性别
|
|
|
aggregations.add(aggregationFactory.getGenderNewAggregation());
|
|
|
// 2)品牌
|
|
|
aggregations.add(aggregationFactory.getBrandAggregation(paramMap));
|
|
|
// 3)品类
|
|
|
aggregations.add(aggregationFactory.getSortGroupAggregation(paramMap));
|
|
|
// 4)颜色
|
|
|
aggregations.add(aggregationFactory.getColorAggregation(paramMap));
|
|
|
// 5)尺码
|
|
|
aggregations.add(aggregationFactory.getSizeAggregation());
|
|
|
// 6)价格
|
|
|
aggregations.add(aggregationFactory.getPriceAggregation());
|
|
|
// 7)折扣
|
|
|
aggregations.add(aggregationFactory.getDiscountAggregation());
|
|
|
// 8)风格
|
|
|
aggregations.add(aggregationFactory.getStyleAggregation(paramMap));
|
|
|
return aggregations;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 老的新品到着的筛选项
|
|
|
*
|
|
|
* @param paramMap
|
|
|
* @return
|
|
|
*/
|
|
|
public List<IAggregation> getNewArrivalOldAggregations(Map<String, String> paramMap) {
|
|
|
List<IAggregation> aggregations = new ArrayList<>();
|
|
|
// 1)性别
|
|
|
aggregations.add(aggregationFactory.getGenderNewAggregation());
|
|
|
// 2)品牌
|
|
|
aggregations.add(aggregationFactory.getBrandAggregation(paramMap));
|
|
|
// 3)品类
|
|
|
aggregations.add(aggregationFactory.getSortGroupAggregation(paramMap));
|
|
|
// 4)颜色
|
|
|
aggregations.add(aggregationFactory.getColorAggregation(paramMap));
|
|
|
// 5)尺码
|
|
|
aggregations.add(aggregationFactory.getSizeAggregation());
|
|
|
// 6)价格
|
|
|
aggregations.add(aggregationFactory.getPriceAggregation());
|
|
|
// 7)折扣
|
|
|
aggregations.add(aggregationFactory.getDiscountAggregation());
|
|
|
// 8)风格
|
|
|
aggregations.add(aggregationFactory.getStyleAggregation(paramMap));
|
|
|
return aggregations;
|
|
|
}
|
|
|
|
|
|
private List<AbstractAggregationBuilder<?>> getAggregationBuilders(List<IAggregation> aggregations) {
|
|
|
List<AbstractAggregationBuilder<?>> builders = new ArrayList<>();
|
|
|
for (IAggregation aggregation : aggregations) {
|
|
|
builders.add(aggregation.getBuilder());
|
|
|
}
|
|
|
return builders;
|
|
|
}
|
|
|
|
|
|
private JSONObject genFilterResult(List<IAggregation> aggregations, Map<String, Aggregation> aggMaps) {
|
|
|
JSONObject filter = new JSONObject();
|
|
|
for (IAggregation aggregation : aggregations) {
|
|
|
Object response = aggregation.getAggregationResponseMap(aggMaps);
|
|
|
if (response == null) {
|
|
|
continue;
|
|
|
}
|
|
|
filter.put(aggregation.filterName(), response);
|
|
|
}
|
|
|
return filter;
|
|
|
}
|
|
|
|
|
|
private JSONObject getFiltersResults(Map<String, String> paramMap, SearchParam searchParam, List<IAggregation> aggregations) {
|
|
|
try {
|
|
|
// 1)构造聚合报文
|
|
|
searchParam.setAggregationBuilders(this.getAggregationBuilders(aggregations));
|
|
|
// 2)构造其他参数
|
|
|
final String productIndexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
|
|
|
searchParam.setOffset(100);// just for cache
|
|
|
searchParam.setSize(0);
|
|
|
|
|
|
// 先从缓存里取,取到则处理下缓存结果并直接返回
|
|
|
JSONObject cacheObject = searchCacheService.getJSONObjectFromCache(this.searchCache, productIndexName, searchParam);
|
|
|
if (cacheObject != null) {
|
|
|
SearchCacheMatchLogger.doSearchCacheMatchLog("/scene/aggregations.json", paramMap);
|
|
|
return cacheObject;
|
|
|
}
|
|
|
|
|
|
// 调用ES
|
|
|
SearchResult searchResult = searchCommonService.doSearch(productIndexName, searchParam);
|
|
|
Map<String, Aggregation> aggMaps = searchResult.getAggMaps();
|
|
|
if (aggMaps == null) {
|
|
|
return null;
|
|
|
}
|
|
|
// 构造返回结果
|
|
|
JSONObject dataMap = this.genFilterResult(aggregations, aggMaps);
|
|
|
searchCacheService.addJSONObjectToCache(this.searchCache, productIndexName, searchParam, dataMap);
|
|
|
return dataMap;
|
|
|
} catch (Exception e) {
|
|
|
logger.error("[func=getFiltersResults][params=" + paramMap + "]", e);
|
|
|
return new JSONObject();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private JSONObject getFiltersResult(Map<String, String> paramMap, List<IAggregation> aggregations, BoolQueryBuilder mustFilter) {
|
|
|
try {
|
|
|
Map<String, String> newParamMap = new HashMap<String, String>(paramMap);
|
|
|
SearchParam searchParam = searchParamHelper.buildWithMustFilter(newParamMap, mustFilter);
|
|
|
return this.getFiltersResults(paramMap, searchParam, aggregations);
|
|
|
} catch (Exception e) {
|
|
|
logger.error("[func=SceneSelectionsService][params=" + paramMap + "]", e);
|
|
|
return new JSONObject();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public SearchApiResult aggregations(Map<String, String> paramMap) throws Exception {
|
|
|
List<IAggregation> commonAggregations = this.getCommonAggregations(paramMap);
|
|
|
return this.aggregations(paramMap, commonAggregations, null);
|
|
|
}
|
|
|
|
|
|
public SearchApiResult aggregations(Map<String, String> paramMap, List<IAggregation> aggregations) throws Exception {
|
|
|
return this.aggregations(paramMap, aggregations, null);
|
|
|
}
|
|
|
|
|
|
public SearchApiResult aggregations(Map<String, String> paramMap, BoolQueryBuilder mustFilter) throws Exception {
|
|
|
List<IAggregation> commonAggregations = this.getCommonAggregations(paramMap);
|
|
|
return this.aggregations(paramMap, commonAggregations, mustFilter);
|
|
|
}
|
|
|
|
|
|
private SearchApiResult aggregations(Map<String, String> paramMap, List<IAggregation> aggregations, BoolQueryBuilder mustFilter) throws Exception {
|
|
|
// 1、获取通用筛选项
|
|
|
JSONObject commonFilters = this.getFiltersResult(paramMap, aggregations, mustFilter);
|
|
|
Map<String, Object> result = new JSONObject();
|
|
|
result.put("filter", commonFilters);
|
|
|
return new SearchApiResult().setData(result);
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|