PriceAggregation.java
2.95 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package com.yoho.search.aggregations.impls;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
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 org.elasticsearch.search.aggregations.bucket.terms.Terms;
import com.yoho.search.aggregations.AbstractAggregation;
import com.yoho.search.utils.PriceRangeUtils;
public class PriceAggregation extends AbstractAggregation {
PriceAggregation(){
super();
}
@Override
public String aggName() {
return "priceAgg";
}
@Override
public AbstractAggregationBuilder getBuilder() {
return AggregationBuilders.terms(aggName()).field("salesPrice").size(10000).order(Terms.Order.term(true));
}
@Override
public Object getAggregationResponseMap(Map<String, Aggregation> aggMaps) {
MultiBucketsAggregation aggregation = this.getAggregation(aggMaps);
if (aggregation == null) {
return null;
}
Map<String, Object> priceMap = new LinkedHashMap<String, Object>();
if (aggregation.getBuckets().isEmpty()) {
return priceMap;
}
Iterator<? extends Bucket> itPriceAgg = aggregation.getBuckets().iterator();
int bucketSizes = aggregation.getBuckets().size();
float[] allPrices = new float[bucketSizes];
int i = 0;
while (itPriceAgg.hasNext()) {
allPrices[i++] = Float.parseFloat(itPriceAgg.next().getKey());
}
float maxPrice = allPrices[allPrices.length - 1];
// 划分价格区间
List<Integer> intervals = PriceRangeUtils.getPriceInterval(allPrices, maxPrice);
// 获取结果
Map<String, Object> priceResult = this.getPriceResult(intervals);
return priceResult;
}
private Map<String, Object> getPriceResult(List<Integer> priceIntervals) {
Map<String, Object> map = new LinkedHashMap<String, Object>();
if (priceIntervals == null || priceIntervals.isEmpty()) {
return map;
}
int priceSize = priceIntervals.size();
int j = 0;
String priceKeyPattern = "%s,%s";
for (j = 0; j < priceSize - 1; j++) {
int currentPrice = priceIntervals.get(j);
int fromPrice = j > 0 ? currentPrice + 1 : currentPrice;
int toPrice = priceIntervals.get(j + 1);
map.put(String.format(priceKeyPattern, fromPrice, toPrice), "¥" + fromPrice + "-" + toPrice);
}
map.put(String.format(priceKeyPattern, priceIntervals.get(j) + 1, 99999), "¥" + priceIntervals.get(j) + "以上");
return map;
}
public Map<String, Object> getPrePriceMap() {
Map<String, Object> priceMap = new LinkedHashMap<String, Object>();
priceMap.put("0,300", "¥0-300");
priceMap.put("300,600", "¥300-600");
priceMap.put("600,1000", "¥600-1000");
priceMap.put("1000,2000", "¥1000-2000");
priceMap.put("2000,99999", "¥2000以上");
return priceMap;
}
}