GenderAggregation.java
2 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
package com.yoho.search.aggregations.impls;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import org.apache.commons.lang3.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;
public class GenderAggregation extends AbstractAggregation {
private Map<String, String> paramMap;
GenderAggregation(Map<String, String> paramMap) {
this.paramMap = paramMap;
}
@Override
public String aggName() {
return "genderAgg";
}
@Override
public AbstractAggregationBuilder getBuilder() {
return AggregationBuilders.terms(aggName()).field("gender").size(10);
}
@Override
public Object getAggregationResponseMap(Map<String, Aggregation> aggMaps) {
MultiBucketsAggregation aggregation = this.getAggregation(aggMaps);
if (aggregation == null) {
return null;
}
Map<String, Object> genderMap = new LinkedHashMap<String, Object>();
Iterator<? extends Bucket> itgenderAgg = aggregation.getBuckets().iterator();
while (itgenderAgg.hasNext()) {
Bucket ltgender = itgenderAgg.next();
String genderId = ltgender.getKey();
if ("1".equals(genderId)) {
genderMap.put("1,3", "BOYS");
} else if ("2".equals(genderId)) {
genderMap.put("2,3", "GIRLS");
} else {
genderMap.put("1,3", "BOYS");
genderMap.put("2,3", "GIRLS");
}
}
if (paramMap!=null && paramMap.containsKey("gender") && StringUtils.isNotBlank(paramMap.get("gender"))) {
String gender = paramMap.get("gender");
if ("1,3".equals(gender) || "1".equals(gender)) {
genderMap.put(gender, "BOYS");
} else if ("2,3".equals(gender) || "2".equals(gender)) {
genderMap.put(gender, "GIRLS");
}
}
return genderMap;
}
}