...
|
...
|
@@ -11,7 +11,9 @@ import java.util.List; |
|
|
import java.util.Map;
|
|
|
import java.util.Map.Entry;
|
|
|
|
|
|
import com.yoho.search.base.utils.ConvertUtils;
|
|
|
import com.yoho.search.service.helper.ProductListHelper;
|
|
|
import org.apache.commons.collections.MapUtils;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.elasticsearch.index.query.BoolQueryBuilder;
|
|
|
import org.elasticsearch.index.query.QueryBuilders;
|
...
|
...
|
@@ -65,397 +67,377 @@ import com.yoho.search.service.service.IBrandService; |
|
|
@Service
|
|
|
public class BrandServiceImpl extends AbstractCacheAbleService implements IBrandService, ApplicationEventPublisherAware {
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(BrandServiceImpl.class);
|
|
|
|
|
|
@Autowired
|
|
|
private SearchSortHelper searchSortHelper;
|
|
|
@Autowired
|
|
|
private SearchParamHelper searchParamHelper;
|
|
|
@Autowired
|
|
|
private AggregationService aggregationService;
|
|
|
@Autowired
|
|
|
private SearchCommonService searchCommonService;
|
|
|
@Autowired
|
|
|
private BrandIndexBaseService brandIndexBaseService;
|
|
|
@Autowired
|
|
|
private ProductIndexBaseService productIndexBaseService;
|
|
|
@Autowired
|
|
|
private AggregationFactoryService aggregationFactoryService;
|
|
|
@Autowired
|
|
|
private SearchCommonHelper searchCommonHelper;
|
|
|
@Autowired
|
|
|
private ProductListHelper productListHelper;
|
|
|
|
|
|
@Override
|
|
|
public SearchCache getSearchCache() {
|
|
|
return searchCacheFactory.getBrandRelatedCache();
|
|
|
}
|
|
|
|
|
|
private ApplicationEventPublisher publisher;
|
|
|
|
|
|
@Override
|
|
|
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
|
|
|
this.publisher = applicationEventPublisher;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public SearchApiResult aggBrand(Map<String, String> paramMap) {
|
|
|
try {
|
|
|
logger.info("[func=aggBrand][param={}][begin={}]", paramMap.toString(), System.currentTimeMillis());
|
|
|
return this.aggBrand(paramMap, null);
|
|
|
} catch (Exception e) {
|
|
|
return SearchApiResultUtils.errorSearchApiResult("aggBrand", paramMap, e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public SearchApiResult aggBrand(Map<String, String> paramMap, BoolQueryBuilder mustFilter) {
|
|
|
try {
|
|
|
// 1、构造带filter和query的SearchParam
|
|
|
boolean needPreAggregation = "Y".equals(paramMap.getOrDefault(SearchRequestParams.PARAM_SEARCH_AGG_WITH_PARAM_BRAND, "N")) ? false : true;
|
|
|
SearchParam searchParam = searchParamHelper.buildSearchParam(paramMap, false, mustFilter, needPreAggregation ? "brand" : null);
|
|
|
|
|
|
// 2、构造aggrations
|
|
|
IAggregation brandAggregation = aggregationFactoryService.getBrandAggregation(paramMap);
|
|
|
searchParam.setAggregationBuilders(Arrays.asList(brandAggregation.getBuilder()));
|
|
|
|
|
|
// 3、从缓存中获取
|
|
|
final String indexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
|
|
|
JSONArray cacheJSONArray = searchCacheService.getJSONArrayFromCache(this.searchCache, indexName, searchParam);
|
|
|
if (cacheJSONArray != null) {
|
|
|
SearchCacheMatchLogger.doSearchCacheMatchLog("/productindex/aggBrand.json", paramMap);
|
|
|
return new SearchApiResult().setData(cacheJSONArray);
|
|
|
}
|
|
|
// 4、从ES中获取
|
|
|
JSONObject jsonObject = aggregationService.getAggNameAndResponse(brandAggregation, searchParam);
|
|
|
if (jsonObject == null) {
|
|
|
return new SearchApiResult().setData(500).setMessage("exception");
|
|
|
}
|
|
|
// 5、生成结果并且加入缓存
|
|
|
JSONArray brandJSONArray = jsonObject.getJSONArray(brandAggregation.aggName());
|
|
|
if (brandJSONArray != null) {
|
|
|
searchCacheService.addJSONArrayToCache(this.searchCache, indexName, searchParam, brandJSONArray);
|
|
|
}
|
|
|
return new SearchApiResult().setData(brandJSONArray);
|
|
|
} catch (Exception e) {
|
|
|
return SearchApiResultUtils.errorSearchApiResult("aggBrand", paramMap, e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public SearchApiResult brands(Map<String, String> paramMap) {
|
|
|
try {
|
|
|
logger.info("[func=brands][param={}][begin={}]", paramMap.toString(), System.currentTimeMillis());
|
|
|
// 1、构造查询参数
|
|
|
SearchParam searchParam = searchParamHelper.buildDefault(paramMap);
|
|
|
|
|
|
// 2、构造聚合参数
|
|
|
List<AbstractAggregationBuilder<?>> list = new ArrayList<AbstractAggregationBuilder<?>>();
|
|
|
list.add(AggregationBuilders.terms("brandAlifAgg").field(ProductIndexEsField.brandAlif).size(1000).order(Terms.Order.term(true))
|
|
|
.subAggregation(AggregationBuilders.terms("brandAgg").field(ProductIndexEsField.brandId).size(1000)));
|
|
|
searchParam.setAggregationBuilders(list);
|
|
|
searchParam.setSize(0);
|
|
|
|
|
|
// 3、brand数据量比较大,走本地缓存
|
|
|
final String indexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
|
|
|
JSONObject cacheObject = searchCacheService.getJSONObjectFromCache(this.searchCache, indexName, searchParam);
|
|
|
if (cacheObject != null) {
|
|
|
SearchCacheMatchLogger.doSearchCacheMatchLog("/productindex/brands.json", paramMap);
|
|
|
return new SearchApiResult().setData(cacheObject);
|
|
|
}
|
|
|
// 4、查询ES
|
|
|
SearchResult searchResult = searchCommonService.doSearch(indexName, searchParam);
|
|
|
SearchApiResult searchApiResult = new SearchApiResult().setMessage("brand List.");
|
|
|
if (searchResult == null || searchResult.getAggMaps() == null) {
|
|
|
return searchApiResult.setCode(500);
|
|
|
}
|
|
|
Map<String, Aggregation> aggregationResult = searchResult.getAggMaps();
|
|
|
if (!aggregationResult.containsKey("brandAlifAgg")) {
|
|
|
return searchApiResult;
|
|
|
}
|
|
|
// 5、构造返回结果并加入缓存
|
|
|
JSONObject result = new JSONObject();
|
|
|
result.put("brands", makeBrandResponse(((MultiBucketsAggregation) aggregationResult.get("brandAlifAgg"))));
|
|
|
searchCacheService.addJSONObjectToCache(this.searchCache, indexName, searchParam, result);
|
|
|
return searchApiResult.setData(result);
|
|
|
} catch (Exception e) {
|
|
|
publisher.publishEvent(new SearchEvent(EventReportEnum.SEARCHCONTROLLER_BRANDS.getEventName(), EventReportEnum.SEARCHCONTROLLER_BRANDS.getFunctionName(),
|
|
|
EventReportEnum.SEARCHCONTROLLER_BRANDS.getMoudleName(), "exception", IgnoreSomeException.filterSomeException(e), null));
|
|
|
return SearchApiResultUtils.errorSearchApiResult("brands", paramMap, e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 制作品牌报文
|
|
|
*/
|
|
|
private Map<String, JSONArray> makeBrandResponse(MultiBucketsAggregation aggregation) {
|
|
|
// 1)获取每个brandAlif对应的brandIds
|
|
|
Map<String, List<String>> brandAlif2BrandIds = new LinkedHashMap<String, List<String>>();
|
|
|
for (Bucket brandAlifBucket : aggregation.getBuckets()) {
|
|
|
String brandAlif = brandAlifBucket.getKeyAsString();
|
|
|
List<String> brandIds = new ArrayList<String>();
|
|
|
MultiBucketsAggregation brandAggs = (MultiBucketsAggregation) brandAlifBucket.getAggregations().asMap().get("brandAgg");
|
|
|
for (Bucket brandIdBucket : brandAggs.getBuckets()) {
|
|
|
brandIds.addAll(Arrays.asList(brandIdBucket.getKeyAsString().split(",")));
|
|
|
}
|
|
|
brandAlif2BrandIds.put(brandAlif, brandIds);
|
|
|
}
|
|
|
// 2)获取所有的品牌id
|
|
|
List<String> brandIds = new ArrayList<String>();
|
|
|
for (Map.Entry<String, List<String>> entry : brandAlif2BrandIds.entrySet()) {
|
|
|
brandIds.addAll(entry.getValue());
|
|
|
}
|
|
|
// 3)获取所有的品牌数据
|
|
|
List<Map<String, Object>> brandList = brandIndexBaseService.getBrandListByIds(brandIds);
|
|
|
Map<String, Map<String, Object>> brandIdMap = new HashMap<String, Map<String, Object>>();
|
|
|
for (Map<String, Object> brand : brandList) {
|
|
|
brandIdMap.put(brand.getOrDefault("id", 0).toString(), brand);
|
|
|
}
|
|
|
// 4)构造真正的数据
|
|
|
Map<String, JSONArray> result = new LinkedHashMap<String, JSONArray>();
|
|
|
for (Map.Entry<String, List<String>> entry : brandAlif2BrandIds.entrySet()) {
|
|
|
String brandAlif = entry.getKey();
|
|
|
JSONArray brands = new JSONArray();
|
|
|
for (String brandId : entry.getValue()) {
|
|
|
Map<String, Object> brand = brandIdMap.get(brandId);
|
|
|
if (brand != null) {
|
|
|
brands.add(brand);
|
|
|
}
|
|
|
}
|
|
|
result.put(brandAlif, brands);
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public SearchApiResult brandList(Map<String, String> paramMap) {
|
|
|
try {
|
|
|
logger.info("[func=brandList][param={}][begin={}]", paramMap.toString(), System.currentTimeMillis());
|
|
|
|
|
|
// 1)构建参数
|
|
|
SearchParam searchParam = new SearchParam();
|
|
|
searchParam.setSize(10000);
|
|
|
searchParam.setQuery(QueryBuilders.matchAllQuery());
|
|
|
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
|
|
|
boolQueryBuilder.mustNot(QueryBuilders.termQuery("isGlobal", "Y"));
|
|
|
searchParam.setFiter(boolQueryBuilder);
|
|
|
|
|
|
// 2、brand数据量比较大,走本地缓存。guavacache缓存中获取result,没有到es中获取
|
|
|
final String brandIndexName = ISearchConstants.INDEX_NAME_BRAND;
|
|
|
JSONArray cacheJSONArray = searchCacheService.getJSONArrayFromCache(this.searchCache, brandIndexName, searchParam);
|
|
|
if (cacheJSONArray != null) {
|
|
|
SearchCacheMatchLogger.doSearchCacheMatchLog("/brand/list.json", paramMap);
|
|
|
return new SearchApiResult().setData(cacheJSONArray);
|
|
|
}
|
|
|
// 3、执行搜索
|
|
|
SearchResult searchResult = searchCommonService.doSearch(brandIndexName, searchParam);
|
|
|
if (searchResult == null || searchResult.getResultList().isEmpty()) {
|
|
|
return new SearchApiResult().setData(400).setMessage("empty result");
|
|
|
}
|
|
|
// 4、构建返回结果并加入缓存
|
|
|
List<Map<String, Object>> result = searchResult.getResultList();
|
|
|
JSONArray jsonArray = new JSONArray();
|
|
|
for (Map<String, Object> map : result) {
|
|
|
Map<String, Object> brandInfo = brandIndexBaseService.getBrandMap(map);
|
|
|
jsonArray.add(brandInfo);
|
|
|
}
|
|
|
searchCacheService.addJSONArrayToCache(this.searchCache, brandIndexName, searchParam, jsonArray);
|
|
|
return new SearchApiResult().setMessage("brands info").setData(jsonArray);
|
|
|
} catch (Exception e) {
|
|
|
publisher.publishEvent(new SearchEvent(EventReportEnum.BRANDCONTROLLER_BRAND_LIST.getEventName(), EventReportEnum.BRANDCONTROLLER_BRAND_LIST.getFunctionName(),
|
|
|
EventReportEnum.BRANDCONTROLLER_BRAND_LIST.getMoudleName(), "exception", IgnoreSomeException.filterSomeException(e), null));
|
|
|
return SearchApiResultUtils.errorSearchApiResult("brandList", paramMap, e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public SearchApiResult groupBrands(Map<String, String> paramMap) {
|
|
|
try {
|
|
|
logger.info("[func=groupBrands][param={}][begin={}]", paramMap.toString(), System.currentTimeMillis());
|
|
|
// 1、参数检验
|
|
|
if (!paramMap.containsKey("brand")) {
|
|
|
return new SearchApiResult().setCode(400).setMessage("没有 brand参数 ");
|
|
|
}
|
|
|
// 2、获取聚合条件
|
|
|
int topHitCount = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum"));
|
|
|
// 3、是否需要个性化
|
|
|
String topHitOrder = "heatValue:desc";
|
|
|
if (topHitCount > 0 && searchCommonHelper.isNeedPersonalSearch(paramMap)) {
|
|
|
topHitOrder = "_score:desc";
|
|
|
}
|
|
|
|
|
|
// 4、构建searchParam
|
|
|
SearchParam searchParam = searchParamHelper.buildWithPersional(paramMap, topHitCount > 0 ? true : false);
|
|
|
IAggregation groupBrandAgg = aggregationFactoryService.getGroupBrandAggregation(topHitOrder, topHitCount);
|
|
|
searchParam.setAggregationBuilders(Arrays.asList(groupBrandAgg.getBuilder()));
|
|
|
searchParam.setSize(0);
|
|
|
searchParam.setOffset(topHitCount * 2);// justForCache
|
|
|
|
|
|
// 5、从缓存中获取
|
|
|
final String productIndexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
|
|
|
JSONObject groupBrandResult = searchCacheService.getJSONObjectFromCache(this.searchCache, productIndexName, searchParam);
|
|
|
if (groupBrandResult != null) {
|
|
|
SearchCacheMatchLogger.doSearchCacheMatchLog("/productindex/groupBrands.json", paramMap);
|
|
|
return new SearchApiResult().setData(groupBrandResult);
|
|
|
}
|
|
|
// 6、从ES中获取
|
|
|
groupBrandResult = aggregationService.getAggNameAndResponseWithTotal(groupBrandAgg, searchParam);
|
|
|
if (groupBrandResult == null) {
|
|
|
return new SearchApiResult().setData(500).setMessage("exception");
|
|
|
}
|
|
|
// 7、生成结果并且加入缓存
|
|
|
JSONObject realResult = new JSONObject();
|
|
|
realResult.put("total", groupBrandResult.getOrDefault("total", 0));
|
|
|
realResult.put("brands", groupBrandResult.getOrDefault(groupBrandAgg.aggName(), new JSONObject()));
|
|
|
searchCacheService.addJSONObjectToCache(this.searchCache, productIndexName, searchParam, realResult);
|
|
|
return new SearchApiResult().setData(realResult).setMessage("groupBrands new List.");
|
|
|
} catch (Exception e) {
|
|
|
publisher.publishEvent(new SearchEvent(EventReportEnum.SEARCHCONTROLLER_GROUP_BRANDS.getEventName(), EventReportEnum.SEARCHCONTROLLER_GROUP_BRANDS.getFunctionName(),
|
|
|
EventReportEnum.SEARCHCONTROLLER_GROUP_BRANDS.getMoudleName(), "exception", IgnoreSomeException.filterSomeException(e), null));
|
|
|
return SearchApiResultUtils.errorSearchApiResult("groupBrands", paramMap, e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
@Override
|
|
|
public SearchApiResult aggProductsByBrandIdInParam(Map<String, String> paramMap) {
|
|
|
try {
|
|
|
logger.info("[func=aggProductsByBrandIdInParam][param={}][begin={}]", paramMap.toString(), System.currentTimeMillis());
|
|
|
|
|
|
// 1、参数校验
|
|
|
if (!paramMap.containsKey("brand")) {
|
|
|
return new SearchApiResult().setCode(400).setMessage("没有 brand参数 ");
|
|
|
}
|
|
|
// 2、处理排序方式
|
|
|
String sortField = searchSortHelper.getLegalOrder(paramMap);
|
|
|
if (StringUtils.isBlank(sortField)) {
|
|
|
sortField = "shelveTime:desc";
|
|
|
}
|
|
|
String[] sortParts = sortField.split(":");
|
|
|
String realSortField = sortParts[0];
|
|
|
String realSortType = sortParts[1];
|
|
|
SortOrder realSortOrder = SortOrder.ASC.toString().equalsIgnoreCase(realSortType) ? SortOrder.ASC : SortOrder.DESC;
|
|
|
|
|
|
// 3、构造查询参数
|
|
|
SearchParam searchParam = searchParamHelper.buildDefault(paramMap);
|
|
|
|
|
|
// 4、构造聚合参数
|
|
|
List<AbstractAggregationBuilder<?>> list = new ArrayList<AbstractAggregationBuilder<?>>();
|
|
|
// 获取取商品的数量
|
|
|
// 构造品牌聚合参数[最多取100个品牌的一个商品,取回来之后再做排序]
|
|
|
TermsAggregationBuilder brandAggregationBuilder = AggregationBuilders.terms("brandAgg").field(ProductIndexEsField.brandId).size(100);
|
|
|
// 再构造对应HIT到的商品的聚合参数
|
|
|
FieldSortBuilder productSortBuilder = SortBuilders.fieldSort(realSortField).order(realSortOrder);
|
|
|
TopHitsAggregationBuilder productTopHitsBuilder = AggregationBuilders.topHits("product").sort(productSortBuilder).size(1);
|
|
|
brandAggregationBuilder.subAggregation(productTopHitsBuilder);
|
|
|
list.add(brandAggregationBuilder);
|
|
|
searchParam.setAggregationBuilders(list);
|
|
|
|
|
|
// 5、设置报文缓存参数
|
|
|
int limit = StringUtils.isBlank(paramMap.get("limit")) ? 1 : Integer.parseInt(paramMap.get("limit"));
|
|
|
searchParam.setOffset(limit);// 这个参数只是用来生成报文以做缓存,不要动
|
|
|
searchParam.setSize(0);
|
|
|
|
|
|
// 6、构造返回结果
|
|
|
SearchApiResult searchApiResult = new SearchApiResult().setMessage("new product List.");
|
|
|
|
|
|
// 7、先从缓存中获取,如果能取到,则直接返回
|
|
|
JSONArray jsonArray = searchCacheService.getJSONArrayFromCache(this.searchCache, ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
|
|
|
if (jsonArray != null) {
|
|
|
SearchCacheMatchLogger.doSearchCacheMatchLog("/new_product.json", paramMap);
|
|
|
return searchApiResult.setData(jsonArray);
|
|
|
}
|
|
|
|
|
|
// 8、查询ES数据
|
|
|
final String indexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
|
|
|
SearchResult searchResult = searchCommonService.doSearch(indexName, searchParam);
|
|
|
if (searchResult == null || searchResult.getAggMaps() == null) {
|
|
|
return searchApiResult.setData("");
|
|
|
}
|
|
|
Map<String, Aggregation> aggMaps = searchResult.getAggMaps();
|
|
|
if (!aggMaps.containsKey("brandAgg")) {
|
|
|
return searchApiResult.setData("");
|
|
|
}
|
|
|
|
|
|
// 9、构造返回数据并加入缓存
|
|
|
jsonArray = getNewBrandResponseMap(((MultiBucketsAggregation) aggMaps.get("brandAgg")), limit, paramMap.get("brand"), realSortField, realSortOrder);
|
|
|
searchCacheService.addJSONArrayToCache(this.searchCache, indexName, searchParam, jsonArray);
|
|
|
return searchApiResult.setData(jsonArray);
|
|
|
} catch (Exception e) {
|
|
|
publisher.publishEvent(new SearchEvent(EventReportEnum.SEARCHCONTROLLER_NEW_PRODUCT.getEventName(), EventReportEnum.SEARCHCONTROLLER_NEW_PRODUCT.getFunctionName(),
|
|
|
EventReportEnum.SEARCHCONTROLLER_NEW_PRODUCT.getMoudleName(), "exception", IgnoreSomeException.filterSomeException(e), null));
|
|
|
return SearchApiResultUtils.errorSearchApiResult("searchNewestProductWithDiffBrand", paramMap, e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private JSONArray getNewBrandResponseMap(MultiBucketsAggregation aggregation, int size, String brands, final String realSortField, final SortOrder realSortOrder) {
|
|
|
Iterator<? extends Bucket> itAgg = aggregation.getBuckets().iterator();
|
|
|
// 首先获取全部品牌的商品列表
|
|
|
Map<String, Map<String, Object>> brand_product_map = new HashMap<>();
|
|
|
while (itAgg.hasNext()) {
|
|
|
Bucket lt = itAgg.next();
|
|
|
if (lt.getAggregations().getAsMap().containsKey("product")) {
|
|
|
TopHits topHits = lt.getAggregations().get("product");
|
|
|
if (topHits != null) {
|
|
|
SearchHits hits = topHits.getHits();
|
|
|
for (SearchHit hit : hits.getHits()) {
|
|
|
brand_product_map.put(lt.getKeyAsString(), hit.getSource());
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
brand_product_map = productListHelper.buildReturnInfoByEsSourceMap(brand_product_map);
|
|
|
|
|
|
// 获取前面“size”个品牌的商品
|
|
|
if (brand_product_map.size() == 0) {
|
|
|
return new JSONArray();
|
|
|
}
|
|
|
// 购造商品列表
|
|
|
List<Map<String, Object>> productList = new ArrayList<Map<String, Object>>();
|
|
|
String[] brandIds = brands.split(",");
|
|
|
if (brandIds.length < size || brand_product_map.size() <= size) {
|
|
|
for (Entry<String, Map<String, Object>> entry : brand_product_map.entrySet()) {
|
|
|
productList.add(entry.getValue());
|
|
|
}
|
|
|
} else {
|
|
|
int num = 0;
|
|
|
int i = 0;
|
|
|
while (num < size) {
|
|
|
if (brand_product_map.containsKey(brandIds[i])) {
|
|
|
productList.add(brand_product_map.get(brandIds[i]));
|
|
|
num++;
|
|
|
}
|
|
|
i++;
|
|
|
}
|
|
|
}
|
|
|
// 再按照某个字段对商品排序
|
|
|
Collections.sort(productList, new Comparator<Map<String, Object>>() {
|
|
|
public int compare(Map<String, Object> o1, Map<String, Object> o2) {
|
|
|
Object realSortFieldValue1 = o1.get(realSortField);
|
|
|
Object realSortFieldValue2 = o1.get(realSortField);
|
|
|
int map1value = getIntValue(realSortFieldValue1);
|
|
|
int map2value = getIntValue(realSortFieldValue2);
|
|
|
if ("desc".equalsIgnoreCase(realSortOrder.name())) {
|
|
|
return map2value - map1value;
|
|
|
} else {
|
|
|
return map1value - map2value;
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
JSONArray jsonArray = new JSONArray();
|
|
|
jsonArray.addAll(productList);
|
|
|
return jsonArray;
|
|
|
}
|
|
|
|
|
|
private int getIntValue(Object value) {
|
|
|
if (value == null) {
|
|
|
return 0;
|
|
|
}
|
|
|
try {
|
|
|
return Integer.parseInt(value.toString());
|
|
|
} catch (Exception e) {
|
|
|
return 0;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(BrandServiceImpl.class);
|
|
|
|
|
|
@Autowired
|
|
|
private SearchSortHelper searchSortHelper;
|
|
|
@Autowired
|
|
|
private SearchParamHelper searchParamHelper;
|
|
|
@Autowired
|
|
|
private AggregationService aggregationService;
|
|
|
@Autowired
|
|
|
private SearchCommonService searchCommonService;
|
|
|
@Autowired
|
|
|
private BrandIndexBaseService brandIndexBaseService;
|
|
|
@Autowired
|
|
|
private AggregationFactoryService aggregationFactoryService;
|
|
|
@Autowired
|
|
|
private SearchCommonHelper searchCommonHelper;
|
|
|
@Autowired
|
|
|
private ProductListHelper productListHelper;
|
|
|
|
|
|
@Override
|
|
|
public SearchCache getSearchCache() {
|
|
|
return searchCacheFactory.getBrandRelatedCache();
|
|
|
}
|
|
|
|
|
|
private ApplicationEventPublisher publisher;
|
|
|
|
|
|
@Override
|
|
|
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
|
|
|
this.publisher = applicationEventPublisher;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public SearchApiResult aggBrand(Map<String, String> paramMap) {
|
|
|
try {
|
|
|
logger.info("[func=aggBrand][param={}][begin={}]", paramMap.toString(), System.currentTimeMillis());
|
|
|
return this.aggBrand(paramMap, null);
|
|
|
} catch (Exception e) {
|
|
|
return SearchApiResultUtils.errorSearchApiResult("aggBrand", paramMap, e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public SearchApiResult aggBrand(Map<String, String> paramMap, BoolQueryBuilder mustFilter) {
|
|
|
try {
|
|
|
// 1、构造带filter和query的SearchParam
|
|
|
boolean needPreAggregation = "Y".equals(paramMap.getOrDefault(SearchRequestParams.PARAM_SEARCH_AGG_WITH_PARAM_BRAND, "N")) ? false : true;
|
|
|
SearchParam searchParam = searchParamHelper.buildSearchParam(paramMap, false, mustFilter, needPreAggregation ? "brand" : null);
|
|
|
|
|
|
// 2、构造aggrations
|
|
|
IAggregation brandAggregation = aggregationFactoryService.getBrandAggregation(paramMap);
|
|
|
searchParam.setAggregationBuilders(Arrays.asList(brandAggregation.getBuilder()));
|
|
|
|
|
|
// 3、从缓存中获取
|
|
|
final String indexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
|
|
|
JSONArray cacheJSONArray = searchCacheService.getJSONArrayFromCache(this.searchCache, indexName, searchParam);
|
|
|
if (cacheJSONArray != null) {
|
|
|
SearchCacheMatchLogger.doSearchCacheMatchLog("/productindex/aggBrand.json", paramMap);
|
|
|
return new SearchApiResult().setData(cacheJSONArray);
|
|
|
}
|
|
|
// 4、从ES中获取
|
|
|
JSONObject jsonObject = aggregationService.getAggNameAndResponse(brandAggregation, searchParam);
|
|
|
if (jsonObject == null) {
|
|
|
return new SearchApiResult().setData(500).setMessage("exception");
|
|
|
}
|
|
|
// 5、生成结果并且加入缓存
|
|
|
JSONArray brandJSONArray = jsonObject.getJSONArray(brandAggregation.aggName());
|
|
|
if (brandJSONArray != null) {
|
|
|
searchCacheService.addJSONArrayToCache(this.searchCache, indexName, searchParam, brandJSONArray);
|
|
|
}
|
|
|
return new SearchApiResult().setData(brandJSONArray);
|
|
|
} catch (Exception e) {
|
|
|
return SearchApiResultUtils.errorSearchApiResult("aggBrand", paramMap, e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public SearchApiResult brands(Map<String, String> paramMap) {
|
|
|
try {
|
|
|
logger.info("[func=brands][param={}][begin={}]", paramMap.toString(), System.currentTimeMillis());
|
|
|
// 1、构造查询参数
|
|
|
SearchParam searchParam = searchParamHelper.buildDefault(paramMap);
|
|
|
|
|
|
// 2、构造聚合参数
|
|
|
List<AbstractAggregationBuilder<?>> list = new ArrayList<AbstractAggregationBuilder<?>>();
|
|
|
list.add(AggregationBuilders.terms("brandAlifAgg").field(ProductIndexEsField.brandAlif).size(1000).order(Terms.Order.term(true))
|
|
|
.subAggregation(AggregationBuilders.terms("brandAgg").field(ProductIndexEsField.brandId).size(1000)));
|
|
|
searchParam.setAggregationBuilders(list);
|
|
|
searchParam.setSize(0);
|
|
|
|
|
|
// 3、brand数据量比较大,走本地缓存
|
|
|
final String indexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
|
|
|
JSONObject cacheObject = searchCacheService.getJSONObjectFromCache(this.searchCache, indexName, searchParam);
|
|
|
if (cacheObject != null) {
|
|
|
SearchCacheMatchLogger.doSearchCacheMatchLog("/productindex/brands.json", paramMap);
|
|
|
return new SearchApiResult().setData(cacheObject);
|
|
|
}
|
|
|
// 4、查询ES
|
|
|
SearchResult searchResult = searchCommonService.doSearch(indexName, searchParam);
|
|
|
SearchApiResult searchApiResult = new SearchApiResult().setMessage("brand List.");
|
|
|
if (searchResult == null || searchResult.getAggMaps() == null) {
|
|
|
return searchApiResult.setCode(500);
|
|
|
}
|
|
|
Map<String, Aggregation> aggregationResult = searchResult.getAggMaps();
|
|
|
if (!aggregationResult.containsKey("brandAlifAgg")) {
|
|
|
return searchApiResult;
|
|
|
}
|
|
|
// 5、构造返回结果并加入缓存
|
|
|
JSONObject result = new JSONObject();
|
|
|
result.put("brands", makeBrandResponse(((MultiBucketsAggregation) aggregationResult.get("brandAlifAgg"))));
|
|
|
searchCacheService.addJSONObjectToCache(this.searchCache, indexName, searchParam, result);
|
|
|
return searchApiResult.setData(result);
|
|
|
} catch (Exception e) {
|
|
|
publisher.publishEvent(new SearchEvent(EventReportEnum.SEARCHCONTROLLER_BRANDS.getEventName(), EventReportEnum.SEARCHCONTROLLER_BRANDS.getFunctionName(),
|
|
|
EventReportEnum.SEARCHCONTROLLER_BRANDS.getMoudleName(), "exception", IgnoreSomeException.filterSomeException(e), null));
|
|
|
return SearchApiResultUtils.errorSearchApiResult("brands", paramMap, e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 制作品牌报文
|
|
|
*/
|
|
|
private Map<String, JSONArray> makeBrandResponse(MultiBucketsAggregation aggregation) {
|
|
|
// 1)获取每个brandAlif对应的brandIds
|
|
|
Map<String, List<String>> brandAlif2BrandIds = new LinkedHashMap<String, List<String>>();
|
|
|
for (Bucket brandAlifBucket : aggregation.getBuckets()) {
|
|
|
String brandAlif = brandAlifBucket.getKeyAsString();
|
|
|
List<String> brandIds = new ArrayList<String>();
|
|
|
MultiBucketsAggregation brandAggs = (MultiBucketsAggregation) brandAlifBucket.getAggregations().asMap().get("brandAgg");
|
|
|
for (Bucket brandIdBucket : brandAggs.getBuckets()) {
|
|
|
brandIds.addAll(Arrays.asList(brandIdBucket.getKeyAsString().split(",")));
|
|
|
}
|
|
|
brandAlif2BrandIds.put(brandAlif, brandIds);
|
|
|
}
|
|
|
// 2)获取所有的品牌id
|
|
|
List<String> brandIds = new ArrayList<String>();
|
|
|
for (Map.Entry<String, List<String>> entry : brandAlif2BrandIds.entrySet()) {
|
|
|
brandIds.addAll(entry.getValue());
|
|
|
}
|
|
|
// 3)获取所有的品牌数据
|
|
|
List<Map<String, Object>> brandList = brandIndexBaseService.getBrandListByIds(brandIds);
|
|
|
Map<String, Map<String, Object>> brandIdMap = new HashMap<String, Map<String, Object>>();
|
|
|
for (Map<String, Object> brand : brandList) {
|
|
|
brandIdMap.put(brand.getOrDefault("id", 0).toString(), brand);
|
|
|
}
|
|
|
// 4)构造真正的数据
|
|
|
Map<String, JSONArray> result = new LinkedHashMap<String, JSONArray>();
|
|
|
for (Map.Entry<String, List<String>> entry : brandAlif2BrandIds.entrySet()) {
|
|
|
String brandAlif = entry.getKey();
|
|
|
JSONArray brands = new JSONArray();
|
|
|
for (String brandId : entry.getValue()) {
|
|
|
Map<String, Object> brand = brandIdMap.get(brandId);
|
|
|
if (brand != null) {
|
|
|
brands.add(brand);
|
|
|
}
|
|
|
}
|
|
|
result.put(brandAlif, brands);
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public SearchApiResult brandList(Map<String, String> paramMap) {
|
|
|
try {
|
|
|
logger.info("[func=brandList][param={}][begin={}]", paramMap.toString(), System.currentTimeMillis());
|
|
|
|
|
|
// 1)构建参数
|
|
|
SearchParam searchParam = new SearchParam();
|
|
|
searchParam.setSize(10000);
|
|
|
searchParam.setQuery(QueryBuilders.matchAllQuery());
|
|
|
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
|
|
|
boolQueryBuilder.mustNot(QueryBuilders.termQuery("isGlobal", "Y"));
|
|
|
searchParam.setFiter(boolQueryBuilder);
|
|
|
|
|
|
// 2、brand数据量比较大,走本地缓存。guavacache缓存中获取result,没有到es中获取
|
|
|
final String brandIndexName = ISearchConstants.INDEX_NAME_BRAND;
|
|
|
JSONArray cacheJSONArray = searchCacheService.getJSONArrayFromCache(this.searchCache, brandIndexName, searchParam);
|
|
|
if (cacheJSONArray != null) {
|
|
|
SearchCacheMatchLogger.doSearchCacheMatchLog("/brand/list.json", paramMap);
|
|
|
return new SearchApiResult().setData(cacheJSONArray);
|
|
|
}
|
|
|
// 3、执行搜索
|
|
|
SearchResult searchResult = searchCommonService.doSearch(brandIndexName, searchParam);
|
|
|
if (searchResult == null || searchResult.getResultList().isEmpty()) {
|
|
|
return new SearchApiResult().setData(400).setMessage("empty result");
|
|
|
}
|
|
|
// 4、构建返回结果并加入缓存
|
|
|
List<Map<String, Object>> result = searchResult.getResultList();
|
|
|
JSONArray jsonArray = new JSONArray();
|
|
|
for (Map<String, Object> map : result) {
|
|
|
Map<String, Object> brandInfo = brandIndexBaseService.getBrandMap(map);
|
|
|
jsonArray.add(brandInfo);
|
|
|
}
|
|
|
searchCacheService.addJSONArrayToCache(this.searchCache, brandIndexName, searchParam, jsonArray);
|
|
|
return new SearchApiResult().setMessage("brands info").setData(jsonArray);
|
|
|
} catch (Exception e) {
|
|
|
publisher.publishEvent(new SearchEvent(EventReportEnum.BRANDCONTROLLER_BRAND_LIST.getEventName(), EventReportEnum.BRANDCONTROLLER_BRAND_LIST.getFunctionName(),
|
|
|
EventReportEnum.BRANDCONTROLLER_BRAND_LIST.getMoudleName(), "exception", IgnoreSomeException.filterSomeException(e), null));
|
|
|
return SearchApiResultUtils.errorSearchApiResult("brandList", paramMap, e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public SearchApiResult groupBrands(Map<String, String> paramMap) {
|
|
|
try {
|
|
|
logger.info("[func=groupBrands][param={}][begin={}]", paramMap.toString(), System.currentTimeMillis());
|
|
|
// 1、参数检验
|
|
|
if (!paramMap.containsKey("brand")) {
|
|
|
return new SearchApiResult().setCode(400).setMessage("没有 brand参数 ");
|
|
|
}
|
|
|
// 2、获取聚合条件
|
|
|
int topHitCount = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum"));
|
|
|
// 3、是否需要个性化
|
|
|
String topHitOrder = "heatValue:desc";
|
|
|
if (topHitCount > 0 && searchCommonHelper.isNeedPersonalSearch(paramMap)) {
|
|
|
topHitOrder = "_score:desc";
|
|
|
}
|
|
|
|
|
|
// 4、构建searchParam
|
|
|
SearchParam searchParam = searchParamHelper.buildWithPersional(paramMap, topHitCount > 0 ? true : false);
|
|
|
IAggregation groupBrandAgg = aggregationFactoryService.getGroupBrandAggregation(topHitOrder, topHitCount);
|
|
|
searchParam.setAggregationBuilders(Arrays.asList(groupBrandAgg.getBuilder()));
|
|
|
searchParam.setSize(0);
|
|
|
searchParam.setOffset(topHitCount * 2);// justForCache
|
|
|
|
|
|
// 5、从缓存中获取
|
|
|
final String productIndexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
|
|
|
JSONObject groupBrandResult = searchCacheService.getJSONObjectFromCache(this.searchCache, productIndexName, searchParam);
|
|
|
if (groupBrandResult != null) {
|
|
|
SearchCacheMatchLogger.doSearchCacheMatchLog("/productindex/groupBrands.json", paramMap);
|
|
|
return new SearchApiResult().setData(groupBrandResult);
|
|
|
}
|
|
|
// 6、从ES中获取
|
|
|
groupBrandResult = aggregationService.getAggNameAndResponseWithTotal(groupBrandAgg, searchParam);
|
|
|
if (groupBrandResult == null) {
|
|
|
return new SearchApiResult().setData(500).setMessage("exception");
|
|
|
}
|
|
|
// 7、生成结果并且加入缓存
|
|
|
JSONObject realResult = new JSONObject();
|
|
|
realResult.put("total", groupBrandResult.getOrDefault("total", 0));
|
|
|
realResult.put("brands", groupBrandResult.getOrDefault(groupBrandAgg.aggName(), new JSONObject()));
|
|
|
searchCacheService.addJSONObjectToCache(this.searchCache, productIndexName, searchParam, realResult);
|
|
|
return new SearchApiResult().setData(realResult).setMessage("groupBrands new List.");
|
|
|
} catch (Exception e) {
|
|
|
publisher.publishEvent(new SearchEvent(EventReportEnum.SEARCHCONTROLLER_GROUP_BRANDS.getEventName(), EventReportEnum.SEARCHCONTROLLER_GROUP_BRANDS.getFunctionName(),
|
|
|
EventReportEnum.SEARCHCONTROLLER_GROUP_BRANDS.getMoudleName(), "exception", IgnoreSomeException.filterSomeException(e), null));
|
|
|
return SearchApiResultUtils.errorSearchApiResult("groupBrands", paramMap, e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
@Override
|
|
|
public SearchApiResult aggProductsByBrandIdInParam(Map<String, String> paramMap) {
|
|
|
try {
|
|
|
logger.info("[func=aggProductsByBrandIdInParam][param={}][begin={}]", paramMap.toString(), System.currentTimeMillis());
|
|
|
|
|
|
// 1、参数校验
|
|
|
List<Integer> brandIds = ConvertUtils.stringToIntList(MapUtils.getString(paramMap, "brand", ""), ",");
|
|
|
if (brandIds == null || brandIds.isEmpty()) {
|
|
|
return new SearchApiResult().setCode(400).setMessage("没有 brand参数 ");
|
|
|
}
|
|
|
|
|
|
// 2、处理排序方式
|
|
|
String sortField = searchSortHelper.getLegalOrder(paramMap);
|
|
|
if (StringUtils.isBlank(sortField)) {
|
|
|
sortField = "shelveTime:desc";
|
|
|
}
|
|
|
String[] sortParts = sortField.split(":");
|
|
|
String realSortField = sortParts[0];
|
|
|
String realSortType = sortParts[1];
|
|
|
SortOrder realSortOrder = SortOrder.ASC.toString().equalsIgnoreCase(realSortType) ? SortOrder.ASC : SortOrder.DESC;
|
|
|
|
|
|
// 3、构造查询参数
|
|
|
SearchParam searchParam = searchParamHelper.buildDefault(paramMap);
|
|
|
|
|
|
// 4、构造聚合参数
|
|
|
List<AbstractAggregationBuilder<?>> list = new ArrayList<AbstractAggregationBuilder<?>>();
|
|
|
// 获取取商品的数量
|
|
|
// 构造品牌聚合参数[最多取100个品牌的一个商品,取回来之后再做排序]
|
|
|
TermsAggregationBuilder brandAggregationBuilder = AggregationBuilders.terms("brandAgg").field(ProductIndexEsField.brandId).size(100);
|
|
|
// 再构造对应HIT到的商品的聚合参数
|
|
|
FieldSortBuilder productSortBuilder = SortBuilders.fieldSort(realSortField).order(realSortOrder);
|
|
|
TopHitsAggregationBuilder productTopHitsBuilder = AggregationBuilders.topHits("product").sort(productSortBuilder).size(1);
|
|
|
brandAggregationBuilder.subAggregation(productTopHitsBuilder);
|
|
|
list.add(brandAggregationBuilder);
|
|
|
searchParam.setAggregationBuilders(list);
|
|
|
|
|
|
// 5、设置报文缓存参数
|
|
|
int limit = StringUtils.isBlank(paramMap.get("limit")) ? 1 : Integer.parseInt(paramMap.get("limit"));
|
|
|
searchParam.setOffset(limit);// 这个参数只是用来生成报文以做缓存,不要动
|
|
|
searchParam.setSize(0);
|
|
|
|
|
|
// 6、构造返回结果
|
|
|
SearchApiResult searchApiResult = new SearchApiResult().setMessage("new product List.");
|
|
|
|
|
|
// 7、先从缓存中获取,如果能取到,则直接返回
|
|
|
JSONArray jsonArray = searchCacheService.getJSONArrayFromCache(this.searchCache, ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
|
|
|
if (jsonArray != null) {
|
|
|
SearchCacheMatchLogger.doSearchCacheMatchLog("/new_product.json", paramMap);
|
|
|
return searchApiResult.setData(jsonArray);
|
|
|
}
|
|
|
|
|
|
// 8、查询ES数据
|
|
|
final String indexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
|
|
|
SearchResult searchResult = searchCommonService.doSearch(indexName, searchParam);
|
|
|
if (searchResult == null || searchResult.getAggMaps() == null) {
|
|
|
return searchApiResult.setData("");
|
|
|
}
|
|
|
Map<String, Aggregation> aggMaps = searchResult.getAggMaps();
|
|
|
if (!aggMaps.containsKey("brandAgg")) {
|
|
|
return searchApiResult.setData("");
|
|
|
}
|
|
|
|
|
|
// 9、构造返回数据并加入缓存
|
|
|
jsonArray = getNewBrandResponseMap(((MultiBucketsAggregation) aggMaps.get("brandAgg")), limit, brandIds, realSortField, realSortOrder);
|
|
|
searchCacheService.addJSONArrayToCache(this.searchCache, indexName, searchParam, jsonArray);
|
|
|
return searchApiResult.setData(jsonArray);
|
|
|
} catch (Exception e) {
|
|
|
publisher.publishEvent(new SearchEvent(EventReportEnum.SEARCHCONTROLLER_NEW_PRODUCT.getEventName(), EventReportEnum.SEARCHCONTROLLER_NEW_PRODUCT.getFunctionName(),
|
|
|
EventReportEnum.SEARCHCONTROLLER_NEW_PRODUCT.getMoudleName(), "exception", IgnoreSomeException.filterSomeException(e), null));
|
|
|
return SearchApiResultUtils.errorSearchApiResult("searchNewestProductWithDiffBrand", paramMap, e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private JSONArray getNewBrandResponseMap(MultiBucketsAggregation aggregation, int size, List<Integer> brandIds, final String realSortField, final SortOrder realSortOrder) {
|
|
|
// 1、首先获取全部品牌的商品列表
|
|
|
Iterator<? extends Bucket> itAgg = aggregation.getBuckets().iterator();
|
|
|
Map<String, Map<String, Object>> brandId2ProductMap = new HashMap<>();
|
|
|
while (itAgg.hasNext()) {
|
|
|
Bucket lt = itAgg.next();
|
|
|
if (lt.getAggregations().getAsMap().containsKey("product")) {
|
|
|
TopHits topHits = lt.getAggregations().get("product");
|
|
|
if (topHits != null) {
|
|
|
SearchHits hits = topHits.getHits();
|
|
|
for (SearchHit hit : hits.getHits()) {
|
|
|
brandId2ProductMap.put(lt.getKeyAsString(), hit.getSource());
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
if (brandId2ProductMap == null || brandId2ProductMap.isEmpty()) {
|
|
|
return new JSONArray();
|
|
|
}
|
|
|
|
|
|
// 2、按品牌id获取商品列表
|
|
|
List<Map<String, Object>> productEsSourceList = new ArrayList<>();
|
|
|
for (Integer brandId : brandIds) {
|
|
|
if (brandId2ProductMap.containsKey(brandId.toString())) {
|
|
|
productEsSourceList.add(brandId2ProductMap.get(brandId.toString()));
|
|
|
}
|
|
|
if (productEsSourceList.size() >= size) {
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 3、对商品列表进行排序
|
|
|
Collections.sort(productEsSourceList, new Comparator<Map<String, Object>>() {
|
|
|
public int compare(Map<String, Object> o1, Map<String, Object> o2) {
|
|
|
Double map1value = MapUtils.getDoubleValue(o1, realSortField);
|
|
|
Double map2value = MapUtils.getDoubleValue(o2, realSortField);
|
|
|
if ("desc".equalsIgnoreCase(realSortOrder.name())) {
|
|
|
return map2value.compareTo(map1value);
|
|
|
} else {
|
|
|
return map1value.compareTo(map2value);
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
|
|
|
//4、生成返回对象
|
|
|
List<Map<String, Object>> productReturnInfoList = productListHelper.buildReturnInfoByEsSourceList(productEsSourceList);
|
|
|
JSONArray jsonArray = new JSONArray();
|
|
|
jsonArray.addAll(productReturnInfoList);
|
|
|
return jsonArray;
|
|
|
}
|
|
|
} |
...
|
...
|
|