|
|
package com.yoho.search.aggregations.impls;
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Iterator;
|
|
|
import java.util.LinkedHashMap;
|
|
|
import java.util.Map;
|
|
|
|
|
|
import com.yoho.search.aggregations.AbstractAggregation;
|
|
|
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 java.util.HashMap;
|
|
|
import java.util.Iterator;
|
|
|
import java.util.LinkedHashMap;
|
|
|
import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
* 折扣聚合对象
|
|
|
*
|
|
|
* @author hugufei
|
|
|
*
|
|
|
* @author hugufei
|
|
|
*/
|
|
|
public class DiscountAggregation extends AbstractAggregation {
|
|
|
|
|
|
DiscountAggregation(){
|
|
|
super();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public String aggName() {
|
|
|
return "discountAgg";
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public AbstractAggregationBuilder getBuilder() {
|
|
|
return AggregationBuilders.range(aggName()).field("promotionDiscount").addRange(0.0, 0.399).addRange(0.4, 0.699).addRange(0.7, 0.999);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public Object getAggregationResponseMap(Map<String, Aggregation> aggMaps) {
|
|
|
MultiBucketsAggregation aggregation = this.getAggregation(aggMaps);
|
|
|
if (aggregation == null) {
|
|
|
return null;
|
|
|
}
|
|
|
Map<String, Object> map = new LinkedHashMap<String, Object>();
|
|
|
Iterator<? extends Bucket> itAgg = aggregation.getBuckets().iterator();
|
|
|
while (itAgg.hasNext()) {
|
|
|
Bucket lt = itAgg.next();
|
|
|
if (lt.getDocCount() > 0) {
|
|
|
String key = lt.getKey();
|
|
|
if ("0.0-0.399".equals(key)) {
|
|
|
map.put("0.01,0.399", makeValueMap("1~3", lt.getDocCount()));
|
|
|
} else if ("0.4-0.699".equals(key)) {
|
|
|
map.put("0.4,0.699", makeValueMap("4~6", lt.getDocCount()));
|
|
|
} else {
|
|
|
map.put("0.7,0.999", makeValueMap("7~9", lt.getDocCount()));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return map;
|
|
|
}
|
|
|
DiscountAggregation() {
|
|
|
super();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public String aggName() {
|
|
|
return "discountAgg";
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public AbstractAggregationBuilder getBuilder() {
|
|
|
return AggregationBuilders.range(aggName()).field("promotionDiscount").addRange(0.0, 0.399).addRange(0.4, 0.699).addRange(0.7, 0.999);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public Object getAggregationResponseMap(Map<String, Aggregation> aggMaps) {
|
|
|
MultiBucketsAggregation aggregation = this.getAggregation(aggMaps);
|
|
|
if (aggregation == null) {
|
|
|
return null;
|
|
|
}
|
|
|
Map<String, Object> map = new LinkedHashMap<String, Object>();
|
|
|
Iterator<? extends Bucket> itAgg = aggregation.getBuckets().iterator();
|
|
|
while (itAgg.hasNext()) {
|
|
|
Bucket lt = itAgg.next();
|
|
|
if (lt.getDocCount() > 0) {
|
|
|
String key = lt.getKey();
|
|
|
if ("0.0-0.399".equals(key)) {
|
|
|
map.put("0.01,0.399", makeValueMap("1~3", lt.getDocCount()));
|
|
|
} else if ("0.4-0.699".equals(key)) {
|
|
|
map.put("0.4,0.699", makeValueMap("4~6", lt.getDocCount()));
|
|
|
} else {
|
|
|
map.put("0.7,0.999", makeValueMap("7~9", lt.getDocCount()));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return map;
|
|
|
}
|
|
|
|
|
|
private Map<String, Object> makeValueMap(String name, long count) {
|
|
|
Map<String, Object> valueMap = new HashMap<String, Object>();
|
|
|
valueMap.put("name", name);
|
|
|
valueMap.put("count", count);
|
|
|
return valueMap;
|
|
|
}
|
|
|
private Map<String, Object> makeValueMap(String name, long count) {
|
|
|
Map<String, Object> valueMap = new HashMap<String, Object>();
|
|
|
valueMap.put("name", name);
|
|
|
valueMap.put("count", count);
|
|
|
return valueMap;
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|