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
|
} |