Authored by hugufei

增加最近7天上架商品数的聚合对象

@@ -12,7 +12,7 @@ public abstract class AbstractAggregation implements IAggregation{ @@ -12,7 +12,7 @@ public abstract class AbstractAggregation implements IAggregation{
12 protected static final Logger logger = LoggerFactory.getLogger(AbstractAggregation.class); 12 protected static final Logger logger = LoggerFactory.getLogger(AbstractAggregation.class);
13 13
14 protected MultiBucketsAggregation getAggregation(Map<String, Aggregation> aggMaps){ 14 protected MultiBucketsAggregation getAggregation(Map<String, Aggregation> aggMaps){
15 - if(!aggMaps.containsKey(aggName())){ 15 + if(aggMaps==null || !aggMaps.containsKey(aggName())){
16 return null; 16 return null;
17 } 17 }
18 return (MultiBucketsAggregation)aggMaps.get(aggName()); 18 return (MultiBucketsAggregation)aggMaps.get(aggName());
@@ -50,4 +50,8 @@ public class AggregationFactoryService { @@ -50,4 +50,8 @@ public class AggregationFactoryService {
50 return new StandardAggregation(searchCommonService,paramMap); 50 return new StandardAggregation(searchCommonService,paramMap);
51 } 51 }
52 52
  53 + public IAggregation getRecentShelveDayAggregation(){
  54 + return new RecentShelveDayAggregation();
  55 + }
  56 +
53 } 57 }
  1 +package com.yoho.search.aggregations.impls;
  2 +
  3 +import java.util.Iterator;
  4 +import java.util.LinkedHashMap;
  5 +import java.util.Map;
  6 +
  7 +import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
  8 +import org.elasticsearch.search.aggregations.Aggregation;
  9 +import org.elasticsearch.search.aggregations.AggregationBuilders;
  10 +import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation;
  11 +import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket;
  12 +import org.elasticsearch.search.aggregations.bucket.terms.Terms;
  13 +
  14 +import com.yoho.search.aggregations.AbstractAggregation;
  15 +
  16 +/**
  17 + * 最近7天每天上架商品数量的聚合对象
  18 + *
  19 + * @author hugufei
  20 + *
  21 + */
  22 +public class RecentShelveDayAggregation extends AbstractAggregation {
  23 +
  24 + RecentShelveDayAggregation(){
  25 + super();
  26 + }
  27 +
  28 + @Override
  29 + public String aggName() {
  30 + return "recentShelveDayAgg";
  31 + }
  32 +
  33 + @Override
  34 + public AbstractAggregationBuilder getBuilder() {
  35 + return AggregationBuilders.terms(aggName()).field("shelveDay").size(7).order(Terms.Order.term(false));
  36 + }
  37 +
  38 + @Override
  39 + public Object getAggregationResponseMap(Map<String, Aggregation> aggMaps) {
  40 + MultiBucketsAggregation aggregation = this.getAggregation(aggMaps);
  41 + if (aggregation == null) {
  42 + return null;
  43 + }
  44 + Map<String, Object> map = new LinkedHashMap<String, Object>();
  45 + Iterator<? extends Bucket> itAgg = aggregation.getBuckets().iterator();
  46 + while (itAgg.hasNext()) {
  47 + Bucket lt = itAgg.next();
  48 + map.put(lt.getKey(), lt.getDocCount());
  49 + }
  50 + return map;
  51 + }
  52 +
  53 +}
@@ -96,13 +96,11 @@ public class SearchService { @@ -96,13 +96,11 @@ public class SearchService {
96 jsonMap.put("data", ""); 96 jsonMap.put("data", "");
97 return jsonMap; 97 return jsonMap;
98 } 98 }
99 - if (searchResult.getAggMaps() != null) { 99 + //获取聚合结果
  100 + Object discountAggResult = discountAggregation.getAggregationResponseMap(searchResult.getAggMaps());
  101 + if (discountAggResult != null) {
100 Map<String, Object> filter = new HashMap<String, Object>(); 102 Map<String, Object> filter = new HashMap<String, Object>();
101 - Map<String, Aggregation> aggMaps = searchResult.getAggMaps();  
102 - Object discountAggResult = discountAggregation.getAggregationResponseMap(aggMaps);  
103 - if (discountAggResult != null) {  
104 - filter.put("discount", discountAggResult);  
105 - } 103 + filter.put("discount", discountAggResult);
106 jsonMap.put("data", filter); 104 jsonMap.put("data", filter);
107 } else { 105 } else {
108 jsonMap.put("data", ""); 106 jsonMap.put("data", "");
@@ -111,13 +109,6 @@ public class SearchService { @@ -111,13 +109,6 @@ public class SearchService {
111 return jsonMap; 109 return jsonMap;
112 } 110 }
113 111
114 - public Map<String, Object> makeValueMap(String name, long count) {  
115 - Map<String, Object> valueMap = new HashMap<String, Object>();  
116 - valueMap.put("name", name);  
117 - valueMap.put("count", count);  
118 - return valueMap;  
119 - }  
120 -  
121 /** 112 /**
122 * 制作过滤条件报文 113 * 制作过滤条件报文
123 * */ 114 * */
@@ -133,37 +124,6 @@ public class SearchService { @@ -133,37 +124,6 @@ public class SearchService {
133 return searchCommonService.doMultiGet(indexName, set, null); 124 return searchCommonService.doMultiGet(indexName, set, null);
134 } 125 }
135 126
136 - public List<Map<String, Object>> getFilterResponseMapForBrand(MultiBucketsAggregation aggregation, String indexName, String[] ids) throws Exception {  
137 - Set<String> set = new LinkedHashSet<String>();  
138 - Iterator<? extends Bucket> itSizeAgg = aggregation.getBuckets().iterator();  
139 - while (itSizeAgg.hasNext()) {  
140 - Bucket ltSize = itSizeAgg.next();  
141 - for (String sizeId : ltSize.getKey().split(",")) {  
142 - set.add(sizeId);  
143 - }  
144 - }  
145 - for (String id : ids) {  
146 - set.add(id);  
147 - }  
148 - return searchCommonService.doMultiGet(indexName, set, null);  
149 - }  
150 -  
151 - /**  
152 - * 获取按最近上架时间聚合的结果  
153 - *  
154 - * @param aggregation  
155 - * @return  
156 - */  
157 - public Map<String, Object> getRecentShelveDayResponseMap(MultiBucketsAggregation aggregation) {  
158 - Map<String, Object> map = new LinkedHashMap<String, Object>();  
159 - Iterator<? extends Bucket> itAgg = aggregation.getBuckets().iterator();  
160 - while (itAgg.hasNext()) {  
161 - Bucket lt = itAgg.next();  
162 - map.put(lt.getKey(), lt.getDocCount());  
163 - }  
164 - return map;  
165 - }  
166 -  
167 /** 127 /**
168 * 获取按上架日期聚合的结果[日期列表] 128 * 获取按上架日期聚合的结果[日期列表]
169 * 129 *
@@ -181,9 +141,8 @@ public class SearchService { @@ -181,9 +141,8 @@ public class SearchService {
181 searchParam.setFiter(searchServiceHelper.constructFilterBuilder(paramMap, null)); 141 searchParam.setFiter(searchServiceHelper.constructFilterBuilder(paramMap, null));
182 142
183 // 构造聚合参数 143 // 构造聚合参数
184 - List<AbstractAggregationBuilder> list = new ArrayList<AbstractAggregationBuilder>();  
185 - list.add(AggregationBuilders.terms("recentShelveDay").field("shelveDay").size(7).order(Terms.Order.term(false)));  
186 - searchParam.setAggregationBuilders(list); 144 + IAggregation recentShelveDayAggregation = aggregationFactoryService.getRecentShelveDayAggregation();
  145 + searchParam.setAggregationBuilders(Arrays.asList(recentShelveDayAggregation.getBuilder()));
187 searchParam.setSearchType(SearchType.COUNT); 146 searchParam.setSearchType(SearchType.COUNT);
188 147
189 // 进行检索 148 // 进行检索
@@ -199,12 +158,12 @@ public class SearchService { @@ -199,12 +158,12 @@ public class SearchService {
199 jsonMap.put("data", ""); 158 jsonMap.put("data", "");
200 return jsonMap; 159 return jsonMap;
201 } 160 }
202 - if (searchResult.getAggMaps() != null) { 161 +
  162 + // 获取聚合结果
  163 + Object recentShelveDayResponse = recentShelveDayAggregation.getAggregationResponseMap(searchResult.getAggMaps());
  164 + if (recentShelveDayResponse != null) {
203 Map<String, Object> filter = new HashMap<String, Object>(); 165 Map<String, Object> filter = new HashMap<String, Object>();
204 - Map<String, Aggregation> aggMaps = searchResult.getAggMaps();  
205 - if (aggMaps.containsKey("recentShelveDay")) {  
206 - filter.put("recent", this.getRecentShelveDayResponseMap((MultiBucketsAggregation) aggMaps.get("recentShelveDay")));  
207 - } 166 + filter.put("recent", recentShelveDayResponse);
208 jsonMap.put("data", filter); 167 jsonMap.put("data", filter);
209 } else { 168 } else {
210 jsonMap.put("data", ""); 169 jsonMap.put("data", "");
@@ -648,7 +607,7 @@ public class SearchService { @@ -648,7 +607,7 @@ public class SearchService {
648 } 607 }
649 return brandAlifMap; 608 return brandAlifMap;
650 } 609 }
651 - 610 +
652 /** 611 /**
653 * 根据query关键字获取商品总数 612 * 根据query关键字获取商品总数
654 * 613 *