|
|
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,63 +11,73 @@ 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;
|
|
|
this.paramMap = paramMap;
|
|
|
}
|
|
|
private static final Logger logger = LoggerFactory.getLogger(StyleAggregation.class);
|
|
|
|
|
|
private Map<String, String> paramMap;
|
|
|
|
|
|
StyleAggregation(Map<String, String> paramMap) {
|
|
|
this.paramMap = paramMap;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public String aggName() {
|
|
|
return "styleAgg";
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public String filterName() {
|
|
|
return "style";
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public String aggName() {
|
|
|
return "styleAgg";
|
|
|
}
|
|
|
@Override
|
|
|
public AbstractAggregationBuilder<?> getBuilder() {
|
|
|
return AggregationBuilders.terms(aggName()).field(ProductIndexEsField.styleIdAndNames).size(500);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public String filterName() {
|
|
|
return "style";
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public AbstractAggregationBuilder<?> getBuilder() {
|
|
|
return AggregationBuilders.terms(aggName()).field(ProductIndexEsField.styleIds).size(100);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public Object getAggregationResponseMap(Map<String, Aggregation> aggMaps) {
|
|
|
MultiBucketsAggregation aggregation = this.getAggregation(aggMaps);
|
|
|
if (aggregation == null) {
|
|
|
return null;
|
|
|
}
|
|
|
Set<String> styleIdsSet = 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);
|
|
|
}
|
|
|
}
|
|
|
if (paramMap.containsKey("style") && StringUtils.isNotBlank(paramMap.get("style"))) {
|
|
|
String[] ids = paramMap.get("style").split(",");
|
|
|
for (String id : ids) {
|
|
|
styleIdsSet.add(id);
|
|
|
}
|
|
|
}
|
|
|
try {
|
|
|
return styleIndexBaseService.getStyleListByIds(styleIdsSet);
|
|
|
} catch (Exception e) {
|
|
|
logger.error(e.getMessage(),e);
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
@Override
|
|
|
public Object getAggregationResponseMap(Map<String, Aggregation> aggMaps) {
|
|
|
try {
|
|
|
MultiBucketsAggregation aggregation = this.getAggregation(aggMaps);
|
|
|
if (aggregation == null) {
|
|
|
return null;
|
|
|
}
|
|
|
Set<String> styleIdAndNames = new LinkedHashSet<String>();
|
|
|
Iterator<? extends Bucket> itSizeAgg = aggregation.getBuckets().iterator();
|
|
|
while (itSizeAgg.hasNext()) {
|
|
|
Bucket ltSize = itSizeAgg.next();
|
|
|
styleIdAndNames.add(ltSize.getKeyAsString());
|
|
|
}
|
|
|
Map<String, List<String>> styleNameToIdMap = new HashMap<String, List<String>>();
|
|
|
for (String styleIdAndName : styleIdAndNames) {
|
|
|
if (!styleIdAndName.contains("-")) {
|
|
|
continue;
|
|
|
}
|
|
|
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);
|
|
|
}
|
|
|
return resultList;
|
|
|
}catch (Exception e){
|
|
|
logger.error(e.getMessage(),e);
|
|
|
return new ArrayList<>();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
System.out.println("989-运动".contains("-"));
|
|
|
}
|
|
|
} |
...
|
...
|
|