|
@@ -8,10 +8,7 @@ import java.util.List; |
|
@@ -8,10 +8,7 @@ import java.util.List; |
8
|
import java.util.Map;
|
8
|
import java.util.Map;
|
9
|
import java.util.Set;
|
9
|
import java.util.Set;
|
10
|
|
10
|
|
11
|
-import javax.annotation.PostConstruct;
|
|
|
12
|
-
|
|
|
13
|
import org.apache.commons.collections.CollectionUtils;
|
11
|
import org.apache.commons.collections.CollectionUtils;
|
14
|
-import org.apache.commons.lang.StringUtils;
|
|
|
15
|
import org.elasticsearch.index.query.BoolQueryBuilder;
|
12
|
import org.elasticsearch.index.query.BoolQueryBuilder;
|
16
|
import org.elasticsearch.index.query.QueryBuilders;
|
13
|
import org.elasticsearch.index.query.QueryBuilders;
|
17
|
import org.slf4j.Logger;
|
14
|
import org.slf4j.Logger;
|
|
@@ -22,19 +19,15 @@ import org.springframework.stereotype.Service; |
|
@@ -22,19 +19,15 @@ import org.springframework.stereotype.Service; |
22
|
import com.alibaba.fastjson.JSONObject;
|
19
|
import com.alibaba.fastjson.JSONObject;
|
23
|
import com.yoho.search.base.utils.ISearchConstants;
|
20
|
import com.yoho.search.base.utils.ISearchConstants;
|
24
|
import com.yoho.search.base.utils.ProductIndexEsField;
|
21
|
import com.yoho.search.base.utils.ProductIndexEsField;
|
25
|
-import com.yoho.search.common.cache.SearchCacheFactory;
|
|
|
26
|
-import com.yoho.search.common.cache.SearchCacheMatchLogger;
|
|
|
27
|
-import com.yoho.search.common.cache.model.SearchCache;
|
22
|
+import com.yoho.search.common.cache.aop.SearchCacheAble;
|
28
|
import com.yoho.search.common.utils.SearchApiResultUtils;
|
23
|
import com.yoho.search.common.utils.SearchApiResultUtils;
|
29
|
import com.yoho.search.core.es.model.SearchParam;
|
24
|
import com.yoho.search.core.es.model.SearchParam;
|
30
|
import com.yoho.search.core.es.model.SearchResult;
|
25
|
import com.yoho.search.core.es.model.SearchResult;
|
31
|
import com.yoho.search.models.SearchApiResult;
|
26
|
import com.yoho.search.models.SearchApiResult;
|
32
|
-import com.yoho.search.service.base.SearchCacheService;
|
|
|
33
|
import com.yoho.search.service.base.SearchCommonService;
|
27
|
import com.yoho.search.service.base.SearchCommonService;
|
34
|
import com.yoho.search.service.base.SearchRequestParams;
|
28
|
import com.yoho.search.service.base.SearchRequestParams;
|
35
|
import com.yoho.search.service.base.index.ProductIndexBaseService;
|
29
|
import com.yoho.search.service.base.index.ProductIndexBaseService;
|
36
|
import com.yoho.search.service.helper.SearchCommonHelper;
|
30
|
import com.yoho.search.service.helper.SearchCommonHelper;
|
37
|
-import com.yoho.search.service.helper.SearchKeyWordHelper;
|
|
|
38
|
import com.yoho.search.service.helper.SearchParamHelper;
|
31
|
import com.yoho.search.service.helper.SearchParamHelper;
|
39
|
import com.yoho.search.service.helper.SearchSortHelper;
|
32
|
import com.yoho.search.service.helper.SearchSortHelper;
|
40
|
import com.yoho.search.service.service.IProductListWithSupplyService;
|
33
|
import com.yoho.search.service.service.IProductListWithSupplyService;
|
|
@@ -55,70 +48,35 @@ public class ProductListWithSupplyServiceImpl implements IProductListWithSupplyS |
|
@@ -55,70 +48,35 @@ public class ProductListWithSupplyServiceImpl implements IProductListWithSupplyS |
55
|
@Autowired
|
48
|
@Autowired
|
56
|
private SearchCommonService searchCommonService;
|
49
|
private SearchCommonService searchCommonService;
|
57
|
@Autowired
|
50
|
@Autowired
|
58
|
- private SearchKeyWordHelper searchKeyWordService;
|
|
|
59
|
- @Autowired
|
|
|
60
|
private ProductIndexBaseService productIndexBaseService;
|
51
|
private ProductIndexBaseService productIndexBaseService;
|
61
|
@Autowired
|
52
|
@Autowired
|
62
|
private ISimilarProductService similarProductService;
|
53
|
private ISimilarProductService similarProductService;
|
63
|
@Autowired
|
54
|
@Autowired
|
64
|
- private SearchCacheService searchCacheService;
|
|
|
65
|
- @Autowired
|
|
|
66
|
- private SearchCacheFactory searchCacheFactory;
|
|
|
67
|
- @Autowired
|
|
|
68
|
private SearchParamHelper searchParamHelper;
|
55
|
private SearchParamHelper searchParamHelper;
|
69
|
|
56
|
|
70
|
- private SearchCache productListSearchCache;
|
|
|
71
|
-
|
|
|
72
|
- @PostConstruct
|
|
|
73
|
- void init() {
|
|
|
74
|
- productListSearchCache = searchCacheFactory.getProductListSearchCache();
|
|
|
75
|
- }
|
|
|
76
|
-
|
|
|
77
|
/**
|
57
|
/**
|
78
|
- * 不支持分页,最大传入skn数100,大于100只返回100条
|
58
|
+ * 不支持分页,最多返回100个
|
79
|
*/
|
59
|
*/
|
80
|
@Override
|
60
|
@Override
|
|
|
61
|
+ @SearchCacheAble(cacheInMinute = 30, cacheName = "PRODUCT_LIST_WITH_SUPPLY")
|
81
|
public SearchApiResult productListWithSupply(Map<String, String> paramMap) {
|
62
|
public SearchApiResult productListWithSupply(Map<String, String> paramMap) {
|
82
|
try {
|
63
|
try {
|
83
|
- SearchApiResult searchResult = innerProductList(paramMap);
|
|
|
84
|
- return searchResult;
|
|
|
85
|
- } catch (Exception e) {
|
|
|
86
|
- logger.error("[func=productListWithSupply][params=" + paramMap + "]", e);
|
|
|
87
|
- return SearchApiResultUtils.errorSearchApiResult("productListWithSupply", paramMap, e);
|
|
|
88
|
- }
|
|
|
89
|
- }
|
|
|
90
|
-
|
|
|
91
|
- private SearchApiResult innerProductList(Map<String, String> paramMap) throws Exception {
|
|
|
92
|
- long begin = System.currentTimeMillis();
|
|
|
93
|
- logger.info("[func=productListWithSupply][param={}][begin={}]", paramMap, begin);
|
|
|
94
|
-
|
|
|
95
|
- // 1)构造搜索参数
|
|
|
96
|
- SearchParam searchParam = buildProductListSearchParam(paramMap);
|
|
|
97
|
-
|
|
|
98
|
- // 2)从缓存中获取数据
|
|
|
99
|
- final String indexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
|
|
|
100
|
- JSONObject cacheObject = searchCacheService.getJSONObjectFromCache(productListSearchCache, indexName, searchParam);
|
|
|
101
|
- if (cacheObject != null) {
|
|
|
102
|
- SearchCacheMatchLogger.doSearchCacheMatchLog("/productindex/productListWithSupply.json", paramMap);
|
|
|
103
|
- return new SearchApiResult().setData(cacheObject);
|
|
|
104
|
- }
|
64
|
+ // 1)构建基本查询参数
|
|
|
65
|
+ SearchParam searchParam = searchParamHelper.buildDefault(paramMap);
|
|
|
66
|
+ searchParam.setAggregationBuilders(null);
|
|
|
67
|
+ searchParam.setOffset(0);
|
|
|
68
|
+ searchParam.setSize(100);
|
105
|
|
69
|
|
106
|
- // 3)查询ES
|
|
|
107
|
- SearchResult searchResult = searchCommonService.doSearch(indexName, searchParam);
|
70
|
+ // 2)查询ES
|
|
|
71
|
+ SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
|
108
|
if (searchResult == null) {
|
72
|
if (searchResult == null) {
|
109
|
return new SearchApiResult().setCode(500).setMessage("execption");
|
73
|
return new SearchApiResult().setCode(500).setMessage("execption");
|
110
|
}
|
74
|
}
|
111
|
|
75
|
|
112
|
- // 4)记录关键字对应的查询结果
|
|
|
113
|
- String queryWord = paramMap.get("query");
|
|
|
114
|
- if (!StringUtils.isBlank(queryWord) && !searchCommonHelper.isQuerySknOrSku(queryWord)) {
|
|
|
115
|
- searchKeyWordService.recordKeyWordByResultCount(queryWord, searchResult.getTotal());
|
|
|
116
|
- }
|
|
|
117
|
-
|
|
|
118
|
// 5)补充SKN逻辑:记录传入的skn列表,调用搜索,把返回的product列表中的skn记录到一个列表,找出无效的skn,用这些skn查找相似的,加入到之前的返回列表中返回
|
76
|
// 5)补充SKN逻辑:记录传入的skn列表,调用搜索,把返回的product列表中的skn记录到一个列表,找出无效的skn,用这些skn查找相似的,加入到之前的返回列表中返回
|
119
|
List<Map<String, Object>> productList = supplySkn(paramMap, searchResult, searchParam);
|
77
|
List<Map<String, Object>> productList = supplySkn(paramMap, searchResult, searchParam);
|
120
|
|
78
|
|
121
|
- // 6)构造返回结果
|
79
|
+ // 7)构造返回结果
|
122
|
JSONObject dataMap = new JSONObject();
|
80
|
JSONObject dataMap = new JSONObject();
|
123
|
dataMap.put("total", productList.size());
|
81
|
dataMap.put("total", productList.size());
|
124
|
dataMap.put("page", 1);
|
82
|
dataMap.put("page", 1);
|
|
@@ -126,10 +84,11 @@ public class ProductListWithSupplyServiceImpl implements IProductListWithSupplyS |
|
@@ -126,10 +84,11 @@ public class ProductListWithSupplyServiceImpl implements IProductListWithSupplyS |
126
|
dataMap.put("page_total", 1);
|
84
|
dataMap.put("page_total", 1);
|
127
|
dataMap.put("product_list", productList);
|
85
|
dataMap.put("product_list", productList);
|
128
|
|
86
|
|
129
|
- // 7)将结果存进缓存
|
|
|
130
|
- searchCacheService.addJSONObjectToCache(productListSearchCache, indexName, searchParam, dataMap);
|
|
|
131
|
- logger.info("[func=productListWithSupply][cost={}]", System.currentTimeMillis() - begin);
|
|
|
132
|
return new SearchApiResult().setData(dataMap);
|
87
|
return new SearchApiResult().setData(dataMap);
|
|
|
88
|
+ } catch (Exception e) {
|
|
|
89
|
+ logger.error("[func=productListWithSupply][params=" + paramMap + "]", e);
|
|
|
90
|
+ return SearchApiResultUtils.errorSearchApiResult("productListWithSupply", paramMap, e);
|
|
|
91
|
+ }
|
133
|
}
|
92
|
}
|
134
|
|
93
|
|
135
|
/**
|
94
|
/**
|
|
@@ -216,24 +175,4 @@ public class ProductListWithSupplyServiceImpl implements IProductListWithSupplyS |
|
@@ -216,24 +175,4 @@ public class ProductListWithSupplyServiceImpl implements IProductListWithSupplyS |
216
|
}
|
175
|
}
|
217
|
return trueInvalidSknList;
|
176
|
return trueInvalidSknList;
|
218
|
}
|
177
|
}
|
219
|
-
|
|
|
220
|
- private SearchParam buildProductListSearchParam(Map<String, String> paramMap) throws Exception {
|
|
|
221
|
- // 1)验证查询条数
|
|
|
222
|
- int pageSize = StringUtils.isBlank(paramMap.get("viewNum")) ? 100 : Integer.parseInt(paramMap.get("viewNum"));
|
|
|
223
|
- int page = 1;
|
|
|
224
|
- if (page < 1 || pageSize < 0) {
|
|
|
225
|
- throw new IllegalArgumentException("分页参数不合法");
|
|
|
226
|
- }
|
|
|
227
|
- if (pageSize > 100) {
|
|
|
228
|
- pageSize = 100;
|
|
|
229
|
- }
|
|
|
230
|
- // 2)构建基本查询参数
|
|
|
231
|
- SearchParam searchParam = searchParamHelper.buildDefault(paramMap);
|
|
|
232
|
- searchParam.setAggregationBuilders(null);
|
|
|
233
|
- searchParam.setOffset((page - 1) * pageSize);
|
|
|
234
|
- searchParam.setSize(pageSize);
|
|
|
235
|
- // 3)设置排序字段
|
|
|
236
|
- searchParam.setSortBuilders(searchSortHelper.buildSortList(paramMap));
|
|
|
237
|
- return searchParam;
|
|
|
238
|
- }
|
|
|
239
|
} |
178
|
} |