1
|
-package com.yoho.search.service.scene.searchlike;
|
|
|
2
|
-
|
|
|
3
|
-import com.alibaba.fastjson.JSONObject;
|
|
|
4
|
-import com.yoho.search.base.utils.SearchCollectionUtils;
|
|
|
5
|
-import com.yoho.search.base.utils.ProductIndexEsField;
|
|
|
6
|
-import com.yoho.search.aop.cache.SearchCacheAble;
|
|
|
7
|
-import com.yoho.search.core.es.model.SearchParam;
|
|
|
8
|
-import com.yoho.search.dal.model.SimilarSkn;
|
|
|
9
|
-import com.yoho.search.models.SearchApiResult;
|
|
|
10
|
-import com.yoho.search.common.SearchRequestParams;
|
|
|
11
|
-import com.yoho.search.service.index.BigdataSimilarSknIndexBaseService;
|
|
|
12
|
-import com.yoho.search.service.index.ProductIndexBaseService;
|
|
|
13
|
-import com.yoho.search.service.helper.ProductListHelper;
|
|
|
14
|
-import org.apache.commons.collections.MapUtils;
|
|
|
15
|
-import org.apache.commons.lang.StringUtils;
|
|
|
16
|
-import org.elasticsearch.index.query.BoolQueryBuilder;
|
|
|
17
|
-import org.elasticsearch.index.query.QueryBuilder;
|
|
|
18
|
-import org.elasticsearch.index.query.QueryBuilders;
|
|
|
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.*;
|
|
|
25
|
-
|
|
|
26
|
-@Service
|
|
|
27
|
-public class BigdataSimilarSknService {
|
|
|
28
|
- private static final Logger logger = LoggerFactory.getLogger(BigdataSimilarSknService.class);
|
|
|
29
|
-
|
|
|
30
|
- @Autowired
|
|
|
31
|
- private SearchLikeHelper searchLikeHelper;
|
|
|
32
|
- @Autowired
|
|
|
33
|
- private ProductIndexBaseService productIndexBaseService;
|
|
|
34
|
- @Autowired
|
|
|
35
|
- private ProductListHelper productListHelper;
|
|
|
36
|
- @Autowired
|
|
|
37
|
- private BigdataSimilarSknIndexBaseService bigdataSimilarSknIndexBaseService;
|
|
|
38
|
-
|
|
|
39
|
- /**
|
|
|
40
|
- * 获取非重复skn
|
|
|
41
|
- *
|
|
|
42
|
- * @param productSknStr
|
|
|
43
|
- * @return
|
|
|
44
|
- */
|
|
|
45
|
- private List<String> getDistinctSknList(String productSknStr) {
|
|
|
46
|
- List<String> results = new ArrayList<>();
|
|
|
47
|
- String[] productSkns = productSknStr.split(",");
|
|
|
48
|
- for (String productSkn : productSkns) {
|
|
|
49
|
- if (!results.contains(productSkn) && StringUtils.isNumeric(productSkn)) {
|
|
|
50
|
- results.add(productSkn);
|
|
|
51
|
- }
|
|
|
52
|
- }
|
|
|
53
|
- return results;
|
|
|
54
|
- }
|
|
|
55
|
-
|
|
|
56
|
- @SearchCacheAble(cacheInMinute = 600, cacheName = "SEARCH_LIKE_NOT_IN_SHOP_SIMILAR_NEW", includeParams = {"product_skn"})
|
|
|
57
|
- public SearchApiResult searchLikeSimilarSknNotInShop(Map<String, String> paramMap) {
|
|
|
58
|
- try {
|
|
|
59
|
- // 1、获取参数
|
|
|
60
|
- String productSkn = paramMap.get(SearchRequestParams.PARAM_SEARCH_PRODUCT_SKN);
|
|
|
61
|
- if (StringUtils.isBlank(productSkn)) {
|
|
|
62
|
- return new SearchApiResult().setCode(400).setMessage("请输入SKN");
|
|
|
63
|
- }
|
|
|
64
|
- // 2、检测分页参数【默认30条,最多60条】
|
|
|
65
|
- int pageSize = 30;
|
|
|
66
|
- // 3、获取当前查询的SKN的基本信息
|
|
|
67
|
- JSONObject productInfoInEs = searchLikeHelper.getProductInfoInEs(productSkn);
|
|
|
68
|
- if (productInfoInEs == null) {
|
|
|
69
|
- return new SearchApiResult().setCode(400).setMessage("SKN不存在");
|
|
|
70
|
- }
|
|
|
71
|
- //4、获取similarskn
|
|
|
72
|
- SimilarSkn similarSkn = bigdataSimilarSknIndexBaseService.querySimilarSkn(productSkn);
|
|
|
73
|
-
|
|
|
74
|
- //5、构造searchParams
|
|
|
75
|
- List<SearchParam> searchParams = new ArrayList<>();
|
|
|
76
|
- //5.1)图片[性别+不同店铺]
|
|
|
77
|
- List<String> diffShopImgSimilarSkns = new ArrayList<>();
|
|
|
78
|
- if (similarSkn != null && StringUtils.isNotEmpty(similarSkn.getDiffShopImgSimilarSkns())) {
|
|
|
79
|
- diffShopImgSimilarSkns = this.getDistinctSknList(similarSkn.getDiffShopImgSimilarSkns());
|
|
|
80
|
- }
|
|
|
81
|
- searchParams.add(this.builderSimilarSknSearchParam(productInfoInEs, diffShopImgSimilarSkns, true));
|
|
|
82
|
-
|
|
|
83
|
- //5.2)行为[性别+不同店铺+同品类]
|
|
|
84
|
- List<String> diffShopActionSimilarSkns = new ArrayList<>();
|
|
|
85
|
- if (similarSkn != null && StringUtils.isNotEmpty(similarSkn.getDiffShopActionSimilarSkns())) {
|
|
|
86
|
- diffShopActionSimilarSkns = this.getDistinctSknList(similarSkn.getDiffShopActionSimilarSkns());
|
|
|
87
|
- }
|
|
|
88
|
- searchParams.add(this.builderSimilarSknSearchParam(productInfoInEs, diffShopActionSimilarSkns, true));
|
|
|
89
|
-
|
|
|
90
|
- //5.3)文字兜底[性别+不同店铺+文字相似性]
|
|
|
91
|
- searchParams.add(searchLikeHelper.builderSearchLikeNotInShopCharactersSearchParam(productInfoInEs, Arrays.asList(productSkn), pageSize, true));
|
|
|
92
|
-
|
|
|
93
|
- // 6、获取搜索结果
|
|
|
94
|
- List<List<Map<String, Object>>> queryResults = searchLikeHelper.queryProductLists(searchParams);
|
|
|
95
|
-
|
|
|
96
|
- // 7、处理图片和行为的顺序
|
|
|
97
|
- List<Map<String, Object>> diffShopImgSimilarProducts = this.sortProductList(queryResults.get(0), diffShopImgSimilarSkns, pageSize / 3);
|
|
|
98
|
- List<Map<String, Object>> diffShopActionSimilarProducts = this.sortProductList(queryResults.get(1), diffShopActionSimilarSkns, pageSize / 3);
|
|
|
99
|
-
|
|
|
100
|
- // 8、获取临时结果
|
|
|
101
|
- List<Map<String, Object>> tempProductList = new ArrayList<>();
|
|
|
102
|
- tempProductList.addAll(diffShopImgSimilarProducts);
|
|
|
103
|
- tempProductList.addAll(diffShopActionSimilarProducts);
|
|
|
104
|
- tempProductList.addAll(queryResults.get(2));
|
|
|
105
|
- if (tempProductList.size() > pageSize) {
|
|
|
106
|
- tempProductList = SearchCollectionUtils.safeSubList(tempProductList, 0, pageSize);
|
|
|
107
|
- }
|
|
|
108
|
- // 8.1 保留偶数
|
|
|
109
|
- if (tempProductList.size() % 2 > 0) {
|
|
|
110
|
- tempProductList = SearchCollectionUtils.safeSubList(tempProductList, 0, tempProductList.size() - 1);
|
|
|
111
|
- }
|
|
|
112
|
- // 9、构造真实返回结果
|
|
|
113
|
- List<Map<String, Object>> productListResults = new ArrayList<>();
|
|
|
114
|
- if (!tempProductList.isEmpty()) {
|
|
|
115
|
- productListResults = productListHelper.buildReturnInfoByEsSourceList(tempProductList);
|
|
|
116
|
- }
|
|
|
117
|
- JSONObject result = new JSONObject();
|
|
|
118
|
- result.put("product_info", searchLikeHelper.genProductInfoResult(productInfoInEs));
|
|
|
119
|
- result.put("product_list", productListResults);
|
|
|
120
|
- return new SearchApiResult().setData(result);
|
|
|
121
|
- } catch (Exception e) {
|
|
|
122
|
- logger.error(e.getMessage(), e);
|
|
|
123
|
- return new SearchApiResult().setData(null).setMessage("searchLikeNotInShop Exception").setCode(500);
|
|
|
124
|
- }
|
|
|
125
|
- }
|
|
|
126
|
-
|
|
|
127
|
- /**
|
|
|
128
|
- * 按productSkn顺序截取商品条数-注意循环引用的问题
|
|
|
129
|
- *
|
|
|
130
|
- * @param esProductList
|
|
|
131
|
- * @param sortedProductSkns
|
|
|
132
|
- * @param size
|
|
|
133
|
- * @return
|
|
|
134
|
- */
|
|
|
135
|
- private List<Map<String, Object>> sortProductList(List<Map<String, Object>> esProductList, List<String> sortedProductSkns, int size) {
|
|
|
136
|
- List<Map<String, Object>> results = new ArrayList<>();
|
|
|
137
|
- if (esProductList == null || esProductList.isEmpty()) {
|
|
|
138
|
- return results;
|
|
|
139
|
- }
|
|
|
140
|
- if (sortedProductSkns == null || sortedProductSkns.isEmpty() || size <= 0) {
|
|
|
141
|
- return results;
|
|
|
142
|
- }
|
|
|
143
|
- Map<String, Map<String, Object>> productMap = SearchCollectionUtils.toMap(esProductList, (product) -> MapUtils.getString(product, ProductIndexEsField.productSkn, ""));
|
|
|
144
|
- for (String productSkn : sortedProductSkns) {
|
|
|
145
|
- if (productMap.containsKey(productSkn)) {
|
|
|
146
|
- results.add(new HashMap<>(productMap.get(productSkn)));//注意循环引用
|
|
|
147
|
- }
|
|
|
148
|
- if (results.size() >= size) {
|
|
|
149
|
- break;
|
|
|
150
|
- }
|
|
|
151
|
- }
|
|
|
152
|
- return results;
|
|
|
153
|
- }
|
|
|
154
|
-
|
|
|
155
|
-
|
|
|
156
|
- /**
|
|
|
157
|
- * 构建SimilarSknSearchParam[考虑productSkns为空的情况]
|
|
|
158
|
- *
|
|
|
159
|
- * @param productInfoInEs
|
|
|
160
|
- * @param productSkns
|
|
|
161
|
- * @param filterSameSort
|
|
|
162
|
- * @return
|
|
|
163
|
- */
|
|
|
164
|
- private SearchParam builderSimilarSknSearchParam(JSONObject productInfoInEs, List<String> productSkns, boolean filterSameSort) {
|
|
|
165
|
- // 1、设置SearchParam
|
|
|
166
|
- SearchParam searchParam = new SearchParam();
|
|
|
167
|
- // 2)设置query和filter
|
|
|
168
|
- searchParam.setFiter(this.builderSimilarSknFilter(productInfoInEs, productSkns, filterSameSort));
|
|
|
169
|
- // 3、设置分页参数
|
|
|
170
|
- searchParam.setOffset(0);
|
|
|
171
|
- searchParam.setSize(productSkns.size());
|
|
|
172
|
- // 4)设置返回的参数【节省带宽】
|
|
|
173
|
- List<String> includeFields = productIndexBaseService.getProductIndexIncludeFields();
|
|
|
174
|
- searchParam.setIncludeFields(includeFields);
|
|
|
175
|
- return searchParam;
|
|
|
176
|
- }
|
|
|
177
|
-
|
|
|
178
|
- private QueryBuilder builderSimilarSknFilter(JSONObject productInfoInEs, List<String> inProductSkns, boolean filterSameSort) {
|
|
|
179
|
- String isGlobalInEs = productInfoInEs.getString(ProductIndexEsField.isGlobal);
|
|
|
180
|
- boolean isGlobal = "Y".equalsIgnoreCase(isGlobalInEs);
|
|
|
181
|
- BoolQueryBuilder boolFilter = searchLikeHelper.genDefaultSearchLikeFilter(null, isGlobal);
|
|
|
182
|
- // 1)设置此SKN相关的性别过滤条件
|
|
|
183
|
- String gender = productInfoInEs.getString(ProductIndexEsField.gender);
|
|
|
184
|
- List<String> genderList = searchLikeHelper.getGenderInfo(gender);
|
|
|
185
|
- if (genderList != null && !genderList.isEmpty()) {
|
|
|
186
|
- boolFilter.must(QueryBuilders.termsQuery(ProductIndexEsField.gender, genderList));
|
|
|
187
|
- }
|
|
|
188
|
- // 2)设置productskn
|
|
|
189
|
- if (inProductSkns != null && !inProductSkns.isEmpty()) {
|
|
|
190
|
- boolFilter.must(QueryBuilders.termsQuery(ProductIndexEsField.productSkn, inProductSkns));
|
|
|
191
|
- }
|
|
|
192
|
- // 3)设置品牌或店铺信息
|
|
|
193
|
- Integer brandId = productInfoInEs.getInteger(ProductIndexEsField.brandId);
|
|
|
194
|
- Integer shopId = productInfoInEs.getInteger(ProductIndexEsField.shopId);
|
|
|
195
|
- if (searchLikeHelper.isLegalInteger(shopId)) {
|
|
|
196
|
- boolFilter.mustNot(QueryBuilders.termQuery(ProductIndexEsField.shopId, shopId));
|
|
|
197
|
- } else if (searchLikeHelper.isLegalInteger(brandId)) {
|
|
|
198
|
- boolFilter.mustNot(QueryBuilders.termQuery(ProductIndexEsField.brandId, brandId));
|
|
|
199
|
- }
|
|
|
200
|
- // 4)是否需要同品类
|
|
|
201
|
- Integer middleSortId = productInfoInEs.getInteger(ProductIndexEsField.middleSortId);
|
|
|
202
|
- if (filterSameSort && searchLikeHelper.isLegalInteger(middleSortId)) {
|
|
|
203
|
- boolFilter.must(QueryBuilders.termQuery(ProductIndexEsField.middleSortId, middleSortId));
|
|
|
204
|
- }
|
|
|
205
|
- return boolFilter;
|
|
|
206
|
- }
|
|
|
207
|
-
|
|
|
208
|
-} |
1
|
+package com.yoho.search.service.scene.searchlike;
|
|
|
2
|
+
|
|
|
3
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
4
|
+import com.yoho.search.base.utils.SearchCollectionUtils;
|
|
|
5
|
+import com.yoho.search.base.utils.ProductIndexEsField;
|
|
|
6
|
+import com.yoho.search.aop.cache.SearchCacheAble;
|
|
|
7
|
+import com.yoho.search.core.es.model.SearchParam;
|
|
|
8
|
+import com.yoho.search.dal.model.BidataSimilarSkn;
|
|
|
9
|
+import com.yoho.search.models.SearchApiResult;
|
|
|
10
|
+import com.yoho.search.common.SearchRequestParams;
|
|
|
11
|
+import com.yoho.search.service.index.BigdataSimilarSknIndexBaseService;
|
|
|
12
|
+import com.yoho.search.service.index.ProductIndexBaseService;
|
|
|
13
|
+import com.yoho.search.service.helper.ProductListHelper;
|
|
|
14
|
+import org.apache.commons.collections.MapUtils;
|
|
|
15
|
+import org.apache.commons.lang.StringUtils;
|
|
|
16
|
+import org.elasticsearch.index.query.BoolQueryBuilder;
|
|
|
17
|
+import org.elasticsearch.index.query.QueryBuilder;
|
|
|
18
|
+import org.elasticsearch.index.query.QueryBuilders;
|
|
|
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.*;
|
|
|
25
|
+
|
|
|
26
|
+@Service
|
|
|
27
|
+public class BigdataSimilarSknService {
|
|
|
28
|
+ private static final Logger logger = LoggerFactory.getLogger(BigdataSimilarSknService.class);
|
|
|
29
|
+
|
|
|
30
|
+ @Autowired
|
|
|
31
|
+ private SearchLikeHelper searchLikeHelper;
|
|
|
32
|
+ @Autowired
|
|
|
33
|
+ private ProductIndexBaseService productIndexBaseService;
|
|
|
34
|
+ @Autowired
|
|
|
35
|
+ private ProductListHelper productListHelper;
|
|
|
36
|
+ @Autowired
|
|
|
37
|
+ private BigdataSimilarSknIndexBaseService bigdataSimilarSknIndexBaseService;
|
|
|
38
|
+
|
|
|
39
|
+ /**
|
|
|
40
|
+ * 获取非重复skn
|
|
|
41
|
+ *
|
|
|
42
|
+ * @param productSknStr
|
|
|
43
|
+ * @return
|
|
|
44
|
+ */
|
|
|
45
|
+ private List<String> getDistinctSknList(String productSknStr) {
|
|
|
46
|
+ List<String> results = new ArrayList<>();
|
|
|
47
|
+ String[] productSkns = productSknStr.split(",");
|
|
|
48
|
+ for (String productSkn : productSkns) {
|
|
|
49
|
+ if (!results.contains(productSkn) && StringUtils.isNumeric(productSkn)) {
|
|
|
50
|
+ results.add(productSkn);
|
|
|
51
|
+ }
|
|
|
52
|
+ }
|
|
|
53
|
+ return results;
|
|
|
54
|
+ }
|
|
|
55
|
+
|
|
|
56
|
+ @SearchCacheAble(cacheInMinute = 600, cacheName = "SEARCH_LIKE_NOT_IN_SHOP_SIMILAR_NEW", includeParams = {"product_skn"})
|
|
|
57
|
+ public SearchApiResult searchLikeSimilarSknNotInShop(Map<String, String> paramMap) {
|
|
|
58
|
+ try {
|
|
|
59
|
+ // 1、获取参数
|
|
|
60
|
+ String productSkn = paramMap.get(SearchRequestParams.PARAM_SEARCH_PRODUCT_SKN);
|
|
|
61
|
+ if (StringUtils.isBlank(productSkn)) {
|
|
|
62
|
+ return new SearchApiResult().setCode(400).setMessage("请输入SKN");
|
|
|
63
|
+ }
|
|
|
64
|
+ // 2、检测分页参数【默认30条,最多60条】
|
|
|
65
|
+ int pageSize = 30;
|
|
|
66
|
+ // 3、获取当前查询的SKN的基本信息
|
|
|
67
|
+ JSONObject productInfoInEs = searchLikeHelper.getProductInfoInEs(productSkn);
|
|
|
68
|
+ if (productInfoInEs == null) {
|
|
|
69
|
+ return new SearchApiResult().setCode(400).setMessage("SKN不存在");
|
|
|
70
|
+ }
|
|
|
71
|
+ //4、获取similarskn
|
|
|
72
|
+ BidataSimilarSkn bidataSimilarSkn = bigdataSimilarSknIndexBaseService.querySimilarSkn(productSkn);
|
|
|
73
|
+
|
|
|
74
|
+ //5、构造searchParams
|
|
|
75
|
+ List<SearchParam> searchParams = new ArrayList<>();
|
|
|
76
|
+ //5.1)图片[性别+不同店铺]
|
|
|
77
|
+ List<String> diffShopImgSimilarSkns = new ArrayList<>();
|
|
|
78
|
+ if (bidataSimilarSkn != null && StringUtils.isNotEmpty(bidataSimilarSkn.getDiffShopImgSimilarSkns())) {
|
|
|
79
|
+ diffShopImgSimilarSkns = this.getDistinctSknList(bidataSimilarSkn.getDiffShopImgSimilarSkns());
|
|
|
80
|
+ }
|
|
|
81
|
+ searchParams.add(this.builderSimilarSknSearchParam(productInfoInEs, diffShopImgSimilarSkns, true));
|
|
|
82
|
+
|
|
|
83
|
+ //5.2)行为[性别+不同店铺+同品类]
|
|
|
84
|
+ List<String> diffShopActionSimilarSkns = new ArrayList<>();
|
|
|
85
|
+ if (bidataSimilarSkn != null && StringUtils.isNotEmpty(bidataSimilarSkn.getDiffShopActionSimilarSkns())) {
|
|
|
86
|
+ diffShopActionSimilarSkns = this.getDistinctSknList(bidataSimilarSkn.getDiffShopActionSimilarSkns());
|
|
|
87
|
+ }
|
|
|
88
|
+ searchParams.add(this.builderSimilarSknSearchParam(productInfoInEs, diffShopActionSimilarSkns, true));
|
|
|
89
|
+
|
|
|
90
|
+ //5.3)文字兜底[性别+不同店铺+文字相似性]
|
|
|
91
|
+ searchParams.add(searchLikeHelper.builderSearchLikeNotInShopCharactersSearchParam(productInfoInEs, Arrays.asList(productSkn), pageSize, true));
|
|
|
92
|
+
|
|
|
93
|
+ // 6、获取搜索结果
|
|
|
94
|
+ List<List<Map<String, Object>>> queryResults = searchLikeHelper.queryProductLists(searchParams);
|
|
|
95
|
+
|
|
|
96
|
+ // 7、处理图片和行为的顺序
|
|
|
97
|
+ List<Map<String, Object>> diffShopImgSimilarProducts = this.sortProductList(queryResults.get(0), diffShopImgSimilarSkns, pageSize / 3);
|
|
|
98
|
+ List<Map<String, Object>> diffShopActionSimilarProducts = this.sortProductList(queryResults.get(1), diffShopActionSimilarSkns, pageSize / 3);
|
|
|
99
|
+
|
|
|
100
|
+ // 8、获取临时结果
|
|
|
101
|
+ List<Map<String, Object>> tempProductList = new ArrayList<>();
|
|
|
102
|
+ tempProductList.addAll(diffShopImgSimilarProducts);
|
|
|
103
|
+ tempProductList.addAll(diffShopActionSimilarProducts);
|
|
|
104
|
+ tempProductList.addAll(queryResults.get(2));
|
|
|
105
|
+ if (tempProductList.size() > pageSize) {
|
|
|
106
|
+ tempProductList = SearchCollectionUtils.safeSubList(tempProductList, 0, pageSize);
|
|
|
107
|
+ }
|
|
|
108
|
+ // 8.1 保留偶数
|
|
|
109
|
+ if (tempProductList.size() % 2 > 0) {
|
|
|
110
|
+ tempProductList = SearchCollectionUtils.safeSubList(tempProductList, 0, tempProductList.size() - 1);
|
|
|
111
|
+ }
|
|
|
112
|
+ // 9、构造真实返回结果
|
|
|
113
|
+ List<Map<String, Object>> productListResults = new ArrayList<>();
|
|
|
114
|
+ if (!tempProductList.isEmpty()) {
|
|
|
115
|
+ productListResults = productListHelper.buildReturnInfoByEsSourceList(tempProductList);
|
|
|
116
|
+ }
|
|
|
117
|
+ JSONObject result = new JSONObject();
|
|
|
118
|
+ result.put("product_info", searchLikeHelper.genProductInfoResult(productInfoInEs));
|
|
|
119
|
+ result.put("product_list", productListResults);
|
|
|
120
|
+ return new SearchApiResult().setData(result);
|
|
|
121
|
+ } catch (Exception e) {
|
|
|
122
|
+ logger.error(e.getMessage(), e);
|
|
|
123
|
+ return new SearchApiResult().setData(null).setMessage("searchLikeNotInShop Exception").setCode(500);
|
|
|
124
|
+ }
|
|
|
125
|
+ }
|
|
|
126
|
+
|
|
|
127
|
+ /**
|
|
|
128
|
+ * 按productSkn顺序截取商品条数-注意循环引用的问题
|
|
|
129
|
+ *
|
|
|
130
|
+ * @param esProductList
|
|
|
131
|
+ * @param sortedProductSkns
|
|
|
132
|
+ * @param size
|
|
|
133
|
+ * @return
|
|
|
134
|
+ */
|
|
|
135
|
+ private List<Map<String, Object>> sortProductList(List<Map<String, Object>> esProductList, List<String> sortedProductSkns, int size) {
|
|
|
136
|
+ List<Map<String, Object>> results = new ArrayList<>();
|
|
|
137
|
+ if (esProductList == null || esProductList.isEmpty()) {
|
|
|
138
|
+ return results;
|
|
|
139
|
+ }
|
|
|
140
|
+ if (sortedProductSkns == null || sortedProductSkns.isEmpty() || size <= 0) {
|
|
|
141
|
+ return results;
|
|
|
142
|
+ }
|
|
|
143
|
+ Map<String, Map<String, Object>> productMap = SearchCollectionUtils.toMap(esProductList, (product) -> MapUtils.getString(product, ProductIndexEsField.productSkn, ""));
|
|
|
144
|
+ for (String productSkn : sortedProductSkns) {
|
|
|
145
|
+ if (productMap.containsKey(productSkn)) {
|
|
|
146
|
+ results.add(new HashMap<>(productMap.get(productSkn)));//注意循环引用
|
|
|
147
|
+ }
|
|
|
148
|
+ if (results.size() >= size) {
|
|
|
149
|
+ break;
|
|
|
150
|
+ }
|
|
|
151
|
+ }
|
|
|
152
|
+ return results;
|
|
|
153
|
+ }
|
|
|
154
|
+
|
|
|
155
|
+
|
|
|
156
|
+ /**
|
|
|
157
|
+ * 构建SimilarSknSearchParam[考虑productSkns为空的情况]
|
|
|
158
|
+ *
|
|
|
159
|
+ * @param productInfoInEs
|
|
|
160
|
+ * @param productSkns
|
|
|
161
|
+ * @param filterSameSort
|
|
|
162
|
+ * @return
|
|
|
163
|
+ */
|
|
|
164
|
+ private SearchParam builderSimilarSknSearchParam(JSONObject productInfoInEs, List<String> productSkns, boolean filterSameSort) {
|
|
|
165
|
+ // 1、设置SearchParam
|
|
|
166
|
+ SearchParam searchParam = new SearchParam();
|
|
|
167
|
+ // 2)设置query和filter
|
|
|
168
|
+ searchParam.setFiter(this.builderSimilarSknFilter(productInfoInEs, productSkns, filterSameSort));
|
|
|
169
|
+ // 3、设置分页参数
|
|
|
170
|
+ searchParam.setOffset(0);
|
|
|
171
|
+ searchParam.setSize(productSkns.size());
|
|
|
172
|
+ // 4)设置返回的参数【节省带宽】
|
|
|
173
|
+ List<String> includeFields = productIndexBaseService.getProductIndexIncludeFields();
|
|
|
174
|
+ searchParam.setIncludeFields(includeFields);
|
|
|
175
|
+ return searchParam;
|
|
|
176
|
+ }
|
|
|
177
|
+
|
|
|
178
|
+ private QueryBuilder builderSimilarSknFilter(JSONObject productInfoInEs, List<String> inProductSkns, boolean filterSameSort) {
|
|
|
179
|
+ String isGlobalInEs = productInfoInEs.getString(ProductIndexEsField.isGlobal);
|
|
|
180
|
+ boolean isGlobal = "Y".equalsIgnoreCase(isGlobalInEs);
|
|
|
181
|
+ BoolQueryBuilder boolFilter = searchLikeHelper.genDefaultSearchLikeFilter(null, isGlobal);
|
|
|
182
|
+ // 1)设置此SKN相关的性别过滤条件
|
|
|
183
|
+ String gender = productInfoInEs.getString(ProductIndexEsField.gender);
|
|
|
184
|
+ List<String> genderList = searchLikeHelper.getGenderInfo(gender);
|
|
|
185
|
+ if (genderList != null && !genderList.isEmpty()) {
|
|
|
186
|
+ boolFilter.must(QueryBuilders.termsQuery(ProductIndexEsField.gender, genderList));
|
|
|
187
|
+ }
|
|
|
188
|
+ // 2)设置productskn
|
|
|
189
|
+ if (inProductSkns != null && !inProductSkns.isEmpty()) {
|
|
|
190
|
+ boolFilter.must(QueryBuilders.termsQuery(ProductIndexEsField.productSkn, inProductSkns));
|
|
|
191
|
+ }
|
|
|
192
|
+ // 3)设置品牌或店铺信息
|
|
|
193
|
+ Integer brandId = productInfoInEs.getInteger(ProductIndexEsField.brandId);
|
|
|
194
|
+ Integer shopId = productInfoInEs.getInteger(ProductIndexEsField.shopId);
|
|
|
195
|
+ if (searchLikeHelper.isLegalInteger(shopId)) {
|
|
|
196
|
+ boolFilter.mustNot(QueryBuilders.termQuery(ProductIndexEsField.shopId, shopId));
|
|
|
197
|
+ } else if (searchLikeHelper.isLegalInteger(brandId)) {
|
|
|
198
|
+ boolFilter.mustNot(QueryBuilders.termQuery(ProductIndexEsField.brandId, brandId));
|
|
|
199
|
+ }
|
|
|
200
|
+ // 4)是否需要同品类
|
|
|
201
|
+ Integer middleSortId = productInfoInEs.getInteger(ProductIndexEsField.middleSortId);
|
|
|
202
|
+ if (filterSameSort && searchLikeHelper.isLegalInteger(middleSortId)) {
|
|
|
203
|
+ boolFilter.must(QueryBuilders.termQuery(ProductIndexEsField.middleSortId, middleSortId));
|
|
|
204
|
+ }
|
|
|
205
|
+ return boolFilter;
|
|
|
206
|
+ }
|
|
|
207
|
+
|
|
|
208
|
+} |