IsSecialofferAggregation.java 1.21 KB
package com.yoho.search.aggregations.impls;

import java.util.Iterator;
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 com.yoho.search.aggregations.AbstractAggregation;

public class IsSecialofferAggregation extends AbstractAggregation{

	@Override
	public String aggName() {
		return "specialofferAgg";
	}

	@Override
	public AbstractAggregationBuilder getBuilder() {
		return AggregationBuilders.terms(aggName()).field("specialoffer").size(2);
	}
	
	@Override
	public Object getAggregationResponseMap(Map<String, Aggregation> aggMaps) {
		MultiBucketsAggregation aggregation = this.getAggregation(aggMaps);
		if (aggregation == null) {
			return null;
		}
		Iterator<? extends Bucket> itAgg = aggregation.getBuckets().iterator();
		while (itAgg.hasNext()) {
			Bucket bucket = itAgg.next();
			String bool = bucket.getKey();
			if ("Y".equals(bool)) {
				return true;
			}
		}
		return false;
	}
}