Authored by 胡古飞

整理品牌相关的接口

package com.yoho.search.service.aggregations.impls;
import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.core.es.agg.AbstractAggregation;
import com.yoho.search.service.service.SearchCommonService;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
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.MultiBucketsAggregation.Bucket;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import com.yoho.search.core.es.agg.AbstractAggregation;
import com.yoho.search.service.service.base.BrandIndexBaseService;
public class BrandAggregation extends AbstractAggregation {
private SearchCommonService searchCommonService;
private static final Logger logger = LoggerFactory.getLogger(BrandAggregation.class);
private BrandIndexBaseService brandIndexBaseService;
private Map<String, String> paramMap;
private int brandCount;
BrandAggregation(SearchCommonService searchCommonService, Map<String, String> paramMap) {
this.searchCommonService = searchCommonService;
BrandAggregation(BrandIndexBaseService brandIndexBaseService, Map<String, String> paramMap) {
this.brandIndexBaseService = brandIndexBaseService;
this.paramMap = paramMap;
this.brandCount=500;
}
BrandAggregation(SearchCommonService searchCommonService, Map<String, String> paramMap,int brandCount){
this.searchCommonService = searchCommonService;
BrandAggregation(BrandIndexBaseService brandIndexBaseService, Map<String, String> paramMap,int brandCount){
this.brandIndexBaseService = brandIndexBaseService;
this.paramMap = paramMap;
this.brandCount = brandCount;
}
... ... @@ -49,24 +53,24 @@ public class BrandAggregation extends AbstractAggregation {
if (aggregation == null) {
return null;
}
Set<String> brandIdSet = new LinkedHashSet<String>();
List<String> brandIds = new ArrayList<String>();
Iterator<? extends Bucket> itSizeAgg = aggregation.getBuckets().iterator();
while (itSizeAgg.hasNext()) {
Bucket ltSize = itSizeAgg.next();
for (String brandId : ltSize.getKeyAsString().split(",")) {
brandIdSet.add(brandId);
brandIds.add(brandId);
}
}
if (paramMap.containsKey("brand") && StringUtils.isNotBlank(paramMap.get("brand"))) {
String[] ids = paramMap.get("brand").split(",");
for (String id : ids) {
brandIdSet.add(id);
brandIds.add(id);
}
}
try {
return searchCommonService.doMultiGet(ISearchConstants.INDEX_NAME_BRAND, brandIdSet, null);
return brandIndexBaseService.getBrandListByIds(brandIds);
} catch (Exception e) {
e.printStackTrace();
logger.error(e.getMessage(),e);
return null;
}
}
... ...
... ... @@ -177,6 +177,22 @@ public class SearchCommonService implements ApplicationEventPublisherAware {
}
return map;
}
private Map<String, Object> getBrandMap(Map<String, Object> esMap) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", esMap.get("id"));
map.put("brand_alif", esMap.get("brandAlif"));
map.put("brand_name_en", esMap.get("brandNameEn"));
map.put("brand_domain", esMap.get("brandDomain"));
map.put("is_hot", esMap.get("isHot"));
map.put("hot_keyword", esMap.get("hotKeyword"));
map.put("brand_name_cn", esMap.get("brandNameCn"));
map.put("brand_ico", esMap.get("brandIco"));
map.put("brand_name", esMap.get("brandName"));
map.put("brand_keyword", esMap.get("brandKeyword"));
map.put("status", esMap.get("status"));
return map;
}
/**
* 通过id获取内容
... ... @@ -227,14 +243,6 @@ public class SearchCommonService implements ApplicationEventPublisherAware {
// 构造返回结果
JSONArray list = new JSONArray();
Map<String, Object> tmpMap;
if (indexName.equals(ISearchConstants.INDEX_NAME_BRAND)) {
for (MultiGetItemResponse item : response.getResponses()) {
if (item.getResponse().isExists()) {
tmpMap = item.getResponse().getSource();
list.add(getBrandMap(tmpMap));
}
}
}
if (indexName.equals(ISearchConstants.INDEX_NAME_SHOPS)) {
for (MultiGetItemResponse item : response.getResponses()) {
if (item.getResponse().isExists()) {
... ... @@ -330,22 +338,6 @@ public class SearchCommonService implements ApplicationEventPublisherAware {
return map;
}
private Map<String, Object> getBrandMap(Map<String, Object> esMap) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", esMap.get("id"));
map.put("brand_alif", esMap.get("brandAlif"));
map.put("brand_name_en", esMap.get("brandNameEn"));
map.put("brand_domain", esMap.get("brandDomain"));
map.put("is_hot", esMap.get("isHot"));
map.put("hot_keyword", esMap.get("hotKeyword"));
map.put("brand_name_cn", esMap.get("brandNameCn"));
map.put("brand_ico", esMap.get("brandIco"));
map.put("brand_name", esMap.get("brandName"));
map.put("brand_keyword", esMap.get("brandKeyword"));
map.put("status", esMap.get("status"));
return map;
}
private Map<String, Object> getShopMap(Map<String, Object> esMap) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("shop_id", esMap.get("shopsId"));
... ...
... ... @@ -20,6 +20,7 @@ import com.alibaba.fastjson.JSONObject;
import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.core.es.model.SearchResult;
import com.yoho.search.service.service.base.BrandIndexBaseService;
import com.yoho.search.service.service.helper.SearchCommonHelper;
import com.yoho.search.service.service.helper.SearchServiceHelper;
import com.yoho.search.service.service.helper.SearchSortHelper;
... ... @@ -43,6 +44,8 @@ public class SearchProductsServiceNew {
private AggregationService aggregationService;
@Autowired
private SearchKeyWordService searchKeyWordService;
@Autowired
private BrandIndexBaseService brandIndexBaseService;
private void setHighlight(final Map<String, String> paramMap, SearchParam searchParam) {
if (StringUtils.isNotBlank(paramMap.get("highlight")) && "1".equals(paramMap.get("highlight")) && StringUtils.isNotBlank(paramMap.get("query"))) {
... ... @@ -274,7 +277,7 @@ public class SearchProductsServiceNew {
if (brandResponse != null) {
filter.put("brand", brandResponse);
} else {
filter.put("brand", searchCommonService.doMultiGet(ISearchConstants.INDEX_NAME_BRAND, paramMap.get("brand").split(","), null));
filter.put("brand", brandIndexBaseService.getBrandListByIds(Arrays.asList(paramMap.get("brand").split(","))));
}
// 7)获取规则的聚合结果[规格不会提前聚合]
... ...
... ... @@ -29,8 +29,8 @@ public class BrandIndexBaseService {
private SearchServiceHelper searchServiceHelper;
private static final String BRAND_INDEX_NAME = ISearchConstants.INDEX_NAME_BRAND;
public Map<String, Object> getBrandMap(Map<String, Object> esMap) {
private Map<String, Object> getBrandMapOld(Map<String, Object> esMap) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", esMap.get("id"));
map.put("brand_alif", esMap.get("brandAlif"));
... ... @@ -43,11 +43,28 @@ public class BrandIndexBaseService {
map.put("brand_name", esMap.get("brandName"));
map.put("brand_keyword", esMap.get("brandKeyword"));
map.put("status", esMap.get("status"));
map.put("yoho_brand_id", esMap.get("yohoBrandId"));
map.put("is_global", esMap.get("isGlobal"));
return map;
}
public Map<String, Object> getBrandMap(Map<String, Object> esMap) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", esMap.get("id"));
map.put("brand_name", esMap.getOrDefault("brandName",""));
map.put("brand_name_en", esMap.getOrDefault("brandNameEn",""));
map.put("brand_name_cn", esMap.getOrDefault("brandNameCn",""));
map.put("brand_ico", esMap.getOrDefault("brandIco",""));
map.put("shelves_brand_time", esMap.getOrDefault("shelvesBrandTime",0));
map.put("is_hot", esMap.getOrDefault("isHot","N"));
map.put("status", esMap.getOrDefault("status",0));
map.put("brand_alif", esMap.getOrDefault("brandAlif",""));
map.put("brand_domain", esMap.getOrDefault("brandDomain",""));
map.put("hot_keyword", esMap.getOrDefault("hotKeyword",""));
map.put("brand_keyword", esMap.getOrDefault("brandKeyword",""));
map.put("yoho_brand_id", esMap.getOrDefault("yohoBrandId",0));
map.put("is_global", esMap.getOrDefault("isGlobal","N"));
return map;
}
public List<Map<String, Object>> getBrandListByIds(List<?> brandIds) {
List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
try {
... ...
... ... @@ -9,7 +9,7 @@ import com.yoho.search.service.vo.SearchApiResult;
public interface IBrandService {
/**
* 获取品牌的聚合结果[使用本地缓存]
* 获取品牌的聚合结果[使用本地缓存][按参数决定是否返回全球购品牌]
*
* @param paramMap
* @return
... ... @@ -17,7 +17,7 @@ public interface IBrandService {
public SearchApiResult aggBrand(Map<String, String> paramMap);
/**
* 获取品牌的聚合结果[使用本地缓存]
* 获取品牌的聚合结果[使用本地缓存][按参数决定是否返回全球购品牌]
*
* @param paramMap
* @return
... ... @@ -25,7 +25,7 @@ public interface IBrandService {
public SearchApiResult aggBrand(Map<String, String> paramMap,BoolQueryBuilder mustFilter);
/**
* 按品牌前缀名获取品牌列表[使用本地缓存]
* 按品牌前缀名获取品牌列表[按参数决定是否返回全球购品牌]
*
* @param paramMap
* @return
... ... @@ -33,7 +33,7 @@ public interface IBrandService {
public SearchApiResult brands(Map<String, String> paramMap);
/**
* 获取商品列表[直接从brand索引里取全部]
* 获取商品列表[直接从brand索引里取全部][不返回全球购品牌]
*
* @param paramMap
* @return
... ... @@ -41,7 +41,7 @@ public interface IBrandService {
public SearchApiResult brandList(Map<String, String> paramMap);
/**
* 按品牌聚合商品
* 按品牌聚合商品[按参数决定是否返回全球购品牌]
* @param paramMap
* @return
*/
... ...
... ... @@ -5,13 +5,11 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
... ... @@ -49,6 +47,7 @@ import com.yoho.search.service.cache.CacheEnum;
import com.yoho.search.service.service.AggregationService;
import com.yoho.search.service.service.SearchCacheService;
import com.yoho.search.service.service.SearchCommonService;
import com.yoho.search.service.service.base.BrandIndexBaseService;
import com.yoho.search.service.service.helper.SearchServiceHelper;
import com.yoho.search.service.service.helper.SearchSortHelper;
import com.yoho.search.service.servicenew.IBrandService;
... ... @@ -71,6 +70,8 @@ public class BrandServiceImpl implements IBrandService, ApplicationEventPublishe
private SearchCommonService searchCommonService;
@Autowired
private SearchCacheService searchCacheService;
@Autowired
private BrandIndexBaseService brandIndexBaseService;
private static final CacheEnum brandCacheEnum = CacheEnum.EHCACHE;
... ... @@ -194,16 +195,15 @@ public class BrandServiceImpl implements IBrandService, ApplicationEventPublishe
brandAlif2BrandIds.put(brandAlif, brandIds);
}
// 2)获取所有的品牌id
Set<String> brandIdSet = new HashSet<String>();
List<String> brandIds = new ArrayList<String>();
for (Map.Entry<String, List<String>> entry : brandAlif2BrandIds.entrySet()) {
brandIdSet.addAll(entry.getValue());
brandIds.addAll(entry.getValue());
}
// 3)获取所有的品牌数据
JSONArray jsonArray = searchCommonService.doMultiGet(ISearchConstants.INDEX_NAME_BRAND, brandIdSet, null);
Map<String, JSONObject> brandIdMap = new HashMap<String, JSONObject>();
for (int index = 0; index < jsonArray.size(); index++) {
JSONObject brandJSONObject = jsonArray.getJSONObject(index);
brandIdMap.put(brandJSONObject.getString("id"), brandJSONObject);
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>();
... ... @@ -211,7 +211,7 @@ public class BrandServiceImpl implements IBrandService, ApplicationEventPublishe
String brandAlif = entry.getKey();
JSONArray brands = new JSONArray();
for (String brandId : entry.getValue()) {
JSONObject brand = brandIdMap.get(brandId);
Map<String, Object> brand = brandIdMap.get(brandId);
if (brand != null) {
brands.add(brand);
}
... ... @@ -230,6 +230,9 @@ public class BrandServiceImpl implements IBrandService, ApplicationEventPublishe
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 indexName = ISearchConstants.INDEX_NAME_BRAND;
... ... @@ -238,25 +241,17 @@ public class BrandServiceImpl implements IBrandService, ApplicationEventPublishe
CACHE_MATCH_REQUEST.info("match cache , url is :/brand/list.json?" + HttpServletRequestUtils.genParamString(paramMap));
return new SearchApiResult().setData(cacheJSONArray);
}
// 3、执行搜索
SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_BRAND, 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> dataMap = new HashMap<>();
dataMap.put("id", map.get("id"));
dataMap.put("brand_alif", map.get("brandAlif"));
dataMap.put("brand_name_en", map.get("brandNameEn"));
dataMap.put("brand_domain", map.get("brandDomain"));
dataMap.put("is_hot", map.get("isHot"));
dataMap.put("hot_keyword", map.get("hotKeyword"));
dataMap.put("brand_name_cn", map.get("brandNameCn"));
dataMap.put("brand_ico", map.get("brandIco"));
dataMap.put("brand_name", map.get("brandName"));
dataMap.put("brand_keyword", map.get("brandKeyword"));
jsonArray.add(dataMap);
Map<String, Object> brandInfo= brandIndexBaseService.getBrandMap(map);
jsonArray.add(brandInfo);
}
searchCacheService.addJSONArrayToCache(brandCacheEnum, indexName, searchParam, jsonArray);
return new SearchApiResult().setMessage("brands info").setData(jsonArray);
... ...