Authored by 胡古飞

添加有好货的逻辑

@@ -166,21 +166,28 @@ public class GoodProductListService implements IGoodProductsService { @@ -166,21 +166,28 @@ public class GoodProductListService implements IGoodProductsService {
166 searchParam.setPage(0); 166 searchParam.setPage(0);
167 searchParam.setSize(0); 167 searchParam.setSize(0);
168 searchParam.setOffset(0); 168 searchParam.setOffset(0);
169 - // 4、执行搜索 169 +
  170 + // 4、先从缓存中获取,如果能取到,则直接返回
  171 + JSONArray sknSmallSortIdJSONArray = searchCacheService.getJSONArrayFromCache(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
  172 + if (sknSmallSortIdJSONArray != null) {
  173 + return this.jsonArrayToList(sknSmallSortIdJSONArray,Integer.class);
  174 + }
  175 + // 5、执行搜索
170 SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam); 176 SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
171 if (searchResult == null || searchResult.getAggMaps() == null || searchResult.getAggMaps().get("smallSortAgg") == null) { 177 if (searchResult == null || searchResult.getAggMaps() == null || searchResult.getAggMaps().get("smallSortAgg") == null) {
172 return new ArrayList<Integer>(); 178 return new ArrayList<Integer>();
173 } 179 }
174 - List<Integer> results = new ArrayList<Integer>(); 180 + sknSmallSortIdJSONArray = new JSONArray();
175 MultiBucketsAggregation aggregation = (MultiBucketsAggregation) searchResult.getAggMaps().get(aggName); 181 MultiBucketsAggregation aggregation = (MultiBucketsAggregation) searchResult.getAggMaps().get(aggName);
176 Iterator<? extends Bucket> smallSortIdIterator = aggregation.getBuckets().iterator(); 182 Iterator<? extends Bucket> smallSortIdIterator = aggregation.getBuckets().iterator();
177 while (smallSortIdIterator.hasNext()) { 183 while (smallSortIdIterator.hasNext()) {
178 Bucket smallSortIdBucket = smallSortIdIterator.next(); 184 Bucket smallSortIdBucket = smallSortIdIterator.next();
179 if (StringUtils.isNumeric(smallSortIdBucket.getKeyAsString())) { 185 if (StringUtils.isNumeric(smallSortIdBucket.getKeyAsString())) {
180 - results.add(Integer.valueOf(smallSortIdBucket.getKeyAsString())); 186 + sknSmallSortIdJSONArray.add(Integer.valueOf(smallSortIdBucket.getKeyAsString()));
181 } 187 }
182 } 188 }
183 - return results; 189 + searchCacheService.addJSONArrayToCache(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam, sknSmallSortIdJSONArray);
  190 + return this.jsonArrayToList(sknSmallSortIdJSONArray,Integer.class);
184 } 191 }
185 192
186 /** 193 /**
@@ -213,6 +220,7 @@ public class GoodProductListService implements IGoodProductsService { @@ -213,6 +220,7 @@ public class GoodProductListService implements IGoodProductsService {
213 // 2、构造filter 220 // 2、构造filter
214 BoolQueryBuilder boolFilter = this.getDefaultBoolQueryBuilder(); 221 BoolQueryBuilder boolFilter = this.getDefaultBoolQueryBuilder();
215 boolFilter.must(QueryBuilders.termsQuery("smallSortId", smallSortIds)); 222 boolFilter.must(QueryBuilders.termsQuery("smallSortId", smallSortIds));
  223 + searchParam.setFiter(boolFilter);
216 224
217 // 3、构造query[针对用户做个性化打分] 225 // 3、构造query[针对用户做个性化打分]
218 searchParam.setQuery(searchServiceHelper.constructQueryBuilderForProductList(paramMap)); 226 searchParam.setQuery(searchServiceHelper.constructQueryBuilderForProductList(paramMap));
@@ -240,7 +248,7 @@ public class GoodProductListService implements IGoodProductsService { @@ -240,7 +248,7 @@ public class GoodProductListService implements IGoodProductsService {
240 // 6、先从缓存中获取,如果能取到,则直接返回 248 // 6、先从缓存中获取,如果能取到,则直接返回
241 JSONArray recommendedSknJSONArray = searchCacheService.getJSONArrayFromCache(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam); 249 JSONArray recommendedSknJSONArray = searchCacheService.getJSONArrayFromCache(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
242 if (recommendedSknJSONArray != null) { 250 if (recommendedSknJSONArray != null) {
243 - return this.jsonArrayToList(recommendedSknJSONArray); 251 + return this.jsonArrayToList(recommendedSknJSONArray,String.class);
244 } 252 }
245 // 7、执行搜索,并构造返回结果 253 // 7、执行搜索,并构造返回结果
246 final String indexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX; 254 final String indexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
@@ -258,15 +266,11 @@ public class GoodProductListService implements IGoodProductsService { @@ -258,15 +266,11 @@ public class GoodProductListService implements IGoodProductsService {
258 recommendedSknJSONArray.add(recommendedSkn); 266 recommendedSknJSONArray.add(recommendedSkn);
259 } 267 }
260 searchCacheService.addJSONArrayToCache(indexName, searchParam, recommendedSknJSONArray); 268 searchCacheService.addJSONArrayToCache(indexName, searchParam, recommendedSknJSONArray);
261 - return this.jsonArrayToList(recommendedSknJSONArray); 269 + return this.jsonArrayToList(recommendedSknJSONArray,String.class);
262 } 270 }
263 271
264 - private List<String> jsonArrayToList(JSONArray jsonArray) {  
265 - List<String> results = new ArrayList<String>();  
266 - for (int i = 0; i < jsonArray.size(); i++) {  
267 - results.add(jsonArray.getString(i));  
268 - }  
269 - return results; 272 + private <T> List<T> jsonArrayToList(JSONArray jsonArray,Class<T> clazz){
  273 + return JSONObject.parseArray(jsonArray.toJSONString(), clazz);
270 } 274 }
271 275
272 private List<String> getRecommendedSknList(MultiBucketsAggregation aggregation) { 276 private List<String> getRecommendedSknList(MultiBucketsAggregation aggregation) {