PriceAggregation.java 2.95 KB
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;
	}
}