Authored by hugufei

DiscountService使用AbstractPageComponent重构

@@ -35,6 +35,8 @@ public class CacheTimeConstants { @@ -35,6 +35,8 @@ public class CacheTimeConstants {
35 35
36 public static final int CACHE_60_MINUTE = 60; 36 public static final int CACHE_60_MINUTE = 60;
37 37
  38 + public static final int CACHE_30_MINUTE = 30;
  39 +
38 public static final int CACHE_10_MINUTE = 10; 40 public static final int CACHE_10_MINUTE = 10;
39 41
40 } 42 }
@@ -2,7 +2,9 @@ package com.yoho.search.service.helper; @@ -2,7 +2,9 @@ package com.yoho.search.service.helper;
2 2
3 import java.util.Map; 3 import java.util.Map;
4 4
  5 +import com.yoho.search.service.recall.models.common.ParamQueryFilter;
5 import org.elasticsearch.index.query.BoolQueryBuilder; 6 import org.elasticsearch.index.query.BoolQueryBuilder;
  7 +import org.elasticsearch.index.query.QueryBuilder;
6 import org.springframework.beans.factory.annotation.Autowired; 8 import org.springframework.beans.factory.annotation.Autowired;
7 import org.springframework.stereotype.Component; 9 import org.springframework.stereotype.Component;
8 10
@@ -35,22 +37,46 @@ public class SearchParamHelper { @@ -35,22 +37,46 @@ public class SearchParamHelper {
35 } 37 }
36 38
37 public SearchParam buildSearchParam(Map<String, String> paramMap, boolean needPersional, BoolQueryBuilder mustFilter, String filterParamName) throws Exception { 39 public SearchParam buildSearchParam(Map<String, String> paramMap, boolean needPersional, BoolQueryBuilder mustFilter, String filterParamName) throws Exception {
  40 + ParamQueryFilter paramQueryFilter = buildParamQueryFilter(paramMap,needPersional,mustFilter,filterParamName);
  41 + SearchParam searchParam = this.buildSearchParam(paramQueryFilter);
  42 + return searchParam;
  43 + }
  44 +
  45 + public SearchParam buildSearchParam(ParamQueryFilter paramQueryFilter){
38 SearchParam searchParam = new SearchParam(); 46 SearchParam searchParam = new SearchParam();
  47 + searchParam.setFiter(paramQueryFilter.getParamFilter());
  48 + searchParam.setQuery(paramQueryFilter.getParamQuery());
  49 + searchParam.setSize(0);
  50 + return searchParam;
  51 + }
  52 +
  53 + public ParamQueryFilter buildParamQueryFilter(Map<String, String> paramMap) throws Exception {
  54 + return buildParamQueryFilter(paramMap,false,null,null);
  55 + }
  56 +
  57 + public ParamQueryFilter buildParamQueryFilter(Map<String, String> paramMap,String filterParamName) throws Exception {
  58 + return buildParamQueryFilter(paramMap,false,null,filterParamName);
  59 + }
  60 +
  61 + public ParamQueryFilter buildParamQueryFilter(Map<String, String> paramMap,BoolQueryBuilder mustFilter) throws Exception {
  62 + return buildParamQueryFilter(paramMap,false,mustFilter,null);
  63 + }
  64 +
  65 + public ParamQueryFilter buildParamQueryFilter(Map<String, String> paramMap, boolean needPersional, BoolQueryBuilder mustFilter,String filterParamName) throws Exception {
39 // 1、构造filter 66 // 1、构造filter
40 BoolQueryBuilder filter = searchQueryHelper.constructFilterBuilder(paramMap, filterParamName); 67 BoolQueryBuilder filter = searchQueryHelper.constructFilterBuilder(paramMap, filterParamName);
41 if (mustFilter != null) { 68 if (mustFilter != null) {
42 filter.must(mustFilter); 69 filter.must(mustFilter);
43 } 70 }
44 - searchParam.setFiter(filter);  
45 // 2、构造query 71 // 2、构造query
  72 + QueryBuilder queryBuilder;
46 if (needPersional) { 73 if (needPersional) {
47 - searchParam.setQuery(searchQueryHelper.constructQueryBuilderPersional(paramMap, filter)); 74 + queryBuilder = searchQueryHelper.constructQueryBuilderPersional(paramMap, filter);
48 } else { 75 } else {
49 - searchParam.setQuery(searchQueryHelper.constructQueryBuilder(paramMap)); 76 + queryBuilder = searchQueryHelper.constructQueryBuilder(paramMap);
50 } 77 }
51 - // 3、默认设置size为0  
52 - searchParam.setSize(0);  
53 - return searchParam; 78 + return new ParamQueryFilter(queryBuilder,filter);
54 } 79 }
55 80
  81 +
56 } 82 }
1 package com.yoho.search.service.scene.general; 1 package com.yoho.search.service.scene.general;
2 2
3 import com.alibaba.fastjson.JSONObject; 3 import com.alibaba.fastjson.JSONObject;
  4 +import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
4 import com.yoho.error.event.SearchEvent; 5 import com.yoho.error.event.SearchEvent;
5 -import com.yoho.search.aop.cache.SearchCacheAble;  
6 import com.yoho.search.base.utils.EventReportEnum; 6 import com.yoho.search.base.utils.EventReportEnum;
7 import com.yoho.search.base.utils.ISearchConstants; 7 import com.yoho.search.base.utils.ISearchConstants;
  8 +import com.yoho.search.cache.CacheTimeConstants;
  9 +import com.yoho.search.cache.beans.AbstractPageComponent;
8 import com.yoho.search.common.SearchCommonService; 10 import com.yoho.search.common.SearchCommonService;
9 -import com.yoho.search.common.utils.SearchApiResultUtils;  
10 import com.yoho.search.core.es.agg.IAggregation; 11 import com.yoho.search.core.es.agg.IAggregation;
11 import com.yoho.search.core.es.model.SearchParam; 12 import com.yoho.search.core.es.model.SearchParam;
12 import com.yoho.search.core.es.model.SearchResult; 13 import com.yoho.search.core.es.model.SearchResult;
@@ -14,6 +15,7 @@ import com.yoho.search.core.es.utils.IgnoreSomeException; @@ -14,6 +15,7 @@ import com.yoho.search.core.es.utils.IgnoreSomeException;
14 import com.yoho.search.models.SearchApiResult; 15 import com.yoho.search.models.SearchApiResult;
15 import com.yoho.search.service.aggregations.impls.AggregationFactory; 16 import com.yoho.search.service.aggregations.impls.AggregationFactory;
16 import com.yoho.search.service.helper.SearchParamHelper; 17 import com.yoho.search.service.helper.SearchParamHelper;
  18 +import com.yoho.search.service.recall.models.common.ParamQueryFilter;
17 import org.elasticsearch.index.query.BoolQueryBuilder; 19 import org.elasticsearch.index.query.BoolQueryBuilder;
18 import org.slf4j.Logger; 20 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory; 21 import org.slf4j.LoggerFactory;
@@ -26,88 +28,74 @@ import java.util.Arrays; @@ -26,88 +28,74 @@ import java.util.Arrays;
26 import java.util.Map; 28 import java.util.Map;
27 29
28 @Service 30 @Service
29 -public class DiscountService implements ApplicationEventPublisherAware {  
30 -  
31 - private static final Logger logger = LoggerFactory.getLogger(DiscountService.class);  
32 -  
33 - @Autowired  
34 - private SearchParamHelper searchParamHelper;  
35 - @Autowired  
36 - private AggregationFactory aggregationFactory;  
37 - @Autowired  
38 - private SearchCommonService searchCommonService;  
39 -  
40 - private ApplicationEventPublisher publisher;  
41 -  
42 - @Override  
43 - public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {  
44 - this.publisher = applicationEventPublisher;  
45 - }  
46 -  
47 - @SearchCacheAble(cacheInMinute = 30, cacheName = "DISCOUNT", excludeParams = {"client_type", "yh_channel", "order", "uid", "udid"})  
48 - public SearchApiResult discount(Map<String, String> paramMap) {  
49 - try {  
50 - // 构造查询参数  
51 - SearchParam searchParam = searchParamHelper.buildDefault(paramMap);  
52 -  
53 - // 按1-3,4-6,7-10折的规则进行聚合[按区间聚合]  
54 - IAggregation discountAggregation = aggregationFactory.getDiscountAggregation();  
55 - searchParam.setAggregationBuilders(Arrays.asList(discountAggregation.getBuilder()));  
56 - searchParam.setSize(0);  
57 -  
58 - // 构造返回结果  
59 - SearchApiResult searchApiResult = new SearchApiResult().setMessage("discount List.");  
60 -  
61 - // es检索  
62 - final String indexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;  
63 - SearchResult searchResult = searchCommonService.doSearch(indexName, searchParam);  
64 - if (searchResult == null || searchResult.getAggMaps() == null) {  
65 - return searchApiResult.setData("");  
66 - }  
67 - Object discountAggResult = discountAggregation.getAggregationResponseMap(searchResult.getAggMaps());  
68 - if (discountAggResult == null) {  
69 - return searchApiResult.setData("");  
70 - }  
71 - JSONObject discountObject = new JSONObject();  
72 - discountObject.put("discount", discountAggResult);  
73 - return searchApiResult.setData(discountObject);  
74 - } catch (Exception e) {  
75 - publisher.publishEvent(new SearchEvent(EventReportEnum.SEARCHCONTROLLER_DISCOUNT.getEventName(), EventReportEnum.SEARCHCONTROLLER_DISCOUNT.getFunctionName(),  
76 - EventReportEnum.SEARCHCONTROLLER_DISCOUNT.getMoudleName(), "exception", IgnoreSomeException.filterSomeException(e), null));  
77 - return SearchApiResultUtils.errorSearchApiResult("discount", paramMap, e);  
78 - }  
79 - }  
80 -  
81 - public JSONObject discount(Map<String, String> paramMap, BoolQueryBuilder mustFilter) {  
82 - try {  
83 - logger.info("[func=discount][param={}][begin={}]", paramMap.toString(), System.currentTimeMillis());  
84 -  
85 - // 构造查询参数  
86 - SearchParam searchParam = searchParamHelper.buildWithMustFilter(paramMap, mustFilter);  
87 -  
88 - // 按1-3,4-6,7-10折的规则进行聚合[按区间聚合]  
89 - IAggregation discountAggregation = aggregationFactory.getDiscountAggregation();  
90 - searchParam.setAggregationBuilders(Arrays.asList(discountAggregation.getBuilder()));  
91 - searchParam.setSize(0);  
92 -  
93 - // es检索  
94 - final String indexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;  
95 - SearchResult searchResult = searchCommonService.doSearch(indexName, searchParam);  
96 - if (searchResult == null || searchResult.getAggMaps() == null) {  
97 - return null;  
98 - }  
99 - Object discountAggResult = discountAggregation.getAggregationResponseMap(searchResult.getAggMaps());  
100 - if (discountAggResult == null) {  
101 - return null;  
102 - }  
103 - JSONObject discountObject = new JSONObject();  
104 - discountObject.put("discount", discountAggResult);  
105 - return discountObject.getJSONObject("discount");  
106 - } catch (Exception e) {  
107 - publisher.publishEvent(new SearchEvent(EventReportEnum.SEARCHCONTROLLER_DISCOUNT.getEventName(), EventReportEnum.SEARCHCONTROLLER_DISCOUNT.getFunctionName(),  
108 - EventReportEnum.SEARCHCONTROLLER_DISCOUNT.getMoudleName(), "exception", IgnoreSomeException.filterSomeException(e), null));  
109 - return null;  
110 - }  
111 - }  
112 - 31 +public class DiscountService extends AbstractPageComponent<JSONObject> implements ApplicationEventPublisherAware {
  32 +
  33 + private static final Logger logger = LoggerFactory.getLogger(DiscountService.class);
  34 +
  35 + @Autowired
  36 + private SearchParamHelper searchParamHelper;
  37 + @Autowired
  38 + private AggregationFactory aggregationFactory;
  39 + @Autowired
  40 + private SearchCommonService searchCommonService;
  41 +
  42 + private ApplicationEventPublisher publisher;
  43 +
  44 + @Override
  45 + public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
  46 + this.publisher = applicationEventPublisher;
  47 + }
  48 +
  49 + public SearchApiResult discount(Map<String, String> paramMap) {
  50 + JSONObject discountObject = discount(paramMap, null);
  51 + return new SearchApiResult().setMessage("discount List.").setData(discountObject);
  52 + }
  53 +
  54 + public JSONObject discount(Map<String, String> paramMap, BoolQueryBuilder mustFilter) {
  55 + try {
  56 + ParamQueryFilter paramQueryFilter = searchParamHelper.buildParamQueryFilter(paramMap, mustFilter);
  57 + Object value = super.queryWithCache(paramQueryFilter, JSONObject.class);
  58 + return value == null ? null : (JSONObject) value;
  59 + } catch (Exception e) {
  60 + logger.error(e.getMessage(), e);
  61 + publisher.publishEvent(new SearchEvent(EventReportEnum.SEARCHCONTROLLER_DISCOUNT.getEventName(), EventReportEnum.SEARCHCONTROLLER_DISCOUNT.getFunctionName(),
  62 + EventReportEnum.SEARCHCONTROLLER_DISCOUNT.getMoudleName(), "exception", IgnoreSomeException.filterSomeException(e), null));
  63 + return null;
  64 + }
  65 + }
  66 +
  67 + @Override
  68 + protected RedisKeyBuilder genRedisKeyBuilder(ParamQueryFilter paramQueryFilter) {
  69 + return RedisKeyBuilder.newInstance().appendFixed("YOHOSEARCH:").appendFixed("DISCOUNT:").appendVar(paramQueryFilter.getParamMd5Key());
  70 + }
  71 +
  72 + @Override
  73 + protected int cacheTimeInMinute() {
  74 + return CacheTimeConstants.CACHE_30_MINUTE;
  75 + }
  76 +
  77 + @Override
  78 + protected JSONObject doRealQuery(ParamQueryFilter paramQueryFilter) {
  79 + // 构造查询参数
  80 + SearchParam searchParam = searchParamHelper.buildSearchParam(paramQueryFilter);
  81 +
  82 + // 按1-3,4-6,7-10折的规则进行聚合[按区间聚合]
  83 + IAggregation discountAggregation = aggregationFactory.getDiscountAggregation();
  84 + searchParam.setAggregationBuilders(Arrays.asList(discountAggregation.getBuilder()));
  85 + searchParam.setSize(0);
  86 +
  87 + // es检索
  88 + final String indexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
  89 + SearchResult searchResult = searchCommonService.doSearch(indexName, searchParam);
  90 + if (searchResult == null || searchResult.getAggMaps() == null) {
  91 + return null;
  92 + }
  93 + Object discountAggResult = discountAggregation.getAggregationResponseMap(searchResult.getAggMaps());
  94 + if (discountAggResult == null) {
  95 + return null;
  96 + }
  97 + JSONObject discountObject = new JSONObject();
  98 + discountObject.put("discount", discountAggResult);
  99 + return discountObject.getJSONObject("discount");
  100 + }
113 } 101 }