Showing
7 changed files
with
380 additions
and
158 deletions
1 | package com.yoho.search.common; | 1 | package com.yoho.search.common; |
2 | 2 | ||
3 | import com.yoho.search.base.utils.ConvertUtils; | 3 | import com.yoho.search.base.utils.ConvertUtils; |
4 | +import org.apache.commons.collections.CollectionUtils; | ||
4 | import org.apache.commons.lang.StringUtils; | 5 | import org.apache.commons.lang.StringUtils; |
5 | import org.elasticsearch.index.query.BoolQueryBuilder; | 6 | import org.elasticsearch.index.query.BoolQueryBuilder; |
6 | import org.elasticsearch.index.query.QueryBuilders; | 7 | import org.elasticsearch.index.query.QueryBuilders; |
@@ -16,25 +17,37 @@ public class BaseService { | @@ -16,25 +17,37 @@ public class BaseService { | ||
16 | /** | 17 | /** |
17 | * 增加一个termsQuery | 18 | * 增加一个termsQuery |
18 | */ | 19 | */ |
19 | - protected BoolQueryBuilder boolQueryTermsQuery(Map<String, String> paramMap, BoolQueryBuilder boolFilter, String fieldName) { | 20 | + protected void boolQueryTermsQuery(Map<String, String> paramMap, BoolQueryBuilder boolFilter, String fieldName) { |
20 | if (paramMap.containsKey(fieldName) && StringUtils.isNotBlank(paramMap.get(fieldName))) { | 21 | if (paramMap.containsKey(fieldName) && StringUtils.isNotBlank(paramMap.get(fieldName))) { |
21 | List<Integer> values = ConvertUtils.stringToIntList(paramMap.get(fieldName), ","); | 22 | List<Integer> values = ConvertUtils.stringToIntList(paramMap.get(fieldName), ","); |
22 | if (values == null || values.isEmpty()) { | 23 | if (values == null || values.isEmpty()) { |
23 | - return boolFilter; | 24 | + return; |
24 | } | 25 | } |
25 | boolFilter.must(QueryBuilders.termsQuery(fieldName, values)); | 26 | boolFilter.must(QueryBuilders.termsQuery(fieldName, values)); |
26 | } | 27 | } |
27 | - return boolFilter; | ||
28 | } | 28 | } |
29 | 29 | ||
30 | /** | 30 | /** |
31 | * 增加一个termQuery | 31 | * 增加一个termQuery |
32 | */ | 32 | */ |
33 | - protected BoolQueryBuilder boolQueryTermQuery(Map<String, String> paramMap, BoolQueryBuilder boolFilter, String fieldName) { | 33 | + protected void boolQueryTermQuery(Map<String, String> paramMap, BoolQueryBuilder boolFilter, String fieldName) { |
34 | if (paramMap.containsKey(fieldName) && StringUtils.isNotBlank(paramMap.get(fieldName))) { | 34 | if (paramMap.containsKey(fieldName) && StringUtils.isNotBlank(paramMap.get(fieldName))) { |
35 | boolFilter.must(QueryBuilders.termQuery(fieldName, paramMap.get(fieldName))); | 35 | boolFilter.must(QueryBuilders.termQuery(fieldName, paramMap.get(fieldName))); |
36 | } | 36 | } |
37 | - return boolFilter; | 37 | + } |
38 | + | ||
39 | + /** | ||
40 | + * 批量增加termQuery | ||
41 | + */ | ||
42 | + protected void addTermQueryBatch(Map<String, String> paramMap, BoolQueryBuilder boolFilter, List<String> paramsList) { | ||
43 | + if (CollectionUtils.isEmpty(paramsList)) { | ||
44 | + return; | ||
45 | + } | ||
46 | + paramsList.stream().forEach(p -> { | ||
47 | + if (paramMap.containsKey(p) && StringUtils.isNotBlank(paramMap.get(p))) { | ||
48 | + boolFilter.must(QueryBuilders.termQuery(p, paramMap.get(p))); | ||
49 | + } | ||
50 | + }); | ||
38 | } | 51 | } |
39 | 52 | ||
40 | /** | 53 | /** |
@@ -131,6 +131,22 @@ public class SearchRequestParams { | @@ -131,6 +131,22 @@ public class SearchRequestParams { | ||
131 | public static final String IMAGEREPERTORY_PARAM_ITEMTYPE = "itemType"; | 131 | public static final String IMAGEREPERTORY_PARAM_ITEMTYPE = "itemType"; |
132 | public static final String IMAGEREPERTORY_PARAM_PORT = "port"; | 132 | public static final String IMAGEREPERTORY_PARAM_PORT = "port"; |
133 | 133 | ||
134 | + //Collage index | ||
135 | + public static final String COLLAGE_PARAM_ACTIVITYID = "activityId"; | ||
136 | + public static final String COLLAGE_PARAM_PRODUCTSKN = "productSkn"; | ||
137 | + public static final String COLLAGE_PARAM_ORDERBY = "orderBy"; | ||
138 | + public static final String COLLAGE_PARAM_OPENLIMIT = "openLimit"; | ||
139 | + public static final String COLLAGE_PARAM_JOINLIMIT = "joinLimit"; | ||
140 | + public static final String COLLAGE_PARAM_ISAUTO = "isAuto"; | ||
141 | + public static final String COLLAGE_PARAM_PAYLIMIT = "payLimit"; | ||
142 | + public static final String COLLAGE_PARAM_PRODUCTLIMIT = "productLimit"; | ||
143 | + public static final String COLLAGE_PARAM_STATUS = "status"; | ||
144 | + public static final String COLLAGE_PARAM_ACTIVITYSTATUS = "activityStatus"; | ||
145 | + public static final String COLLAGE_PARAM_BEGINTIME = "beginTime"; | ||
146 | + public static final String COLLAGE_PARAM_ENDTIME = "endTime"; | ||
147 | + | ||
148 | + | ||
149 | + | ||
134 | 150 | ||
135 | 151 | ||
136 | public static final String SHOPS_PARAM_CUSTOMIZE_TAG = "customize_tag"; | 152 | public static final String SHOPS_PARAM_CUSTOMIZE_TAG = "customize_tag"; |
1 | +package com.yoho.search.restapi.others; | ||
2 | + | ||
3 | +import com.yoho.search.aop.cache.SearchCacheAble; | ||
4 | +import com.yoho.search.common.utils.HttpServletRequestUtils; | ||
5 | +import com.yoho.search.models.SearchApiResult; | ||
6 | +import com.yoho.search.service.scene.others.CollageService; | ||
7 | +import org.springframework.beans.factory.annotation.Autowired; | ||
8 | +import org.springframework.stereotype.Controller; | ||
9 | +import org.springframework.web.bind.annotation.RequestMapping; | ||
10 | +import org.springframework.web.bind.annotation.RequestMethod; | ||
11 | +import org.springframework.web.bind.annotation.ResponseBody; | ||
12 | + | ||
13 | +import javax.servlet.http.HttpServletRequest; | ||
14 | +import java.util.Map; | ||
15 | + | ||
16 | +/** | ||
17 | + * @author wangnan | ||
18 | + * @version 2018/9/4 | ||
19 | + */ | ||
20 | +@Controller | ||
21 | +public class CollageController { | ||
22 | + | ||
23 | + @Autowired | ||
24 | + private CollageService collageService; | ||
25 | + | ||
26 | + /** | ||
27 | + * 搜索拼团购 | ||
28 | + */ | ||
29 | + @SearchCacheAble(cacheInMinute = 1, cacheName = "COLLAGE") | ||
30 | + @RequestMapping(method = RequestMethod.GET, value = "/collage") | ||
31 | + @ResponseBody | ||
32 | + public SearchApiResult collage(HttpServletRequest request) { | ||
33 | + Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request); | ||
34 | + return collageService.searchList(paramMap); | ||
35 | + } | ||
36 | +} |
1 | package com.yoho.search.service.index; | 1 | package com.yoho.search.service.index; |
2 | 2 | ||
3 | -import java.util.ArrayList; | ||
4 | -import java.util.Collection; | ||
5 | -import java.util.HashMap; | ||
6 | -import java.util.List; | ||
7 | -import java.util.Map; | ||
8 | - | 3 | +import com.yoho.search.base.utils.ConvertUtils; |
4 | +import com.yoho.search.base.utils.ISearchConstants; | ||
5 | +import com.yoho.search.common.BaseService; | ||
6 | +import com.yoho.search.common.SearchCommonService; | ||
7 | +import com.yoho.search.common.SearchRequestParams; | ||
8 | +import com.yoho.search.core.es.model.SearchParam; | ||
9 | +import com.yoho.search.core.es.model.SearchResult; | ||
9 | import org.apache.commons.collections.MapUtils; | 10 | import org.apache.commons.collections.MapUtils; |
10 | import org.apache.commons.lang.StringUtils; | 11 | import org.apache.commons.lang.StringUtils; |
11 | import org.elasticsearch.index.query.BoolQueryBuilder; | 12 | import org.elasticsearch.index.query.BoolQueryBuilder; |
@@ -15,128 +16,124 @@ import org.slf4j.LoggerFactory; | @@ -15,128 +16,124 @@ import org.slf4j.LoggerFactory; | ||
15 | import org.springframework.beans.factory.annotation.Autowired; | 16 | import org.springframework.beans.factory.annotation.Autowired; |
16 | import org.springframework.stereotype.Service; | 17 | import org.springframework.stereotype.Service; |
17 | 18 | ||
18 | -import com.yoho.search.base.utils.ConvertUtils; | ||
19 | -import com.yoho.search.base.utils.ISearchConstants; | ||
20 | -import com.yoho.search.core.es.model.SearchParam; | ||
21 | -import com.yoho.search.core.es.model.SearchResult; | ||
22 | -import com.yoho.search.common.SearchCommonService; | ||
23 | -import com.yoho.search.common.SearchRequestParams; | ||
24 | -import com.yoho.search.common.BaseService; | 19 | +import java.util.*; |
25 | 20 | ||
26 | @Service | 21 | @Service |
27 | public class ShopsIndexBaseService extends BaseService { | 22 | public class ShopsIndexBaseService extends BaseService { |
28 | 23 | ||
29 | - private static final Logger logger = LoggerFactory.getLogger(ShopsIndexBaseService.class); | ||
30 | - | ||
31 | - @Autowired | ||
32 | - private SearchCommonService searchCommonService; | ||
33 | - | ||
34 | - private static final String SHOP_INDEX_NAME = ISearchConstants.INDEX_NAME_SHOPS; | ||
35 | - | ||
36 | - public Map<String, Object> getShopMap(Map<String, Object> esMap) { | ||
37 | - Map<String, Object> map = new HashMap<String, Object>(); | ||
38 | - map.put("shop_id", MapUtils.getIntValue(esMap, "shopsId", 0)); | ||
39 | - map.put("shop_name", MapUtils.getString(esMap, "shopName", "")); | ||
40 | - map.put("shop_logo", MapUtils.getString(esMap, "shopLogo", "")); | ||
41 | - map.put("shop_domain", MapUtils.getString(esMap, "shopDomain", "")); | ||
42 | - map.put("shop_type", MapUtils.getIntValue(esMap, "shopsType", 0)); | ||
43 | - map.put("decorator_template_type", MapUtils.getIntValue(esMap, "decoratorTemplateType", 0)); | ||
44 | - map.put("status", MapUtils.getIntValue(esMap, "status", 0)); | ||
45 | - map.put("blk_status", MapUtils.getIntValue(esMap, "blkStatus", 0)); | ||
46 | - map.put("check_status", MapUtils.getIntValue(esMap, "checkStatus", 0)); | ||
47 | - map.put("search_show_image", MapUtils.getString(esMap, "searchShowImage", "")); | ||
48 | - map.put("shop_intro", MapUtils.getString(esMap, "shopIntro", "品牌官方授权")); | ||
49 | - return map; | ||
50 | - } | ||
51 | - | ||
52 | - public List<Map<String, Object>> getShopsListByIds(Collection<?> shopIds) { | ||
53 | - List<Map<String, Object>> shopResults = new ArrayList<Map<String, Object>>(); | ||
54 | - try { | ||
55 | - List<Map<String, Object>> multiGetResults = searchCommonService.doMultiGetCommon(SHOP_INDEX_NAME, shopIds); | ||
56 | - for (Map<String, Object> esMap : multiGetResults) { | ||
57 | - shopResults.add(this.getShopMap(esMap)); | ||
58 | - } | ||
59 | - } catch (Exception e) { | ||
60 | - logger.error(e.getMessage(), e); | ||
61 | - } | ||
62 | - return shopResults; | ||
63 | - } | ||
64 | - | ||
65 | - public Map<String, Map<String, Object>> getShopsMapByIds(Collection<?> shopIds) { | ||
66 | - List<Map<String, Object>> shopList = this.getShopsListByIds(shopIds); | ||
67 | - Map<String, Map<String, Object>> results = new HashMap<String, Map<String, Object>>(); | ||
68 | - for (Map<String, Object> shop : shopList) { | ||
69 | - results.put(shop.getOrDefault("shop_id", 0).toString(), shop); | ||
70 | - } | ||
71 | - return results; | ||
72 | - } | ||
73 | - | ||
74 | - public List<Map<String, Object>> getShopListByIdsWithSortAndStatus(Collection<?> shopIds) { | ||
75 | - Map<String, Map<String, Object>> shopMap = this.getShopsMapByIds(shopIds); | ||
76 | - List<Map<String, Object>> results = new ArrayList<Map<String, Object>>(); | ||
77 | - for (Object shopId : shopIds) { | ||
78 | - Map<String, Object> shop = shopMap.get(shopId.toString()); | ||
79 | - if (shop != null && MapUtils.getIntValue(shop, "status", 0)==1) { | ||
80 | - results.add(shop); | ||
81 | - } | ||
82 | - } | ||
83 | - return results; | ||
84 | - } | ||
85 | - | ||
86 | - private List<Map<String, Object>> queryShopListByParam(SearchParam searchParam) { | ||
87 | - List<Map<String, Object>> results = new ArrayList<Map<String, Object>>(); | ||
88 | - SearchResult searchResult = searchCommonService.doSearch(SHOP_INDEX_NAME, searchParam); | ||
89 | - if (searchResult == null || searchResult.getResultList() == null || searchResult.getResultList().isEmpty()) { | ||
90 | - return results; | ||
91 | - } | ||
92 | - for (Map<String, Object> esMap : searchResult.getResultList()) { | ||
93 | - results.add(this.getShopMap(esMap)); | ||
94 | - } | ||
95 | - return results; | ||
96 | - } | ||
97 | - | ||
98 | - // 根据参数构造filter | ||
99 | - private BoolQueryBuilder constructShopsFilterBuilder(Map<String, String> paramMap) { | ||
100 | - BoolQueryBuilder boolFilter = QueryBuilders.boolQuery(); | ||
101 | - if (paramMap.containsKey(SearchRequestParams.PARAM_SEARCH_SHOP) && StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_SHOP))) { | ||
102 | - List<Integer> shopsIds = ConvertUtils.stringToIntList(paramMap.get(SearchRequestParams.PARAM_SEARCH_SHOP), ","); | ||
103 | - boolFilter.must(QueryBuilders.termsQuery("shopsId", shopsIds)); | ||
104 | - } | ||
105 | - boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.SHOPS_PARAM_BLKSTATUS); | ||
106 | - boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.SHOPS_PARAM_CHECKSTATUS); | ||
107 | - boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.SHOPS_PARAM_EXAMINESTATUS); | ||
108 | - boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.SHOPS_PARAM_OPERATIONSTATUS); | ||
109 | - boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.SHOPS_PARAM_SHOPSTYPE); | ||
110 | - boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.SHOPS_PARAM_STATUS); | ||
111 | - boolFilter = generalHandler(paramMap, boolFilter); | ||
112 | - return boolFilter; | ||
113 | - } | ||
114 | - | ||
115 | - public List<Map<String, Object>> queryShopListByParam(Map<String, String> paramMap) { | ||
116 | - try { | ||
117 | - // 1、构建SearchParam | ||
118 | - SearchParam searchParam = new SearchParam(); | ||
119 | - BoolQueryBuilder boolFilterForShops = this.constructShopsFilterBuilder(paramMap); | ||
120 | - searchParam.setFiter(boolFilterForShops); | ||
121 | - searchParam.setSize(10000); | ||
122 | - searchParam.setQuery(QueryBuilders.matchAllQuery()); | ||
123 | - | ||
124 | - // 2、调搜索,并将结果加入缓存 | ||
125 | - List<Map<String, Object>> shopsList = this.queryShopListByParam(searchParam); | ||
126 | - return shopsList; | ||
127 | - } catch (Exception e) { | ||
128 | - logger.error(e.getMessage(), e); | ||
129 | - return new ArrayList<Map<String, Object>>(); | ||
130 | - } | ||
131 | - } | ||
132 | - | ||
133 | - public Map<String, Map<String, Object>> queryShopMapByParam(Map<String, String> paramMap) { | ||
134 | - List<Map<String, Object>> shopList = this.queryShopListByParam(paramMap); | ||
135 | - Map<String, Map<String, Object>> shopInfoMap = new HashMap<String, Map<String, Object>>(); | ||
136 | - for (Map<String, Object> shopInfo : shopList) { | ||
137 | - shopInfoMap.put(shopInfo.getOrDefault("shop_id", 0).toString(), shopInfo); | ||
138 | - } | ||
139 | - return shopInfoMap; | ||
140 | - } | 24 | + private static final Logger logger = LoggerFactory.getLogger(ShopsIndexBaseService.class); |
25 | + | ||
26 | + private static final List<String> paramsList = Arrays.asList(SearchRequestParams.SHOPS_PARAM_BLKSTATUS, | ||
27 | + SearchRequestParams.SHOPS_PARAM_CHECKSTATUS, | ||
28 | + SearchRequestParams.SHOPS_PARAM_EXAMINESTATUS, | ||
29 | + SearchRequestParams.SHOPS_PARAM_OPERATIONSTATUS, | ||
30 | + SearchRequestParams.SHOPS_PARAM_SHOPSTYPE, | ||
31 | + SearchRequestParams.SHOPS_PARAM_STATUS); | ||
32 | + | ||
33 | + @Autowired | ||
34 | + private SearchCommonService searchCommonService; | ||
35 | + | ||
36 | + private static final String SHOP_INDEX_NAME = ISearchConstants.INDEX_NAME_SHOPS; | ||
37 | + | ||
38 | + public Map<String, Object> getShopMap(Map<String, Object> esMap) { | ||
39 | + Map<String, Object> map = new HashMap<String, Object>(); | ||
40 | + map.put("shop_id", MapUtils.getIntValue(esMap, "shopsId", 0)); | ||
41 | + map.put("shop_name", MapUtils.getString(esMap, "shopName", "")); | ||
42 | + map.put("shop_logo", MapUtils.getString(esMap, "shopLogo", "")); | ||
43 | + map.put("shop_domain", MapUtils.getString(esMap, "shopDomain", "")); | ||
44 | + map.put("shop_type", MapUtils.getIntValue(esMap, "shopsType", 0)); | ||
45 | + map.put("decorator_template_type", MapUtils.getIntValue(esMap, "decoratorTemplateType", 0)); | ||
46 | + map.put("status", MapUtils.getIntValue(esMap, "status", 0)); | ||
47 | + map.put("blk_status", MapUtils.getIntValue(esMap, "blkStatus", 0)); | ||
48 | + map.put("check_status", MapUtils.getIntValue(esMap, "checkStatus", 0)); | ||
49 | + map.put("search_show_image", MapUtils.getString(esMap, "searchShowImage", "")); | ||
50 | + map.put("shop_intro", MapUtils.getString(esMap, "shopIntro", "品牌官方授权")); | ||
51 | + return map; | ||
52 | + } | ||
53 | + | ||
54 | + public List<Map<String, Object>> getShopsListByIds(Collection<?> shopIds) { | ||
55 | + List<Map<String, Object>> shopResults = new ArrayList<Map<String, Object>>(); | ||
56 | + try { | ||
57 | + List<Map<String, Object>> multiGetResults = searchCommonService.doMultiGetCommon(SHOP_INDEX_NAME, shopIds); | ||
58 | + for (Map<String, Object> esMap : multiGetResults) { | ||
59 | + shopResults.add(this.getShopMap(esMap)); | ||
60 | + } | ||
61 | + } catch (Exception e) { | ||
62 | + logger.error(e.getMessage(), e); | ||
63 | + } | ||
64 | + return shopResults; | ||
65 | + } | ||
66 | + | ||
67 | + public Map<String, Map<String, Object>> getShopsMapByIds(Collection<?> shopIds) { | ||
68 | + List<Map<String, Object>> shopList = this.getShopsListByIds(shopIds); | ||
69 | + Map<String, Map<String, Object>> results = new HashMap<String, Map<String, Object>>(); | ||
70 | + for (Map<String, Object> shop : shopList) { | ||
71 | + results.put(shop.getOrDefault("shop_id", 0).toString(), shop); | ||
72 | + } | ||
73 | + return results; | ||
74 | + } | ||
75 | + | ||
76 | + public List<Map<String, Object>> getShopListByIdsWithSortAndStatus(Collection<?> shopIds) { | ||
77 | + Map<String, Map<String, Object>> shopMap = this.getShopsMapByIds(shopIds); | ||
78 | + List<Map<String, Object>> results = new ArrayList<Map<String, Object>>(); | ||
79 | + for (Object shopId : shopIds) { | ||
80 | + Map<String, Object> shop = shopMap.get(shopId.toString()); | ||
81 | + if (shop != null && MapUtils.getIntValue(shop, "status", 0) == 1) { | ||
82 | + results.add(shop); | ||
83 | + } | ||
84 | + } | ||
85 | + return results; | ||
86 | + } | ||
87 | + | ||
88 | + private List<Map<String, Object>> queryShopListByParam(SearchParam searchParam) { | ||
89 | + List<Map<String, Object>> results = new ArrayList<Map<String, Object>>(); | ||
90 | + SearchResult searchResult = searchCommonService.doSearch(SHOP_INDEX_NAME, searchParam); | ||
91 | + if (searchResult == null || searchResult.getResultList() == null || searchResult.getResultList().isEmpty()) { | ||
92 | + return results; | ||
93 | + } | ||
94 | + for (Map<String, Object> esMap : searchResult.getResultList()) { | ||
95 | + results.add(this.getShopMap(esMap)); | ||
96 | + } | ||
97 | + return results; | ||
98 | + } | ||
99 | + | ||
100 | + // 根据参数构造filter | ||
101 | + private BoolQueryBuilder constructShopsFilterBuilder(Map<String, String> paramMap) { | ||
102 | + BoolQueryBuilder boolFilter = QueryBuilders.boolQuery(); | ||
103 | + if (paramMap.containsKey(SearchRequestParams.PARAM_SEARCH_SHOP) && StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_SHOP))) { | ||
104 | + List<Integer> shopsIds = ConvertUtils.stringToIntList(paramMap.get(SearchRequestParams.PARAM_SEARCH_SHOP), ","); | ||
105 | + boolFilter.must(QueryBuilders.termsQuery("shopsId", shopsIds)); | ||
106 | + } | ||
107 | + addTermQueryBatch(paramMap, boolFilter, paramsList); | ||
108 | + boolFilter = generalHandler(paramMap, boolFilter); | ||
109 | + return boolFilter; | ||
110 | + } | ||
111 | + | ||
112 | + public List<Map<String, Object>> queryShopListByParam(Map<String, String> paramMap) { | ||
113 | + try { | ||
114 | + // 1、构建SearchParam | ||
115 | + SearchParam searchParam = new SearchParam(); | ||
116 | + BoolQueryBuilder boolFilterForShops = this.constructShopsFilterBuilder(paramMap); | ||
117 | + searchParam.setFiter(boolFilterForShops); | ||
118 | + searchParam.setSize(10000); | ||
119 | + searchParam.setQuery(QueryBuilders.matchAllQuery()); | ||
120 | + | ||
121 | + // 2、调搜索,并将结果加入缓存 | ||
122 | + List<Map<String, Object>> shopsList = this.queryShopListByParam(searchParam); | ||
123 | + return shopsList; | ||
124 | + } catch (Exception e) { | ||
125 | + logger.error(e.getMessage(), e); | ||
126 | + return new ArrayList<Map<String, Object>>(); | ||
127 | + } | ||
128 | + } | ||
129 | + | ||
130 | + public Map<String, Map<String, Object>> queryShopMapByParam(Map<String, String> paramMap) { | ||
131 | + List<Map<String, Object>> shopList = this.queryShopListByParam(paramMap); | ||
132 | + Map<String, Map<String, Object>> shopInfoMap = new HashMap<String, Map<String, Object>>(); | ||
133 | + for (Map<String, Object> shopInfo : shopList) { | ||
134 | + shopInfoMap.put(shopInfo.getOrDefault("shop_id", 0).toString(), shopInfo); | ||
135 | + } | ||
136 | + return shopInfoMap; | ||
137 | + } | ||
141 | 138 | ||
142 | } | 139 | } |
1 | +package com.yoho.search.service.scene.others; | ||
2 | + | ||
3 | +import com.alibaba.fastjson.JSONObject; | ||
4 | +import com.yoho.error.event.SearchEvent; | ||
5 | +import com.yoho.search.base.utils.DateUtil; | ||
6 | +import com.yoho.search.base.utils.EventReportEnum; | ||
7 | +import com.yoho.search.base.utils.ISearchConstants; | ||
8 | +import com.yoho.search.common.BaseService; | ||
9 | +import com.yoho.search.common.SearchCommonService; | ||
10 | +import com.yoho.search.common.SearchRequestParams; | ||
11 | +import com.yoho.search.common.utils.SearchApiResultUtils; | ||
12 | +import com.yoho.search.core.es.model.SearchParam; | ||
13 | +import com.yoho.search.core.es.model.SearchResult; | ||
14 | +import com.yoho.search.core.es.utils.IgnoreSomeException; | ||
15 | +import com.yoho.search.models.SearchApiResult; | ||
16 | +import com.yoho.search.service.recall.beans.cache.SknReturnInfoCacheBean; | ||
17 | +import org.apache.commons.collections.MapUtils; | ||
18 | +import org.apache.commons.lang.StringUtils; | ||
19 | +import org.elasticsearch.index.query.BoolQueryBuilder; | ||
20 | +import org.elasticsearch.index.query.QueryBuilders; | ||
21 | +import org.slf4j.Logger; | ||
22 | +import org.slf4j.LoggerFactory; | ||
23 | +import org.springframework.beans.factory.annotation.Autowired; | ||
24 | +import org.springframework.context.ApplicationEventPublisher; | ||
25 | +import org.springframework.context.ApplicationEventPublisherAware; | ||
26 | +import org.springframework.stereotype.Service; | ||
27 | + | ||
28 | +import java.util.*; | ||
29 | +import java.util.stream.Collectors; | ||
30 | + | ||
31 | +/** | ||
32 | + * 拼团购 | ||
33 | + * | ||
34 | + * @author wangnan | ||
35 | + * @version 2018/9/4 | ||
36 | + */ | ||
37 | +@Service | ||
38 | +public class CollageService extends BaseService implements ApplicationEventPublisherAware { | ||
39 | + private final Logger logger = LoggerFactory.getLogger(this.getClass()); | ||
40 | + | ||
41 | + private ApplicationEventPublisher publisher; | ||
42 | + | ||
43 | + private static final List<String> paramsList = Arrays.asList( | ||
44 | + SearchRequestParams.COLLAGE_PARAM_ACTIVITYID, | ||
45 | + SearchRequestParams.COLLAGE_PARAM_PRODUCTSKN, | ||
46 | + SearchRequestParams.COLLAGE_PARAM_ORDERBY, | ||
47 | + SearchRequestParams.COLLAGE_PARAM_OPENLIMIT, | ||
48 | + SearchRequestParams.COLLAGE_PARAM_JOINLIMIT, | ||
49 | + SearchRequestParams.COLLAGE_PARAM_JOINLIMIT, | ||
50 | + SearchRequestParams.COLLAGE_PARAM_ISAUTO, | ||
51 | + SearchRequestParams.COLLAGE_PARAM_PAYLIMIT, | ||
52 | + SearchRequestParams.COLLAGE_PARAM_PRODUCTLIMIT); | ||
53 | + | ||
54 | + @Autowired | ||
55 | + private SearchCommonService searchCommonService; | ||
56 | + @Autowired | ||
57 | + private SknReturnInfoCacheBean sknReturnInfoCacheBean; | ||
58 | + | ||
59 | + @Override | ||
60 | + public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) { | ||
61 | + this.publisher = applicationEventPublisher; | ||
62 | + } | ||
63 | + | ||
64 | + public SearchApiResult searchList(Map<String, String> paramMap) { | ||
65 | + try { | ||
66 | + logger.info("[class=CollageService][func=searchList][param={}][begin={}]", paramMap.toString(), System.currentTimeMillis()); | ||
67 | + | ||
68 | + // 1.构造查询参数 | ||
69 | + SearchParam searchParam = new SearchParam(); | ||
70 | + int pageSize = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum")); | ||
71 | + int page = StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page")); | ||
72 | + if (page < 1 || pageSize < 0) { | ||
73 | + return new SearchApiResult().setCode(400).setMessage("分页参数错误"); | ||
74 | + } | ||
75 | + if (pageSize > 100) { | ||
76 | + pageSize = 100; | ||
77 | + } | ||
78 | + searchParam.setOffset((page - 1) * pageSize); | ||
79 | + searchParam.setSize(pageSize); | ||
80 | + | ||
81 | + //2.配置Filter | ||
82 | + BoolQueryBuilder boolFilter = constructHelperFilterBuilder(paramMap); | ||
83 | + searchParam.setFiter(boolFilter); | ||
84 | + | ||
85 | + final String indexName = ISearchConstants.INDEX_NAME_COLLAGE; | ||
86 | + | ||
87 | + //3.查询ES | ||
88 | + SearchResult searchResult = searchCommonService.doSearch(indexName, searchParam); | ||
89 | + SearchApiResult searchApiResult = new SearchApiResult(); | ||
90 | + if (searchResult == null) { | ||
91 | + return searchApiResult.setCode(500); | ||
92 | + } | ||
93 | + | ||
94 | + //4.关联ProductIndex信息 | ||
95 | + List<Integer> productSknList = new ArrayList<>(searchResult.getResultList().size()); | ||
96 | + List<Map<String, Object>> tempList = getTempMap(searchResult.getResultList(), productSknList); | ||
97 | + List<Map<String, Object>> sknInfoList = sknReturnInfoCacheBean.queryProductListBySkn(productSknList, productSknList.size()); | ||
98 | + Map<Integer, Map<String, Object>> sknInfoMap = new HashMap<>(); | ||
99 | + sknInfoList.stream().forEach(p -> sknInfoMap.put(MapUtils.getInteger(p, "product_skn"), p)); | ||
100 | + tempList.stream().forEach(p -> { | ||
101 | + Integer productSkn = MapUtils.getInteger(p, "product_skn"); | ||
102 | + Map<String, Object> sknMap = sknInfoMap.get(productSkn); | ||
103 | + p.put("product_name", MapUtils.getString(sknMap, "product_name", "")); | ||
104 | + p.put("sales_price", MapUtils.getDoubleValue(sknMap, "sales_price")); | ||
105 | + p.put("market_price", MapUtils.getDoubleValue(sknMap, "market_price")); | ||
106 | + p.put("sales_num", MapUtils.getIntValue(sknMap, "sales_num")); | ||
107 | + }); | ||
108 | + Map<Map<String, Object>, Integer> salesNumSknInfoMap = tempList.stream().collect(Collectors.toMap(p -> p, p -> MapUtils.getInteger(p, "sales_num"))); | ||
109 | + Map<Map<String, Object>,Integer> finalMap = new LinkedHashMap<>(); | ||
110 | + salesNumSknInfoMap.entrySet().stream().sorted(Map.Entry.comparingByValue()).forEachOrdered(e -> finalMap.put(e.getKey(), e.getValue())); | ||
111 | + List<Map<String, Object>> finalList = new ArrayList<>(finalMap.size()); | ||
112 | + Iterator iterator = finalMap.entrySet().iterator(); | ||
113 | + while (iterator.hasNext()) { | ||
114 | + Map.Entry entry = (Map.Entry) iterator.next(); | ||
115 | + finalList.add((Map<String, Object>) entry.getKey()); | ||
116 | + } | ||
117 | + Collections.reverse(finalList); | ||
118 | + // 4.构造返回结果并加入缓存 | ||
119 | + JSONObject dataMap = new JSONObject(); | ||
120 | + dataMap.put("total", searchResult.getTotal()); | ||
121 | + dataMap.put("page_size", searchParam.getSize()); | ||
122 | + dataMap.put("page_total", searchResult.getTotalPage()); | ||
123 | + dataMap.put("list", finalList); | ||
124 | + return new SearchApiResult().setData(dataMap); | ||
125 | + } catch (Exception e) { | ||
126 | + publisher.publishEvent(new SearchEvent(EventReportEnum.SEARCHCONTROLLER_IMAGEREPERTORY.getEventName(), EventReportEnum.SEARCHCONTROLLER_IMAGEREPERTORY.getFunctionName(), | ||
127 | + EventReportEnum.SEARCHCONTROLLER_IMAGEREPERTORY.getMoudleName(), "exception", IgnoreSomeException.filterSomeException(e), null)); | ||
128 | + return SearchApiResultUtils.errorSearchApiResult(logger, paramMap, e); | ||
129 | + } | ||
130 | + } | ||
131 | + | ||
132 | + private BoolQueryBuilder constructHelperFilterBuilder(Map<String, String> paramMap) throws Exception { | ||
133 | + BoolQueryBuilder boolFilter = QueryBuilders.boolQuery(); | ||
134 | + boolFilter.must(QueryBuilders.termQuery(SearchRequestParams.COLLAGE_PARAM_STATUS, 1)); | ||
135 | + boolFilter.must(QueryBuilders.termQuery(SearchRequestParams.COLLAGE_PARAM_ACTIVITYSTATUS, 1)); | ||
136 | + boolFilter.must(QueryBuilders.rangeQuery(SearchRequestParams.COLLAGE_PARAM_BEGINTIME).lte(DateUtil.getCurrentTimeSecond())); | ||
137 | + boolFilter.must(QueryBuilders.rangeQuery(SearchRequestParams.COLLAGE_PARAM_ENDTIME).gte(DateUtil.getCurrentTimeSecond())); | ||
138 | + addTermQueryBatch(paramMap, boolFilter, paramsList); | ||
139 | + boolFilter = generalHandler(paramMap, boolFilter); | ||
140 | + return boolFilter; | ||
141 | + } | ||
142 | + | ||
143 | + private List<Map<String, Object>> getTempMap(List<Map<String, Object>> resultList, List<Integer> productSknList) { | ||
144 | + List<Map<String, Object>> returnList = new ArrayList<Map<String, Object>>(); | ||
145 | + for (Map<String, Object> map : resultList) { | ||
146 | + Map<String, Object> productMap = getTempMap(map, productSknList); | ||
147 | + returnList.add(productMap); | ||
148 | + } | ||
149 | + return returnList; | ||
150 | + } | ||
151 | + | ||
152 | + private Map<String, Object> getTempMap(Map<String, Object> map, List<Integer> productSknList) { | ||
153 | + Map<String, Object> resultMap = new LinkedHashMap<>(); | ||
154 | + Integer productSkn = MapUtils.getIntValue(map, "productSkn"); | ||
155 | + productSknList.add(productSkn); | ||
156 | + resultMap.put("product_skn", productSkn); | ||
157 | + resultMap.put("people_num", MapUtils.getIntValue(map, "peopleNum")); | ||
158 | + return resultMap; | ||
159 | + } | ||
160 | +} |
@@ -4,14 +4,14 @@ import com.alibaba.fastjson.JSONObject; | @@ -4,14 +4,14 @@ import com.alibaba.fastjson.JSONObject; | ||
4 | import com.yoho.error.event.SearchEvent; | 4 | import com.yoho.error.event.SearchEvent; |
5 | import com.yoho.search.base.utils.EventReportEnum; | 5 | import com.yoho.search.base.utils.EventReportEnum; |
6 | import com.yoho.search.base.utils.ISearchConstants; | 6 | import com.yoho.search.base.utils.ISearchConstants; |
7 | +import com.yoho.search.common.BaseService; | ||
8 | +import com.yoho.search.common.SearchCommonService; | ||
9 | +import com.yoho.search.common.SearchRequestParams; | ||
7 | import com.yoho.search.common.utils.SearchApiResultUtils; | 10 | import com.yoho.search.common.utils.SearchApiResultUtils; |
8 | import com.yoho.search.core.es.model.SearchParam; | 11 | import com.yoho.search.core.es.model.SearchParam; |
9 | import com.yoho.search.core.es.model.SearchResult; | 12 | import com.yoho.search.core.es.model.SearchResult; |
10 | import com.yoho.search.core.es.utils.IgnoreSomeException; | 13 | import com.yoho.search.core.es.utils.IgnoreSomeException; |
11 | import com.yoho.search.models.SearchApiResult; | 14 | import com.yoho.search.models.SearchApiResult; |
12 | -import com.yoho.search.common.SearchCommonService; | ||
13 | -import com.yoho.search.common.SearchRequestParams; | ||
14 | -import com.yoho.search.common.BaseService; | ||
15 | import org.apache.commons.lang.StringUtils; | 15 | import org.apache.commons.lang.StringUtils; |
16 | import org.elasticsearch.index.query.BoolQueryBuilder; | 16 | import org.elasticsearch.index.query.BoolQueryBuilder; |
17 | import org.elasticsearch.index.query.MultiMatchQueryBuilder; | 17 | import org.elasticsearch.index.query.MultiMatchQueryBuilder; |
@@ -27,10 +27,7 @@ import org.springframework.context.ApplicationEventPublisher; | @@ -27,10 +27,7 @@ import org.springframework.context.ApplicationEventPublisher; | ||
27 | import org.springframework.context.ApplicationEventPublisherAware; | 27 | import org.springframework.context.ApplicationEventPublisherAware; |
28 | import org.springframework.stereotype.Service; | 28 | import org.springframework.stereotype.Service; |
29 | 29 | ||
30 | -import java.util.ArrayList; | ||
31 | -import java.util.LinkedHashMap; | ||
32 | -import java.util.List; | ||
33 | -import java.util.Map; | 30 | +import java.util.*; |
34 | 31 | ||
35 | /** | 32 | /** |
36 | * Created by wangnan on 2016/12/13. | 33 | * Created by wangnan on 2016/12/13. |
@@ -41,6 +38,13 @@ public class HelperService extends BaseService implements ApplicationEventPublis | @@ -41,6 +38,13 @@ public class HelperService extends BaseService implements ApplicationEventPublis | ||
41 | 38 | ||
42 | private ApplicationEventPublisher publisher; | 39 | private ApplicationEventPublisher publisher; |
43 | 40 | ||
41 | + private static final List<String> paramsList = Arrays.asList(SearchRequestParams.HELPER_PARAM_FIRSTCATEGORYID, | ||
42 | + SearchRequestParams.HELPER_PARAM_SECENDCATEGORYID, | ||
43 | + SearchRequestParams.HELPER_PARAM_HELPERTYPE, | ||
44 | + SearchRequestParams.HELPER_PARAM_ISFAQ, | ||
45 | + SearchRequestParams.HELPER_PARAM_ISROBOTQA, | ||
46 | + SearchRequestParams.HELPER_PARAM_SHOWPLATFORM); | ||
47 | + | ||
44 | @Autowired | 48 | @Autowired |
45 | private SearchCommonService searchCommonService; | 49 | private SearchCommonService searchCommonService; |
46 | 50 | ||
@@ -128,17 +132,12 @@ public class HelperService extends BaseService implements ApplicationEventPublis | @@ -128,17 +132,12 @@ public class HelperService extends BaseService implements ApplicationEventPublis | ||
128 | private BoolQueryBuilder constructHelperFilterBuilder(Map<String, String> paramMap) throws Exception { | 132 | private BoolQueryBuilder constructHelperFilterBuilder(Map<String, String> paramMap) throws Exception { |
129 | BoolQueryBuilder boolFilter = QueryBuilders.boolQuery(); | 133 | BoolQueryBuilder boolFilter = QueryBuilders.boolQuery(); |
130 | if (paramMap.containsKey(SearchRequestParams.HELPER_PARAM_ISCUSTOMERSERVICE) && StringUtils.isNotBlank(paramMap.get(SearchRequestParams.HELPER_PARAM_ISCUSTOMERSERVICE))) { | 134 | if (paramMap.containsKey(SearchRequestParams.HELPER_PARAM_ISCUSTOMERSERVICE) && StringUtils.isNotBlank(paramMap.get(SearchRequestParams.HELPER_PARAM_ISCUSTOMERSERVICE))) { |
131 | - boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.HELPER_PARAM_ISCUSTOMERSERVICE); | 135 | + boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.HELPER_PARAM_ISCUSTOMERSERVICE); |
132 | } else if (!paramMap.containsKey("showCustomerService") || !StringUtils.isNotBlank(paramMap.get("showCustomerService")) || !paramMap.get("showCustomerService").equals("Y")) { | 136 | } else if (!paramMap.containsKey("showCustomerService") || !StringUtils.isNotBlank(paramMap.get("showCustomerService")) || !paramMap.get("showCustomerService").equals("Y")) { |
133 | boolFilter.must(QueryBuilders.termQuery(SearchRequestParams.HELPER_PARAM_ISCUSTOMERSERVICE, "N")); | 137 | boolFilter.must(QueryBuilders.termQuery(SearchRequestParams.HELPER_PARAM_ISCUSTOMERSERVICE, "N")); |
134 | } | 138 | } |
135 | boolFilter.must(QueryBuilders.termQuery(SearchRequestParams.HELPER_PARAM_STATUS, 1)); | 139 | boolFilter.must(QueryBuilders.termQuery(SearchRequestParams.HELPER_PARAM_STATUS, 1)); |
136 | - boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.HELPER_PARAM_FIRSTCATEGORYID); | ||
137 | - boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.HELPER_PARAM_SECENDCATEGORYID); | ||
138 | - boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.HELPER_PARAM_HELPERTYPE); | ||
139 | - boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.HELPER_PARAM_ISFAQ); | ||
140 | - boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.HELPER_PARAM_ISROBOTQA); | ||
141 | - boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.HELPER_PARAM_SHOWPLATFORM); | 140 | + addTermQueryBatch(paramMap, boolFilter, paramsList); |
142 | boolFilter = generalHandler(paramMap, boolFilter); | 141 | boolFilter = generalHandler(paramMap, boolFilter); |
143 | return boolFilter; | 142 | return boolFilter; |
144 | } | 143 | } |
@@ -22,14 +22,12 @@ import org.springframework.context.ApplicationEventPublisher; | @@ -22,14 +22,12 @@ import org.springframework.context.ApplicationEventPublisher; | ||
22 | import org.springframework.context.ApplicationEventPublisherAware; | 22 | import org.springframework.context.ApplicationEventPublisherAware; |
23 | import org.springframework.stereotype.Service; | 23 | import org.springframework.stereotype.Service; |
24 | 24 | ||
25 | -import java.util.ArrayList; | ||
26 | -import java.util.LinkedHashMap; | ||
27 | -import java.util.List; | ||
28 | -import java.util.Map; | 25 | +import java.util.*; |
29 | 26 | ||
30 | /** | 27 | /** |
31 | * 图库服务 | 28 | * 图库服务 |
32 | - * @Author wangnan | 29 | + * |
30 | + * @Author wangnan | ||
33 | * @Date: Created in 2018/4/16 | 31 | * @Date: Created in 2018/4/16 |
34 | */ | 32 | */ |
35 | @Service | 33 | @Service |
@@ -39,6 +37,16 @@ public class ImageRepertoryService extends BaseService implements ApplicationEve | @@ -39,6 +37,16 @@ public class ImageRepertoryService extends BaseService implements ApplicationEve | ||
39 | 37 | ||
40 | private ApplicationEventPublisher publisher; | 38 | private ApplicationEventPublisher publisher; |
41 | 39 | ||
40 | + private static final List<String> paramsList = Arrays.asList( | ||
41 | + SearchRequestParams.IMAGEREPERTORY_PARAM_ID, | ||
42 | + SearchRequestParams.IMAGEREPERTORY_PARAM_CHANNEL, | ||
43 | + SearchRequestParams.IMAGEREPERTORY_PARAM_SEASON, | ||
44 | + SearchRequestParams.IMAGEREPERTORY_PARAM_IMAGESIZE, | ||
45 | + SearchRequestParams.IMAGEREPERTORY_PARAM_IMAGENAME, | ||
46 | + SearchRequestParams.IMAGEREPERTORY_PARAM_IMAGETYPE, | ||
47 | + SearchRequestParams.IMAGEREPERTORY_PARAM_ITEMTYPE, | ||
48 | + SearchRequestParams.IMAGEREPERTORY_PARAM_PORT); | ||
49 | + | ||
42 | @Autowired | 50 | @Autowired |
43 | private SearchCommonService searchCommonService; | 51 | private SearchCommonService searchCommonService; |
44 | 52 | ||
@@ -85,15 +93,8 @@ public class ImageRepertoryService extends BaseService implements ApplicationEve | @@ -85,15 +93,8 @@ public class ImageRepertoryService extends BaseService implements ApplicationEve | ||
85 | 93 | ||
86 | private BoolQueryBuilder constructHelperFilterBuilder(Map<String, String> paramMap) throws Exception { | 94 | private BoolQueryBuilder constructHelperFilterBuilder(Map<String, String> paramMap) throws Exception { |
87 | BoolQueryBuilder boolFilter = QueryBuilders.boolQuery(); | 95 | BoolQueryBuilder boolFilter = QueryBuilders.boolQuery(); |
88 | - boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.IMAGEREPERTORY_PARAM_ID); | ||
89 | - boolFilter = boolQueryTermsQuery(paramMap, boolFilter, SearchRequestParams.IMAGEREPERTORY_PARAM_ITEMID); | ||
90 | - boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.IMAGEREPERTORY_PARAM_CHANNEL); | ||
91 | - boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.IMAGEREPERTORY_PARAM_SEASON); | ||
92 | - boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.IMAGEREPERTORY_PARAM_IMAGESIZE); | ||
93 | - boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.IMAGEREPERTORY_PARAM_IMAGENAME); | ||
94 | - boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.IMAGEREPERTORY_PARAM_IMAGETYPE); | ||
95 | - boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.IMAGEREPERTORY_PARAM_ITEMTYPE); | ||
96 | - boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.IMAGEREPERTORY_PARAM_PORT); | 96 | + boolQueryTermsQuery(paramMap, boolFilter, SearchRequestParams.IMAGEREPERTORY_PARAM_ITEMID); |
97 | + addTermQueryBatch(paramMap, boolFilter, paramsList); | ||
97 | boolFilter = generalHandler(paramMap, boolFilter); | 98 | boolFilter = generalHandler(paramMap, boolFilter); |
98 | return boolFilter; | 99 | return boolFilter; |
99 | } | 100 | } |
-
Please register or login to post a comment