StandardAggregation.java
5.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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(10000);
}
@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;
}
}