...
|
...
|
@@ -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;
|
...
|
...
|
@@ -78,8 +80,6 @@ public class BrandServiceImpl extends AbstractCacheAbleService implements IBrand |
|
|
@Autowired
|
|
|
private BrandIndexBaseService brandIndexBaseService;
|
|
|
@Autowired
|
|
|
private ProductIndexBaseService productIndexBaseService;
|
|
|
@Autowired
|
|
|
private AggregationFactoryService aggregationFactoryService;
|
|
|
@Autowired
|
|
|
private SearchCommonHelper searchCommonHelper;
|
...
|
...
|
@@ -323,9 +323,11 @@ public class BrandServiceImpl extends AbstractCacheAbleService implements IBrand |
|
|
logger.info("[func=aggProductsByBrandIdInParam][param={}][begin={}]", paramMap.toString(), System.currentTimeMillis());
|
|
|
|
|
|
// 1、参数校验
|
|
|
if (!paramMap.containsKey("brand")) {
|
|
|
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)) {
|
...
|
...
|
@@ -378,7 +380,7 @@ public class BrandServiceImpl extends AbstractCacheAbleService implements IBrand |
|
|
}
|
|
|
|
|
|
// 9、构造返回数据并加入缓存
|
|
|
jsonArray = getNewBrandResponseMap(((MultiBucketsAggregation) aggMaps.get("brandAgg")), limit, paramMap.get("brand"), realSortField, realSortOrder);
|
|
|
jsonArray = getNewBrandResponseMap(((MultiBucketsAggregation) aggMaps.get("brandAgg")), limit, brandIds, realSortField, realSortOrder);
|
|
|
searchCacheService.addJSONArrayToCache(this.searchCache, indexName, searchParam, jsonArray);
|
|
|
return searchApiResult.setData(jsonArray);
|
|
|
} catch (Exception e) {
|
...
|
...
|
@@ -388,10 +390,10 @@ public class BrandServiceImpl extends AbstractCacheAbleService implements IBrand |
|
|
}
|
|
|
}
|
|
|
|
|
|
private JSONArray getNewBrandResponseMap(MultiBucketsAggregation aggregation, int size, String brands, final String realSortField, final SortOrder realSortOrder) {
|
|
|
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>> brand_product_map = new HashMap<>();
|
|
|
Map<String, Map<String, Object>> brandId2ProductMap = new HashMap<>();
|
|
|
while (itAgg.hasNext()) {
|
|
|
Bucket lt = itAgg.next();
|
|
|
if (lt.getAggregations().getAsMap().containsKey("product")) {
|
...
|
...
|
@@ -399,63 +401,43 @@ public class BrandServiceImpl extends AbstractCacheAbleService implements IBrand |
|
|
if (topHits != null) {
|
|
|
SearchHits hits = topHits.getHits();
|
|
|
for (SearchHit hit : hits.getHits()) {
|
|
|
brand_product_map.put(lt.getKeyAsString(), hit.getSource());
|
|
|
brandId2ProductMap.put(lt.getKeyAsString(), hit.getSource());
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
brand_product_map = productListHelper.buildReturnInfoByEsSourceMap(brand_product_map);
|
|
|
|
|
|
// 获取前面“size”个品牌的商品
|
|
|
if (brand_product_map.size() == 0) {
|
|
|
if (brandId2ProductMap == null || brandId2ProductMap.isEmpty()) {
|
|
|
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++;
|
|
|
|
|
|
// 2、按品牌id获取商品列表
|
|
|
List<Map<String, Object>> productEsSourceList = new ArrayList<>();
|
|
|
for (Integer brandId : brandIds) {
|
|
|
if (brandId2ProductMap.containsKey(brandId.toString())) {
|
|
|
productEsSourceList.add(brandId2ProductMap.get(brandId.toString()));
|
|
|
}
|
|
|
i++;
|
|
|
if (productEsSourceList.size() >= size) {
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
// 再按照某个字段对商品排序
|
|
|
Collections.sort(productList, new Comparator<Map<String, Object>>() {
|
|
|
|
|
|
// 3、对商品列表进行排序
|
|
|
Collections.sort(productEsSourceList, 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);
|
|
|
Double map1value = MapUtils.getDoubleValue(o1, realSortField);
|
|
|
Double map2value = MapUtils.getDoubleValue(o2, realSortField);
|
|
|
if ("desc".equalsIgnoreCase(realSortOrder.name())) {
|
|
|
return map2value - map1value;
|
|
|
return map2value.compareTo(map1value);
|
|
|
} else {
|
|
|
return map1value - map2value;
|
|
|
return map1value.compareTo(map2value);
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
|
|
|
//4、生成返回对象
|
|
|
List<Map<String, Object>> productReturnInfoList = productListHelper.buildReturnInfoByEsSourceList(productEsSourceList);
|
|
|
JSONArray jsonArray = new JSONArray();
|
|
|
jsonArray.addAll(productList);
|
|
|
jsonArray.addAll(productReturnInfoList);
|
|
|
return jsonArray;
|
|
|
}
|
|
|
|
|
|
private int getIntValue(Object value) {
|
|
|
if (value == null) {
|
|
|
return 0;
|
|
|
}
|
|
|
try {
|
|
|
return Integer.parseInt(value.toString());
|
|
|
} catch (Exception e) {
|
|
|
return 0;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|