StyleAggregation.java
2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.yoho.search.aggregations.impls;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
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 com.yoho.search.aggregations.AbstractAggregation;
import com.yoho.search.service.SearchCommonService;
import com.yoho.search.utils.ISearchConstans;
public class StyleAggregation extends AbstractAggregation {
private SearchCommonService searchCommonService;
private Map<String, String> paramMap;
StyleAggregation(SearchCommonService searchCommonService, Map<String, String> paramMap) {
this.searchCommonService = searchCommonService;
this.paramMap = paramMap;
}
@Override
public String aggName() {
return "styleAgg";
}
@Override
public AbstractAggregationBuilder getBuilder() {
return AggregationBuilders.terms(aggName()).field("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.getKey().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 searchCommonService.doMultiGet(ISearchConstans.INDEX_NAME_STYLE, styleIdsSet, null);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}