|
|
package com.yoho.search.service.scene.shopbrand;
|
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.yoho.error.event.SearchEvent;
|
...
|
...
|
@@ -13,40 +11,27 @@ import com.yoho.search.base.utils.ProductIndexEsField; |
|
|
import com.yoho.search.cache.beans.AbstractCacheAbleService;
|
|
|
import com.yoho.search.cache.log.SearchCacheMatchLogger;
|
|
|
import com.yoho.search.cache.model.SearchCache;
|
|
|
import com.yoho.search.common.SearchCommonService;
|
|
|
import com.yoho.search.common.SearchRequestParams;
|
|
|
import com.yoho.search.common.utils.SearchApiResultUtils;
|
|
|
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.models.SearchApiResult;
|
|
|
import com.yoho.search.service.aggregations.AggregationsService;
|
|
|
import com.yoho.search.service.aggregations.impls.AggregationFactory;
|
|
|
import com.yoho.search.common.SearchCommonService;
|
|
|
import com.yoho.search.common.SearchRequestParams;
|
|
|
import com.yoho.search.service.index.BrandIndexBaseService;
|
|
|
import com.yoho.search.service.helper.ProductListHelper;
|
|
|
import com.yoho.search.service.helper.SearchCommonHelper;
|
|
|
import com.yoho.search.service.helper.SearchParamHelper;
|
|
|
import com.yoho.search.service.helper.SearchSortHelper;
|
|
|
import org.apache.commons.collections.MapUtils;
|
|
|
import com.yoho.search.service.index.BrandIndexBaseService;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.elasticsearch.index.query.BoolQueryBuilder;
|
|
|
|
|
|
import com.yoho.search.models.SearchApiResult;
|
|
|
import org.elasticsearch.index.query.MatchQueryBuilder;
|
|
|
import org.elasticsearch.index.query.QueryBuilders;
|
|
|
import org.elasticsearch.search.SearchHit;
|
|
|
import org.elasticsearch.search.SearchHits;
|
|
|
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
|
|
|
import org.elasticsearch.search.aggregations.Aggregation;
|
|
|
import org.elasticsearch.search.aggregations.AggregationBuilders;
|
|
|
import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation;
|
|
|
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
|
|
|
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
|
|
|
import org.elasticsearch.search.aggregations.metrics.tophits.TopHits;
|
|
|
import org.elasticsearch.search.aggregations.metrics.tophits.TopHitsAggregationBuilder;
|
|
|
import org.elasticsearch.search.sort.FieldSortBuilder;
|
|
|
import org.elasticsearch.search.sort.SortBuilders;
|
|
|
import org.elasticsearch.search.sort.SortOrder;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
...
|
...
|
@@ -54,421 +39,251 @@ import org.springframework.context.ApplicationEventPublisher; |
|
|
import org.springframework.context.ApplicationEventPublisherAware;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
|
|
|
|
|
|
@Service
|
|
|
public class BrandService extends AbstractCacheAbleService implements ApplicationEventPublisherAware {
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(BrandService.class);
|
|
|
|
|
|
@Autowired
|
|
|
private SearchSortHelper searchSortHelper;
|
|
|
@Autowired
|
|
|
private SearchParamHelper searchParamHelper;
|
|
|
@Autowired
|
|
|
private AggregationsService aggregationsService;
|
|
|
@Autowired
|
|
|
private SearchCommonService searchCommonService;
|
|
|
@Autowired
|
|
|
private BrandIndexBaseService brandIndexBaseService;
|
|
|
@Autowired
|
|
|
private AggregationFactory aggregationFactory;
|
|
|
@Autowired
|
|
|
private ProductListHelper productListHelper;
|
|
|
|
|
|
@Override
|
|
|
public SearchCache getSearchCache() {
|
|
|
return searchCacheFactory.getBrandRelatedCache();
|
|
|
}
|
|
|
|
|
|
private ApplicationEventPublisher publisher;
|
|
|
|
|
|
@Override
|
|
|
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
|
|
|
this.publisher = applicationEventPublisher;
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
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 = aggregationFactory.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 = aggregationsService.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);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
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 (MultiBucketsAggregation.Bucket brandAlifBucket : aggregation.getBuckets()) {
|
|
|
String brandAlif = brandAlifBucket.getKeyAsString();
|
|
|
List<String> brandIds = new ArrayList<String>();
|
|
|
MultiBucketsAggregation brandAggs = (MultiBucketsAggregation) brandAlifBucket.getAggregations().asMap().get("brandAgg");
|
|
|
for (MultiBucketsAggregation.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;
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@SearchCacheAble(cacheInMinute = 30, cacheName = "GROUP_BRANDS", excludeParams = {"page", "order", "uid", "udid"})
|
|
|
public SearchApiResult groupBrands(Map<String, String> paramMap) {
|
|
|
try {
|
|
|
// 0、参数检验
|
|
|
if (!paramMap.containsKey(SearchRequestParams.PARAM_SEARCH_BRAND)) {
|
|
|
return new SearchApiResult().setCode(400).setMessage("没有 brand参数 ");
|
|
|
}
|
|
|
// 1、获取topHitCount
|
|
|
int topHitCount = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum"));
|
|
|
// 2、获取聚合的排序规则
|
|
|
String topHitOrder = "heatValue:desc";
|
|
|
|
|
|
// 3、构建searchParam
|
|
|
SearchParam searchParam = searchParamHelper.buildWithPersional(paramMap, false);
|
|
|
IAggregation groupBrandAgg = aggregationFactory.getGroupBrandAggregation(topHitOrder, topHitCount);
|
|
|
searchParam.setAggregationBuilders(Arrays.asList(groupBrandAgg.getBuilder()));
|
|
|
searchParam.setOffset(0);
|
|
|
searchParam.setSize(0);
|
|
|
|
|
|
// 4、从ES中获取
|
|
|
JSONObject groupBrandResult = aggregationsService.getAggNameAndResponseWithTotal(groupBrandAgg, searchParam);
|
|
|
if (groupBrandResult == null) {
|
|
|
return new SearchApiResult().setData(500).setMessage("exception");
|
|
|
}
|
|
|
// 5、返回生成结果
|
|
|
JSONObject realResult = new JSONObject();
|
|
|
realResult.put("total", groupBrandResult.getOrDefault("total", 0));
|
|
|
realResult.put("brands", groupBrandResult.getOrDefault(groupBrandAgg.aggName(), new JSONObject()));
|
|
|
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);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
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);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
public SearchApiResult searchBrand(Map<String, String> paramMap) {
|
|
|
// 1、参数校验
|
|
|
if (StringUtils.isBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_QUERY))) {
|
|
|
return new SearchApiResult().setCode(400).setMessage("请传query参数");
|
|
|
}
|
|
|
// 1)构建参数
|
|
|
SearchParam searchParam = new SearchParam();
|
|
|
searchParam.setSize(15);
|
|
|
|
|
|
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
|
|
|
MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("brandName", paramMap.get(SearchRequestParams.PARAM_SEARCH_QUERY));
|
|
|
matchQueryBuilder.boost(3);
|
|
|
boolQueryBuilder.should(matchQueryBuilder);
|
|
|
boolQueryBuilder.should(QueryBuilders.matchPhrasePrefixQuery("brandName", paramMap.get(SearchRequestParams.PARAM_SEARCH_QUERY)));
|
|
|
boolQueryBuilder.minimumShouldMatch(1);
|
|
|
searchParam.setQuery(boolQueryBuilder);
|
|
|
|
|
|
// 2、brand数据量比较大,走本地缓存。guavacache缓存中获取result,没有到es中获取
|
|
|
final String brandIndexName = ISearchConstants.INDEX_NAME_BRAND;
|
|
|
// 3、执行搜索
|
|
|
SearchResult searchResult = searchCommonService.doSearch(brandIndexName, searchParam);
|
|
|
if (searchResult == null || searchResult.getResultList().isEmpty()) {
|
|
|
return new SearchApiResult().setCode(400).setMessage("empty result");
|
|
|
}
|
|
|
Map<String, List> data = new HashMap<>();
|
|
|
data.put("brands", searchResult.getResultList());
|
|
|
return new SearchApiResult().setCode(200).setData(data);
|
|
|
}
|
|
|
|
|
|
|
|
|
private JSONArray getNewBrandResponseMap(MultiBucketsAggregation aggregation, int size, List<Integer> brandIds, final String realSortField, final SortOrder realSortOrder) {
|
|
|
// 1、首先获取全部品牌的商品列表
|
|
|
Iterator<? extends MultiBucketsAggregation.Bucket> itAgg = aggregation.getBuckets().iterator();
|
|
|
Map<String, Map<String, Object>> brandId2ProductMap = new HashMap<>();
|
|
|
while (itAgg.hasNext()) {
|
|
|
MultiBucketsAggregation.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;
|
|
|
}
|
|
|
|
|
|
public SearchApiResult queryBrandsByIds(Map<String, String> paramMap) {
|
|
|
if (StringUtils.isBlank(paramMap.get(SearchRequestParams.BRANDINDEX_ID))) {
|
|
|
return new SearchApiResult().setCode(400).setMessage("请传brandIds参数");
|
|
|
}
|
|
|
|
|
|
List<Integer> values = ConvertUtils.stringToIntList(paramMap.get(SearchRequestParams.BRANDINDEX_ID), ",");
|
|
|
if(values==null || values.isEmpty()){
|
|
|
return new SearchApiResult().setCode(400).setMessage("empty result");
|
|
|
}
|
|
|
BoolQueryBuilder boolFilter = QueryBuilders.boolQuery();
|
|
|
boolFilter.must(QueryBuilders.termsQuery("id", values));
|
|
|
|
|
|
SearchParam searchParam = new SearchParam();
|
|
|
searchParam.setFiter(boolFilter);
|
|
|
searchParam.setQuery(matchAllQuery());
|
|
|
searchParam.setSize(values.size());
|
|
|
final String brandIndexName = ISearchConstants.INDEX_NAME_BRAND;
|
|
|
SearchResult searchResult = searchCommonService.doSearch(brandIndexName, searchParam);
|
|
|
if (searchResult == null || searchResult.getResultList().isEmpty()) {
|
|
|
return new SearchApiResult().setCode(400).setMessage("empty result");
|
|
|
}
|
|
|
Map<String, List> data = new HashMap<>();
|
|
|
data.put("brands", searchResult.getResultList());
|
|
|
return new SearchApiResult().setCode(200).setData(data);
|
|
|
}
|
|
|
public class BrandListService extends AbstractCacheAbleService implements ApplicationEventPublisherAware {
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(BrandListService.class);
|
|
|
|
|
|
@Autowired
|
|
|
private SearchParamHelper searchParamHelper;
|
|
|
@Autowired
|
|
|
private AggregationsService aggregationsService;
|
|
|
@Autowired
|
|
|
private SearchCommonService searchCommonService;
|
|
|
@Autowired
|
|
|
private BrandIndexBaseService brandIndexBaseService;
|
|
|
@Autowired
|
|
|
private AggregationFactory aggregationFactory;
|
|
|
|
|
|
@Override
|
|
|
public SearchCache getSearchCache() {
|
|
|
return searchCacheFactory.getBrandRelatedCache();
|
|
|
}
|
|
|
|
|
|
private ApplicationEventPublisher publisher;
|
|
|
|
|
|
@Override
|
|
|
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
|
|
|
this.publisher = applicationEventPublisher;
|
|
|
}
|
|
|
|
|
|
public SearchApiResult aggAllBrandList(Map<String, String> paramMap) {
|
|
|
try {
|
|
|
logger.info("[func=aggBrand][param={}][begin={}]", paramMap.toString(), System.currentTimeMillis());
|
|
|
return this.aggAllBrandList(paramMap, null);
|
|
|
} catch (Exception e) {
|
|
|
return SearchApiResultUtils.errorSearchApiResult("aggBrand", paramMap, e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public SearchApiResult aggAllBrandList(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 = aggregationFactory.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 = aggregationsService.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);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@SearchCacheAble(cacheInMinute = 30, cacheName = "BRAND_LIST", excludeParams = {"page", "order", "uid", "udid"})
|
|
|
public SearchApiResult brandList(Map<String, String> paramMap) {
|
|
|
try {
|
|
|
// 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、执行搜索
|
|
|
final String brandIndexName = ISearchConstants.INDEX_NAME_BRAND;
|
|
|
SearchResult searchResult = searchCommonService.doSearch(brandIndexName, searchParam);
|
|
|
if (searchResult == null || searchResult.getResultList().isEmpty()) {
|
|
|
return new SearchApiResult().setData(400).setMessage("empty result");
|
|
|
}
|
|
|
// 3、构建返回结果并加入缓存
|
|
|
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);
|
|
|
}
|
|
|
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);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@SearchCacheAble(cacheInMinute = 30, cacheName = "SEARCH_BRANDS", excludeParams = {"page", "order", "uid", "udid"})
|
|
|
public SearchApiResult searchBrand(Map<String, String> paramMap) {
|
|
|
try {
|
|
|
// 1、参数校验
|
|
|
if (StringUtils.isBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_QUERY))) {
|
|
|
return new SearchApiResult().setCode(400).setMessage("请传query参数");
|
|
|
}
|
|
|
// 2、构建参数
|
|
|
SearchParam searchParam = new SearchParam();
|
|
|
searchParam.setSize(15);
|
|
|
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
|
|
|
MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("brandName", paramMap.get(SearchRequestParams.PARAM_SEARCH_QUERY));
|
|
|
matchQueryBuilder.boost(3);
|
|
|
boolQueryBuilder.should(matchQueryBuilder);
|
|
|
boolQueryBuilder.should(QueryBuilders.matchPhrasePrefixQuery("brandName", paramMap.get(SearchRequestParams.PARAM_SEARCH_QUERY)));
|
|
|
boolQueryBuilder.minimumShouldMatch(1);
|
|
|
searchParam.setQuery(boolQueryBuilder);
|
|
|
|
|
|
final String brandIndexName = ISearchConstants.INDEX_NAME_BRAND;
|
|
|
// 3、执行搜索
|
|
|
SearchResult searchResult = searchCommonService.doSearch(brandIndexName, searchParam);
|
|
|
if (searchResult == null || searchResult.getResultList().isEmpty()) {
|
|
|
return new SearchApiResult().setCode(400).setMessage("empty result");
|
|
|
}
|
|
|
Map<String, List> data = new HashMap<>();
|
|
|
data.put("brands", searchResult.getResultList());
|
|
|
return new SearchApiResult().setCode(200).setData(data);
|
|
|
} catch (Exception e) {
|
|
|
logger.error(e.getMessage(), e);
|
|
|
return SearchApiResultUtils.defaultErrorSearchApiResult(e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@SearchCacheAble(cacheInMinute = 30, cacheName = "QUERY_BRAND_BY_IDS", excludeParams = {"page", "order", "uid", "udid"})
|
|
|
public SearchApiResult queryBrandsByIds(Map<String, String> paramMap) {
|
|
|
try {
|
|
|
if (StringUtils.isBlank(paramMap.get(SearchRequestParams.BRANDINDEX_ID))) {
|
|
|
return new SearchApiResult().setCode(400).setMessage("请传brandIds参数");
|
|
|
}
|
|
|
List<Integer> values = ConvertUtils.stringToIntList(paramMap.get(SearchRequestParams.BRANDINDEX_ID), ",");
|
|
|
if (values == null || values.isEmpty()) {
|
|
|
return new SearchApiResult().setCode(400).setMessage("empty result");
|
|
|
}
|
|
|
BoolQueryBuilder boolFilter = QueryBuilders.boolQuery();
|
|
|
boolFilter.must(QueryBuilders.termsQuery("id", values));
|
|
|
|
|
|
SearchParam searchParam = new SearchParam();
|
|
|
searchParam.setFiter(boolFilter);
|
|
|
searchParam.setQuery(matchAllQuery());
|
|
|
searchParam.setSize(values.size());
|
|
|
final String brandIndexName = ISearchConstants.INDEX_NAME_BRAND;
|
|
|
SearchResult searchResult = searchCommonService.doSearch(brandIndexName, searchParam);
|
|
|
if (searchResult == null || searchResult.getResultList().isEmpty()) {
|
|
|
return new SearchApiResult().setCode(400).setMessage("empty result");
|
|
|
}
|
|
|
Map<String, List> data = new HashMap<>();
|
|
|
data.put("brands", searchResult.getResultList());
|
|
|
return new SearchApiResult().setCode(200).setData(data);
|
|
|
} catch (Exception e) {
|
|
|
logger.error(e.getMessage(), e);
|
|
|
return SearchApiResultUtils.defaultErrorSearchApiResult(e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@SearchCacheAble(cacheInMinute = 30, cacheName = "AGG_BRAND_LIST_WITH_ALIF", excludeParams = {"page", "order", "uid", "udid"})
|
|
|
public SearchApiResult aggAllBrandListWithAlif(Map<String, String> paramMap) {
|
|
|
try {
|
|
|
// 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、执行查询
|
|
|
final String indexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
|
|
|
SearchResult searchResult = searchCommonService.doSearch(indexName, searchParam);
|
|
|
SearchApiResult searchApiResult = new SearchApiResult().setMessage("brand List.");
|
|
|
if (searchResult == null || searchResult.getAggMaps() == null) {
|
|
|
return searchApiResult.setCode(500);
|
|
|
}
|
|
|
|
|
|
//4、构造查询结果
|
|
|
Map<String, Aggregation> aggregationResult = searchResult.getAggMaps();
|
|
|
if (!aggregationResult.containsKey("brandAlifAgg")) {
|
|
|
return searchApiResult;
|
|
|
}
|
|
|
JSONObject result = new JSONObject();
|
|
|
result.put("brands", makeAllBrandListWithAlifResponse(((MultiBucketsAggregation) aggregationResult.get("brandAlifAgg"))));
|
|
|
return searchApiResult.setData(result);
|
|
|
} catch (Exception e) {
|
|
|
logger.error(e.getMessage(), e);
|
|
|
return SearchApiResultUtils.defaultErrorSearchApiResult(e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 制作品牌报文
|
|
|
*/
|
|
|
private Map<String, JSONArray> makeAllBrandListWithAlifResponse(MultiBucketsAggregation aggregation) {
|
|
|
// 1)获取每个brandAlif对应的brandIds
|
|
|
Map<String, List<String>> brandAlif2BrandIds = new LinkedHashMap<String, List<String>>();
|
|
|
for (MultiBucketsAggregation.Bucket brandAlifBucket : aggregation.getBuckets()) {
|
|
|
String brandAlif = brandAlifBucket.getKeyAsString();
|
|
|
List<String> brandIds = new ArrayList<String>();
|
|
|
MultiBucketsAggregation brandAggs = (MultiBucketsAggregation) brandAlifBucket.getAggregations().asMap().get("brandAgg");
|
|
|
for (MultiBucketsAggregation.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;
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|