Authored by wangnan

promotion聚合的各种地方改成zqname聚合

@@ -29,7 +29,7 @@ public abstract class AbstractSingleFieldAggregation extends AbstractAggregation @@ -29,7 +29,7 @@ public abstract class AbstractSingleFieldAggregation extends AbstractAggregation
29 } 29 }
30 30
31 @Override 31 @Override
32 - public Object getAggregationResponseMap(Map<String, Aggregation> aggMaps) { 32 + public Object getAggregationResponseMap(Map<String, Aggregation> aggMaps) {
33 List<String> valueList = new ArrayList<String>(); 33 List<String> valueList = new ArrayList<String>();
34 MultiBucketsAggregation aggregation = this.getAggregation(aggMaps); 34 MultiBucketsAggregation aggregation = this.getAggregation(aggMaps);
35 if (aggregation == null) { 35 if (aggregation == null) {
@@ -126,8 +126,8 @@ public class AggregationFactoryService { @@ -126,8 +126,8 @@ public class AggregationFactoryService {
126 return new IsGlobalAggregation(); 126 return new IsGlobalAggregation();
127 } 127 }
128 128
129 - public IAggregation getPromotionAggregation(int aggCount) {  
130 - return new PromotionAggregation(zqNameIndexBaseService, aggCount); 129 + public IAggregation getZqNameAggregation(int aggCount) {
  130 + return new ZqNameAggregation(zqNameIndexBaseService, aggCount);
131 } 131 }
132 132
133 public IAggregation getKeywordAggregation(Map<String, String> paramMap, int aggCount) { 133 public IAggregation getKeywordAggregation(Map<String, String> paramMap, int aggCount) {
@@ -14,64 +14,59 @@ import java.util.stream.Collectors; @@ -14,64 +14,59 @@ import java.util.stream.Collectors;
14 /** 14 /**
15 * Created by wangnan on 2017/5/11. 15 * Created by wangnan on 2017/5/11.
16 */ 16 */
17 -public class PromotionAggregation extends AbstractSingleFieldAggregation { 17 +public class ZqNameAggregation extends AbstractSingleFieldAggregation {
18 18
19 - private ZqNameIndexBaseService zqNameIndexBaseService; 19 + private ZqNameIndexBaseService zqNameIndexBaseService;
20 20
21 - public PromotionAggregation(ZqNameIndexBaseService zqNameIndexBaseService, int count) {  
22 - super(count);  
23 - this.zqNameIndexBaseService = zqNameIndexBaseService;  
24 - } 21 + public ZqNameAggregation(ZqNameIndexBaseService zqNameIndexBaseService, int count) {
  22 + super(count);
  23 + this.zqNameIndexBaseService = zqNameIndexBaseService;
  24 + }
25 25
26 - @Override  
27 - protected String getField() {  
28 - return ProductIndexEsField.ispromotion;  
29 - } 26 + @Override
  27 + protected String getField() {
  28 + return ProductIndexEsField.ispromotion;
  29 + }
30 30
31 - @Override  
32 - public String aggName() {  
33 - return "promotionAgg";  
34 - } 31 + @Override
  32 + public String aggName() {
  33 + return "zqNameAgg";
  34 + }
35 35
36 - @Override  
37 - public String filterName() {  
38 - return "promotion";  
39 - } 36 + @Override
  37 + public String filterName() {
  38 + return "zqName";
  39 + }
40 40
41 - @SuppressWarnings("unchecked")  
42 - @Override  
43 - public Object getAggregationResponseMap(Map<String, Aggregation> aggMaps) {  
44 - List<String> promotionIdList = (List<String>) super.getAggregationResponseMap(aggMaps);  
45 - if (CollectionUtils.isNotEmpty(promotionIdList)) {  
46 - promotionIdList = promotionIdList.stream().collect(Collectors.toList());  
47 - }  
48 - if (CollectionUtils.isEmpty(promotionIdList)) {  
49 - return new ArrayList<>();  
50 - }  
51 - // 调用zqname索引查询title  
52 - Map<String, String> promotionParam = new HashMap<>();  
53 - promotionParam.put("ids", promotionIdList.stream().collect(Collectors.joining(",")));  
54 - List<Map<String, Object>> promotionList = zqNameIndexBaseService.queryZQNameListByParam(promotionParam);  
55 - // promotion_title去重,is_promotion用逗号连接  
56 - Map<String, Map<String, Object>> titleMap = new HashMap<>();  
57 - for (Map<String, Object> map : promotionList) {  
58 - String title = map.get("promotion_title").toString();  
59 - if (titleMap.containsKey(title)) {  
60 - String newKey = map.get("is_promotion").toString();  
61 - String oldKey = titleMap.get(title).get("is_promotion").toString();  
62 - Map<String, Object> newMap = new HashMap<String, Object>();  
63 - newMap.put("promotion_title", title);  
64 - newMap.put("is_promotion", newKey + "," + oldKey);  
65 - titleMap.put(title, newMap);  
66 - } else {  
67 - titleMap.put(title, map);  
68 - }  
69 - }  
70 - promotionList = new ArrayList<>();  
71 - for (Map.Entry<String, Map<String, Object>> entry : titleMap.entrySet()) {  
72 - promotionList.add(entry.getValue());  
73 - }  
74 - return promotionList == null || promotionList.isEmpty() ? new ArrayList<>() : promotionList;  
75 - } 41 + @Override
  42 + public Object getAggregationResponseMap(Map<String, Aggregation> aggMaps) {
  43 + List<String> zqIdList = (List<String>) super.getAggregationResponseMap(aggMaps);
  44 + if (CollectionUtils.isEmpty(zqIdList)) {
  45 + return new ArrayList<>();
  46 + }
  47 + // 调用zqname索引查询zqtitle
  48 + String ids = zqIdList.stream().collect(Collectors.joining(","));
  49 + List<Map<String, Object>> zqNameList = zqNameIndexBaseService.queryZQNameListByIds(ids);
  50 + // promotion_title去重,is_promotion用逗号连接
  51 + Map<String, Map<String, Object>> titleMap = new HashMap<>();
  52 + for (Map<String, Object> map : zqNameList) {
  53 + String title = map.get("promotion_title").toString();
  54 + if (titleMap.containsKey(title)) {
  55 + String newKey = map.get("is_promotion").toString();
  56 + String oldKey = titleMap.get(title).get("is_promotion").toString();
  57 + Map<String, Object> newMap = new HashMap<>();
  58 + newMap.put("promotion_title", title);
  59 + newMap.put("is_promotion", newKey + "," + oldKey);
  60 + titleMap.put(title, newMap);
  61 + } else {
  62 + titleMap.put(title, map);
  63 + }
  64 + }
  65 + zqNameList = new ArrayList<>();
  66 + for (Map.Entry<String, Map<String, Object>> entry : titleMap.entrySet()) {
  67 + zqNameList.add(entry.getValue());
  68 + }
  69 + return zqNameList == null || zqNameList.isEmpty() ? new ArrayList<>() : zqNameList;
  70 + }
76 71
77 } 72 }
@@ -28,22 +28,25 @@ import java.util.Map; @@ -28,22 +28,25 @@ import java.util.Map;
28 @Service 28 @Service
29 public class ZqNameIndexBaseService { 29 public class ZqNameIndexBaseService {
30 30
31 - @Autowired  
32 - private SearchCommonService searchCommonService;  
33 private static final Logger logger = LoggerFactory.getLogger(PromotionIndexBaseService.class); 31 private static final Logger logger = LoggerFactory.getLogger(PromotionIndexBaseService.class);
34 private static final String INDEX_NAME = ISearchConstants.INDEX_NAME_ZQ_NAME; 32 private static final String INDEX_NAME = ISearchConstants.INDEX_NAME_ZQ_NAME;
35 33
36 - public List<Map<String, Object>> queryZQNameListByParam(Map<String, String> paramMap) { 34 + @Autowired
  35 + private SearchCommonService searchCommonService;
  36 +
  37 + public List<Map<String, Object>> queryZQNameListByIds(String ids) {
37 try { 38 try {
38 - // 1、构建SearchParam  
39 SearchParam searchParam = new SearchParam(); 39 SearchParam searchParam = new SearchParam();
40 - BoolQueryBuilder boolFilterForShops = this.constructFilterBuilder(paramMap);  
41 - searchParam.setFiter(boolFilterForShops); 40 + BoolQueryBuilder boolFilter = QueryBuilders.boolQuery();
  41 + if (StringUtils.isNotBlank(ids)) {
  42 + int[] idArray = ConvertUtils.stringToIntArray(ids, ",");
  43 + boolFilter.must(QueryBuilders.termsQuery("id", idArray));
  44 + }
  45 + searchParam.setFiter(boolFilter);
42 searchParam.setSize(10000); 46 searchParam.setSize(10000);
43 searchParam.setQuery(QueryBuilders.matchAllQuery()); 47 searchParam.setQuery(QueryBuilders.matchAllQuery());
44 - // 2、调搜索,并将结果加入缓存  
45 - List<Map<String, Object>> promotionsList = this.queryZQNameListByParam(searchParam);  
46 - return promotionsList; 48 + List<Map<String, Object>> ZqNameList = this.queryZQNameListByParam(searchParam);
  49 + return ZqNameList;
47 } catch (Exception e) { 50 } catch (Exception e) {
48 logger.error(e.getMessage(), e); 51 logger.error(e.getMessage(), e);
49 return new ArrayList<>(); 52 return new ArrayList<>();
@@ -62,15 +65,6 @@ public class ZqNameIndexBaseService { @@ -62,15 +65,6 @@ public class ZqNameIndexBaseService {
62 return results; 65 return results;
63 } 66 }
64 67
65 - private BoolQueryBuilder constructFilterBuilder(Map<String, String> paramMap) {  
66 - BoolQueryBuilder boolFilter = QueryBuilders.boolQuery();  
67 - if (paramMap.containsKey("ids") && StringUtils.isNotBlank(paramMap.get("ids"))) {  
68 - int[] ids = ConvertUtils.stringToIntArray(paramMap.get("ids"), ",");  
69 - boolFilter.must(QueryBuilders.termsQuery("id", ids));  
70 - }  
71 - return boolFilter;  
72 - }  
73 -  
74 private Map<String, Object> getMap(Map<String, Object> esMap) { 68 private Map<String, Object> getMap(Map<String, Object> esMap) {
75 Map<String, Object> map = new HashMap<>(); 69 Map<String, Object> map = new HashMap<>();
76 int id = MapUtils.getIntValue(esMap, "id", 0); 70 int id = MapUtils.getIntValue(esMap, "id", 0);
1 package com.yoho.search.service.scene.common; 1 package com.yoho.search.service.scene.common;
2 2
3 -import java.util.ArrayList;  
4 -import java.util.HashMap;  
5 -import java.util.List;  
6 -import java.util.Map;  
7 -import java.util.concurrent.CompletableFuture;  
8 -import java.util.concurrent.ExecutorService;  
9 -import java.util.concurrent.Executors;  
10 -  
11 -import org.elasticsearch.index.query.BoolQueryBuilder;  
12 -import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;  
13 -import org.elasticsearch.search.aggregations.Aggregation;  
14 -import org.slf4j.Logger;  
15 -import org.slf4j.LoggerFactory;  
16 -import org.springframework.beans.factory.annotation.Autowired;  
17 -import org.springframework.stereotype.Service;  
18 -  
19 import com.alibaba.fastjson.JSONArray; 3 import com.alibaba.fastjson.JSONArray;
20 import com.alibaba.fastjson.JSONObject; 4 import com.alibaba.fastjson.JSONObject;
21 import com.yoho.search.base.utils.ISearchConstants; 5 import com.yoho.search.base.utils.ISearchConstants;
@@ -29,6 +13,21 @@ import com.yoho.search.service.aggregations.impls.AggregationFactoryService; @@ -29,6 +13,21 @@ import com.yoho.search.service.aggregations.impls.AggregationFactoryService;
29 import com.yoho.search.service.base.SearchCommonService; 13 import com.yoho.search.service.base.SearchCommonService;
30 import com.yoho.search.service.helper.SearchParamHelper; 14 import com.yoho.search.service.helper.SearchParamHelper;
31 import com.yoho.search.service.service.IAggRecommendService; 15 import com.yoho.search.service.service.IAggRecommendService;
  16 +import org.elasticsearch.index.query.BoolQueryBuilder;
  17 +import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
  18 +import org.elasticsearch.search.aggregations.Aggregation;
  19 +import org.slf4j.Logger;
  20 +import org.slf4j.LoggerFactory;
  21 +import org.springframework.beans.factory.annotation.Autowired;
  22 +import org.springframework.stereotype.Service;
  23 +
  24 +import java.util.ArrayList;
  25 +import java.util.HashMap;
  26 +import java.util.List;
  27 +import java.util.Map;
  28 +import java.util.concurrent.CompletableFuture;
  29 +import java.util.concurrent.ExecutorService;
  30 +import java.util.concurrent.Executors;
32 31
33 @Service 32 @Service
34 public class SceneSelectionsService extends AbstractCacheAbleService { 33 public class SceneSelectionsService extends AbstractCacheAbleService {
@@ -68,8 +67,8 @@ public class SceneSelectionsService extends AbstractCacheAbleService { @@ -68,8 +67,8 @@ public class SceneSelectionsService extends AbstractCacheAbleService {
68 67
69 public List<IAggregation> getCommonAggregations(Map<String, String> paramMap) { 68 public List<IAggregation> getCommonAggregations(Map<String, String> paramMap) {
70 List<IAggregation> aggregations = new ArrayList<>(); 69 List<IAggregation> aggregations = new ArrayList<>();
71 - // 1)促销  
72 - aggregations.add(aggregationFactoryService.getPromotionAggregation(1000)); 70 + // 1)专区名称
  71 + aggregations.add(aggregationFactoryService.getZqNameAggregation(1000));
73 // 2)全球购 72 // 2)全球购
74 aggregations.add(aggregationFactoryService.getIsGlobalAggregation()); 73 aggregations.add(aggregationFactoryService.getIsGlobalAggregation());
75 // 3)品牌 74 // 3)品牌
@@ -99,8 +98,8 @@ public class SceneSelectionsService extends AbstractCacheAbleService { @@ -99,8 +98,8 @@ public class SceneSelectionsService extends AbstractCacheAbleService {
99 */ 98 */
100 public List<IAggregation> getBreakSizeAggregations(Map<String, String> paramMap) { 99 public List<IAggregation> getBreakSizeAggregations(Map<String, String> paramMap) {
101 List<IAggregation> aggregations = new ArrayList<>(); 100 List<IAggregation> aggregations = new ArrayList<>();
102 - // 1)促销  
103 - aggregations.add(aggregationFactoryService.getPromotionAggregation(1000)); 101 + // 1)专区名称
  102 + aggregations.add(aggregationFactoryService.getZqNameAggregation(1000));
104 // 2)全球购 103 // 2)全球购
105 aggregations.add(aggregationFactoryService.getIsGlobalAggregation()); 104 aggregations.add(aggregationFactoryService.getIsGlobalAggregation());
106 // 3)品牌 105 // 3)品牌
1 package com.yoho.search.service.service; 1 package com.yoho.search.service.service;
2 2
3 -import java.util.Arrays;  
4 -import java.util.Map;  
5 -  
6 -import org.elasticsearch.search.aggregations.Aggregation;  
7 -import org.slf4j.Logger;  
8 -import org.slf4j.LoggerFactory;  
9 -import org.springframework.beans.factory.annotation.Autowired;  
10 -import org.springframework.context.ApplicationEventPublisher;  
11 -import org.springframework.context.ApplicationEventPublisherAware;  
12 -import org.springframework.stereotype.Service;  
13 -  
14 import com.alibaba.fastjson.JSONObject; 3 import com.alibaba.fastjson.JSONObject;
15 import com.yoho.error.event.SearchEvent; 4 import com.yoho.error.event.SearchEvent;
16 import com.yoho.search.base.utils.EventReportEnum; 5 import com.yoho.search.base.utils.EventReportEnum;
@@ -25,6 +14,16 @@ import com.yoho.search.service.base.SearchCommonService; @@ -25,6 +14,16 @@ import com.yoho.search.service.base.SearchCommonService;
25 import com.yoho.search.service.base.SearchRequestParams; 14 import com.yoho.search.service.base.SearchRequestParams;
26 import com.yoho.search.service.helper.SearchParamHelper; 15 import com.yoho.search.service.helper.SearchParamHelper;
27 import com.yoho.search.service.scene.common.AbstractCacheAbleService; 16 import com.yoho.search.service.scene.common.AbstractCacheAbleService;
  17 +import org.elasticsearch.search.aggregations.Aggregation;
  18 +import org.slf4j.Logger;
  19 +import org.slf4j.LoggerFactory;
  20 +import org.springframework.beans.factory.annotation.Autowired;
  21 +import org.springframework.context.ApplicationEventPublisher;
  22 +import org.springframework.context.ApplicationEventPublisherAware;
  23 +import org.springframework.stereotype.Service;
  24 +
  25 +import java.util.Arrays;
  26 +import java.util.Map;
28 27
29 @Service 28 @Service
30 public class AggregationService extends AbstractCacheAbleService implements ApplicationEventPublisherAware { 29 public class AggregationService extends AbstractCacheAbleService implements ApplicationEventPublisherAware {
@@ -232,14 +231,14 @@ public class AggregationService extends AbstractCacheAbleService implements Appl @@ -232,14 +231,14 @@ public class AggregationService extends AbstractCacheAbleService implements Appl
232 } 231 }
233 232
234 /** 233 /**
235 - * 获取促销层面的聚合结果 234 + * 获取专区名称面的聚合结果
236 */ 235 */
237 public JSONObject getPromotionAggregationResult(Map<String, String> paramMap, int aggCount) throws Exception { 236 public JSONObject getPromotionAggregationResult(Map<String, String> paramMap, int aggCount) throws Exception {
238 - IAggregation promotionAggregation = aggregationFactoryService.getPromotionAggregation(aggCount); 237 + IAggregation zqNameAggregation = aggregationFactoryService.getZqNameAggregation(aggCount);
239 String filterParamName = null; 238 String filterParamName = null;
240 Integer paramNum = Integer.valueOf(paramMap.getOrDefault(SearchRequestParams.PARAM_SEARCH_PARAMETPERNUM, "5")); 239 Integer paramNum = Integer.valueOf(paramMap.getOrDefault(SearchRequestParams.PARAM_SEARCH_PARAMETPERNUM, "5"));
241 - SearchParam searchParam = this.genSearchParamForAgg(promotionAggregation, paramMap, filterParamName, paramNum);  
242 - return this.getAggNameAndResponseWithCache(promotionAggregation, searchParam); 240 + SearchParam searchParam = this.genSearchParamForAgg(zqNameAggregation, paramMap, filterParamName, paramNum);
  241 + return this.getAggNameAndResponseWithCache(zqNameAggregation, searchParam);
243 } 242 }
244 243
245 /** 244 /**
1 package com.yoho.search.service.service.impl; 1 package com.yoho.search.service.service.impl;
2 2
3 -import java.util.ArrayList;  
4 -import java.util.HashMap;  
5 -import java.util.Iterator;  
6 -import java.util.LinkedHashMap;  
7 -import java.util.List;  
8 -import java.util.Map;  
9 -import java.util.Map.Entry;  
10 -  
11 -import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;  
12 -import org.elasticsearch.search.aggregations.Aggregation;  
13 -import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation;  
14 -import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket;  
15 -import org.springframework.beans.factory.annotation.Autowired;  
16 -import org.springframework.stereotype.Service;  
17 -  
18 import com.alibaba.fastjson.JSONArray; 3 import com.alibaba.fastjson.JSONArray;
19 import com.alibaba.fastjson.JSONObject; 4 import com.alibaba.fastjson.JSONObject;
20 import com.yoho.search.base.utils.ISearchConstants; 5 import com.yoho.search.base.utils.ISearchConstants;
@@ -34,6 +19,15 @@ import com.yoho.search.service.helper.SearchAfterCacheHelper; @@ -34,6 +19,15 @@ import com.yoho.search.service.helper.SearchAfterCacheHelper;
34 import com.yoho.search.service.helper.SearchParamHelper; 19 import com.yoho.search.service.helper.SearchParamHelper;
35 import com.yoho.search.service.scene.common.AbstractCacheAbleService; 20 import com.yoho.search.service.scene.common.AbstractCacheAbleService;
36 import com.yoho.search.service.service.ISelectionsForApp; 21 import com.yoho.search.service.service.ISelectionsForApp;
  22 +import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
  23 +import org.elasticsearch.search.aggregations.Aggregation;
  24 +import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation;
  25 +import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket;
  26 +import org.springframework.beans.factory.annotation.Autowired;
  27 +import org.springframework.stereotype.Service;
  28 +
  29 +import java.util.*;
  30 +import java.util.Map.Entry;
37 31
38 @Service 32 @Service
39 public class SelectionForAppImpl extends AbstractCacheAbleService implements ISelectionsForApp { 33 public class SelectionForAppImpl extends AbstractCacheAbleService implements ISelectionsForApp {
@@ -137,8 +131,8 @@ public class SelectionForAppImpl extends AbstractCacheAbleService implements ISe @@ -137,8 +131,8 @@ public class SelectionForAppImpl extends AbstractCacheAbleService implements ISe
137 // 12)全球购 131 // 12)全球购
138 list.add(aggregationFactoryService.getIsGlobalAggregation().getBuilder()); 132 list.add(aggregationFactoryService.getIsGlobalAggregation().getBuilder());
139 133
140 - // 13)促销  
141 - list.add(aggregationFactoryService.getPromotionAggregation(1000).getBuilder()); 134 + // 13)专区名称
  135 + list.add(aggregationFactoryService.getZqNameAggregation(1000).getBuilder());
142 136
143 return list; 137 return list;
144 } 138 }
@@ -205,9 +199,9 @@ public class SelectionForAppImpl extends AbstractCacheAbleService implements ISe @@ -205,9 +199,9 @@ public class SelectionForAppImpl extends AbstractCacheAbleService implements ISe
205 filter.put("size", new JSONArray()); 199 filter.put("size", new JSONArray());
206 } 200 }
207 201
208 - // 8)获取促销聚合结果[促销不会提前聚合]  
209 - IAggregation promotionAggregation = aggregationFactoryService.getPromotionAggregation(1000);  
210 - Object promotionResponse = promotionAggregation.getAggregationResponseMap(aggMaps); 202 + // 8)获取专区名称聚合结果[促销不会提前聚合]
  203 + IAggregation zqNameAggregation = aggregationFactoryService.getZqNameAggregation(1000);
  204 + Object promotionResponse = zqNameAggregation.getAggregationResponseMap(aggMaps);
211 if (promotionResponse != null) { 205 if (promotionResponse != null) {
212 filter.put("promotion", promotionResponse); 206 filter.put("promotion", promotionResponse);
213 } else { 207 } else {