Authored by hugufei

风格聚合按名称合并id

... ... @@ -73,7 +73,7 @@ public class AggregationFactory {
}
public IAggregation getStyleAggregation(Map<String, String> paramMap) {
return new StyleAggregation(styleIndexBaseService, paramMap);
return new StyleAggregation(paramMap);
}
public IAggregation getBrandAggregation(Map<String, String> paramMap) {
... ...
package com.yoho.search.service.aggregations.impls;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import com.yoho.search.base.utils.ProductIndexEsField;
import com.yoho.search.core.es.agg.AbstractAggregation;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
import org.elasticsearch.search.aggregations.Aggregation;
... ... @@ -14,19 +11,15 @@ import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Buck
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.yoho.search.base.utils.ProductIndexEsField;
import com.yoho.search.core.es.agg.AbstractAggregation;
import com.yoho.search.service.index.StyleIndexBaseService;
import java.util.*;
public class StyleAggregation extends AbstractAggregation {
private static final Logger logger = LoggerFactory.getLogger(StyleAggregation.class);
private StyleIndexBaseService styleIndexBaseService;
private Map<String, String> paramMap;
StyleAggregation(StyleIndexBaseService styleIndexBaseService, Map<String, String> paramMap) {
this.styleIndexBaseService = styleIndexBaseService;
StyleAggregation(Map<String, String> paramMap) {
this.paramMap = paramMap;
}
... ... @@ -42,35 +35,49 @@ public class StyleAggregation extends AbstractAggregation {
@Override
public AbstractAggregationBuilder<?> getBuilder() {
return AggregationBuilders.terms(aggName()).field(ProductIndexEsField.styleIds).size(100);
return AggregationBuilders.terms(aggName()).field(ProductIndexEsField.styleIdAndNames).size(500);
}
@Override
public Object getAggregationResponseMap(Map<String, Aggregation> aggMaps) {
try {
MultiBucketsAggregation aggregation = this.getAggregation(aggMaps);
if (aggregation == null) {
return null;
}
Set<String> styleIdsSet = new LinkedHashSet<String>();
Set<String> styleIdAndNames = new LinkedHashSet<String>();
Iterator<? extends Bucket> itSizeAgg = aggregation.getBuckets().iterator();
while (itSizeAgg.hasNext()) {
Bucket ltSize = itSizeAgg.next();
for (String sizeId : ltSize.getKeyAsString().split(",")) {
styleIdsSet.add(sizeId);
styleIdAndNames.add(ltSize.getKeyAsString());
}
Map<String, List<String>> styleNameToIdMap = new HashMap<String, List<String>>();
for (String styleIdAndName : styleIdAndNames) {
if (!styleIdAndName.contains("-")) {
continue;
}
if (paramMap.containsKey("style") && StringUtils.isNotBlank(paramMap.get("style"))) {
String[] ids = paramMap.get("style").split(",");
for (String id : ids) {
styleIdsSet.add(id);
String styleId = styleIdAndName.split("-")[0];
String styleName = styleIdAndName.split("-")[1];
List<String> styleIdList = styleNameToIdMap.computeIfAbsent(styleName, a -> new ArrayList<>());
styleIdList.add(styleId);
}
List<Map<String, Object>> resultList = new ArrayList<>();
for (Map.Entry<String, List<String>> entry : styleNameToIdMap.entrySet()) {
String styleName = entry.getKey();
String styleId = StringUtils.join(entry.getValue(), ",");
Map<String, Object> result = new HashMap<>();
result.put("style_id", styleId);
result.put("style_name", styleName);
resultList.add(result);
}
try {
return styleIndexBaseService.getStyleListByIds(styleIdsSet);
} catch (Exception e) {
return resultList;
}catch (Exception e){
logger.error(e.getMessage(),e);
return null;
return new ArrayList<>();
}
}
public static void main(String[] args) {
System.out.println("989-运动".contains("-"));
}
}
... ...