RecentShelveDayAggregation.java 1.49 KB
package com.yoho.search.aggregations.impls;

import java.util.Iterator;
import java.util.LinkedHashMap;
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;

/**
 * 最近7天每天上架商品数量的聚合对象
 * 
 * @author hugufei
 *
 */
public class RecentShelveDayAggregation extends AbstractAggregation {
	
	RecentShelveDayAggregation(){
		super();
	}
	
	@Override
	public String aggName() {
		return "recentShelveDayAgg";
	}

	@Override
	public AbstractAggregationBuilder getBuilder() {
		return AggregationBuilders.terms(aggName()).field("shelveDay").size(7).order(Terms.Order.term(false));
	}

	@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();
			map.put(lt.getKey(), lt.getDocCount());
		}
		return map;
	}

}