StandardAggregation.java 5.42 KB
package com.yoho.search.aggregations.impls;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

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 com.yoho.search.service.SearchCommonService;
import com.yoho.search.utils.ISearchConstans;

public class StandardAggregation extends AbstractAggregation {
	
	private SearchCommonService searchCommonService;
	private Map<String, String> paramMap;
	
	StandardAggregation(SearchCommonService searchCommonService,Map<String, String> paramMap){
		this.searchCommonService = searchCommonService;
		this.paramMap = paramMap;
	}
	
	
	@Override
	public String aggName() {
		return "standardAgg";
	}

	@Override
	public AbstractAggregationBuilder getBuilder() {
		return AggregationBuilders.terms(aggName()).field("standardNames").size(5000);
	}

	@Override
	public Object getAggregationResponseMap(Map<String, Aggregation> aggMaps) {
		MultiBucketsAggregation aggregation = this.getAggregation(aggMaps);
		if (aggregation == null) {
			return null;
		}
		return getStandardReponse(aggregation);
	}
	
	@SuppressWarnings("unchecked")
	private Map<String, Object> getStandardReponse(MultiBucketsAggregation aggregation) {
		Map<String, Object> standardMap = new LinkedHashMap<String, Object>();

		Set<String> standardSet = new LinkedHashSet<String>(new ArrayList<String>());
		Iterator<? extends Bucket> itstandardAgg = aggregation.getBuckets().iterator();
		while (itstandardAgg.hasNext()) {
			Bucket ltstandard = itstandardAgg.next();
			// 787-领型:4438-圆领,788-衣长:4445-适中,789-袖长:4447-短袖,790-版型:4451-正常,791-厚度:4453-薄
			for (String standardid : ltstandard.getKey().split(";")) {
				standardSet.add(standardid.trim());
			}
		}
		// 返回规格数
		int iParamNum = paramMap.containsKey(ISearchConstans.PARAM_SEARCH_PARAMETPERNUM) ? Integer.parseInt(paramMap.get(ISearchConstans.PARAM_SEARCH_PARAMETPERNUM))
				: ISearchConstans.RESULT_PARAM_COMMON;

		Iterator<String> itStandardTypeAndValues = standardSet.iterator();

		Map<String, String> map = new HashMap<String, String>();
		while (itStandardTypeAndValues.hasNext()) {
			// 787-领型:4438-圆领
			String standardTypeAndValue = itStandardTypeAndValues.next();
			if (!"".equals(standardTypeAndValue) && !"null".equals(standardTypeAndValue)) {
				String[] tokens = standardTypeAndValue.split(":");
				if (tokens.length > 1 && tokens[0].split("-").length > 1 && tokens[1].split("-").length > 1) {
					String standardTypeId = tokens[0].split("-")[0];
					String standardTypeName = tokens[0].split("-")[1];
					if ("风格".equals(standardTypeName))
						continue;
					if (map.containsKey(standardTypeName)) {
						if (!standardTypeId.equals(map.get(standardTypeName))) {
							continue;
						}
					} else {
						map.put(standardTypeName, standardTypeId);
					}
					String standardId = tokens[1].split("-")[0];
					if (!"0".equals(standardId)) {
						String standardName = tokens[1].split("-")[1];
						String filterMapKey = "parameter_" + standardTypeId; // parameter_787
						if (standardMap.containsKey(filterMapKey)) {
							Map<String, Object> standardTypeMap = (Map<String, Object>) standardMap.get(filterMapKey);
							List<Map<String, String>> standardList = (List<Map<String, String>>) standardTypeMap.get("sub");
							Map<String, String> singleStandardMap = new HashMap<String, String>();
							singleStandardMap.put("standard_id", standardId);
							singleStandardMap.put("standard_name", standardName);
							standardList.add(singleStandardMap);
							standardTypeMap.put("sub", standardList);
							standardMap.put(filterMapKey, standardTypeMap);
						} else {
							Map<String, Object> standardTypeMap = new HashMap<String, Object>();
							standardTypeMap.put("standard_id", standardTypeId);
							standardTypeMap.put("standard_name", standardTypeName);
							List<Map<String, String>> standardList = new ArrayList<Map<String, String>>();
							Map<String, String> singleStandardMap = new HashMap<String, String>();
							singleStandardMap.put("standard_id", standardId);
							singleStandardMap.put("standard_name", standardName);
							standardList.add(singleStandardMap);
							standardTypeMap.put("sub", standardList);
							standardMap.put(filterMapKey, standardTypeMap);
						}
					}
				}
			}
		}

		Map<String, Object> rtnStandardMap = new LinkedHashMap<String, Object>();
		Iterator<Entry<String, Object>> entryStandard = standardMap.entrySet().iterator();
		int paramIndex = 0;

		for (String key : paramMap.keySet()) {
			if (key.contains("parameter_")) {
				rtnStandardMap.put(key, searchCommonService.doGet(ISearchConstans.INDEX_NAME_STANDARD, paramMap.get(key)));
			}
		}

		while (entryStandard.hasNext()) {
			paramIndex++;
			Entry<String, Object> entry = entryStandard.next();
			if (!rtnStandardMap.containsKey(entry.getKey())) {
				rtnStandardMap.put(entry.getKey(), entry.getValue());
			}
			if (paramIndex >= iParamNum) {
				break;
			}
		}

		return rtnStandardMap;
	}
}