Authored by 胡古飞

/productindex/aggProductListByBrand 默认按品牌聚合

... ... @@ -87,24 +87,28 @@ public class SearchSortHelper {
if (!orderValues.contains(sortField)) {
continue;
}
String[] sortParts = sortField.split(":");
String realSortField = sortParts[0];
if (realSortField.contains("activities")) {
// do nothing
}else if (realSortField.contains("pools")) {
// do nothing
}else if (realSortField.equals("discount")) {
realSortField = "promotionDiscount";
} else {
realSortField = CharUtils.underlineToCamelhump(realSortField);
}
if (realSortField.equalsIgnoreCase("score")) {
realSortField = "_score";
}
realOrder.append("," + realSortField + ":" + sortParts[1]);
realOrder.append("," + this.dealSortField(sortField));
}
return realOrder.toString().replaceFirst(",", "");
}
public String dealSortField(String sortField){
String[] sortParts = sortField.split(":");
String realSortField = sortParts[0];
if (realSortField.contains("activities")) {
// do nothing
}else if (realSortField.contains("pools")) {
// do nothing
}else if (realSortField.equals("discount")) {
realSortField = "promotionDiscount";
} else {
realSortField = CharUtils.underlineToCamelhump(realSortField);
}
if (realSortField.equalsIgnoreCase("score")) {
realSortField = "_score";
}
return realSortField + ":" + sortParts[1];
}
/**
*
... ...
... ... @@ -207,14 +207,22 @@ public class AggProductListServiceImpl implements IAggProductListService, Applic
// 4、构造聚合条件
final String firstAggName = "firstAgg";
String order = "sales_num:desc";
if (searchCommonHelper.isNeedPersonalSearch(paramMap)) {
order = "_score:desc";
}
order = searchSortHelper.dealSortField(order);
String sortField = order.split(":")[0];
SortOrder sortOrder = order.split(":")[1].equals("desc") ? SortOrder.DESC : SortOrder.ASC;
List<AbstractAggregationBuilder> list = new ArrayList<AbstractAggregationBuilder>();
// 4.1)构造父聚合:品牌或品类聚合【同时按子聚合的sort字段排序】
TermsBuilder brandAggregationBuilder = AggregationBuilders.terms(firstAggName).field("brandId").order(Terms.Order.aggregation("sort", false)).size(10*pageSize);
TermsBuilder brandAggregationBuilder = AggregationBuilders.terms(firstAggName).field("brandId").order(Terms.Order.aggregation("sort", sortOrder.equals(SortOrder.ASC)))
.size(200+pageSize);
// 4.2)添加子聚合:取得分最大的值
brandAggregationBuilder.subAggregation(AggregationBuilders.max("sort").field("_score"));
brandAggregationBuilder.subAggregation(AggregationBuilders.max("sort").field(sortField));
// 4.3)添加孙聚合:取打分最高的一个product
brandAggregationBuilder.subAggregation(AggregationBuilders.topHits("product").addSort(SortBuilders.fieldSort("_score").order(SortOrder.DESC)).setSize(1));
brandAggregationBuilder.subAggregation(AggregationBuilders.topHits("product").addSort(SortBuilders.fieldSort(sortField).order(sortOrder)).setSize(1));
list.add(brandAggregationBuilder);
searchParam.setAggregationBuilders(list);
... ... @@ -236,7 +244,7 @@ public class AggProductListServiceImpl implements IAggProductListService, Applic
if (!aggMaps.containsKey(firstAggName)) {
return searchApiResult.setData("");
}
List<Map<String, Object>> productList = this.getProductListOrderByScore(((MultiBucketsAggregation) aggMaps.get(firstAggName)), pageSize);
List<Map<String, Object>> productList = this.getProductListOrderByScore(((MultiBucketsAggregation) aggMaps.get(firstAggName)), pageSize,sortField,sortOrder);
jsonObject = new JSONObject();
jsonObject.put("total", pageSize);
jsonObject.put("page", 1);
... ... @@ -252,7 +260,7 @@ public class AggProductListServiceImpl implements IAggProductListService, Applic
}
}
private List<Map<String, Object>> getProductListOrderByScore(final MultiBucketsAggregation aggregation, int viewNum) {
private List<Map<String, Object>> getProductListOrderByScore(final MultiBucketsAggregation aggregation, int viewNum, String sortField, SortOrder sortOrder) {
Iterator<? extends Bucket> itAgg = aggregation.getBuckets().iterator();
// 获取品牌聚合出来的商品
TopHits topHits;
... ... @@ -274,7 +282,7 @@ public class AggProductListServiceImpl implements IAggProductListService, Applic
}
}
}
dataList = this.sortListByScore(dataList, viewNum);
dataList = this.sortListBySortField(dataList, viewNum, sortField, sortOrder);
try {
List<String> sknStr = new ArrayList<String>();
for (Map<String, Object> data : dataList) {
... ... @@ -289,34 +297,51 @@ public class AggProductListServiceImpl implements IAggProductListService, Applic
}
return result;
}
private float getFloat(Object value){
if(value==null){
private double getDouble(Object value) {
if (value == null) {
return 0;
}
if(! (value instanceof Float)){
return 0;
if (value instanceof Float){
return ((Float) value).floatValue();
}
if (value instanceof Integer){
return ((Integer) value);
}
return ((Float)value).floatValue();
if (value instanceof Long){
return ((Long) value);
}
if (value instanceof String){
return Double.valueOf(value.toString());
}
if (value instanceof Double){
return Double.valueOf(value.toString());
}
return 0;
}
private List<Map<String, Object>> sortListByScore(List<Map<String, Object>> productList, int viewNum) {
private List<Map<String, Object>> sortListBySortField(List<Map<String, Object>> productList, int viewNum, String orderField, SortOrder sortOrder) {
if (productList == null || productList.isEmpty()) {
return new ArrayList<Map<String, Object>>();
}
// 再按照某个字段对商品排序
boolean isDesc = sortOrder.equals(SortOrder.DESC) ? true : false;
Collections.sort(productList, new Comparator<Map<String, Object>>() {
public int compare(Map<String, Object> o1, Map<String, Object> o2) {
try {
float score1 = getFloat(o1.get("_score"));
float score2 = getFloat(o2.get("_score"));
if(score1==score2){
double value1 = getDouble(o1.get(orderField));
double value2 = getDouble(o2.get(orderField));
if (value1 == value2) {
return 0;
}
return score1 - score2 > 0 ? -1 : 1;
if (isDesc) {
return value1 - value2 > 0 ? -1 : 1;
} else {
return value1 - value2 > 0 ? 1 : -1;
}
} catch (Exception e) {
logger.error(e.getMessage(),e);
logger.error(e.getMessage(), e);
return -1;
}
}
... ...