Authored by hugufei

使用CollectionUtils.safeSubList重构sublist

@@ -22,6 +22,7 @@ import org.elasticsearch.search.sort.SortBuilders; @@ -22,6 +22,7 @@ import org.elasticsearch.search.sort.SortBuilders;
22 import org.slf4j.Logger; 22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory; 23 import org.slf4j.LoggerFactory;
24 24
  25 +import com.yoho.search.base.utils.CollectionUtils;
25 import com.yoho.search.service.aggregations.common.FieldSortOrder; 26 import com.yoho.search.service.aggregations.common.FieldSortOrder;
26 import com.yoho.search.service.aggregations.common.KeyTopHitModel; 27 import com.yoho.search.service.aggregations.common.KeyTopHitModel;
27 import com.yoho.search.service.aggregations.common.SimpleFieldAgg; 28 import com.yoho.search.service.aggregations.common.SimpleFieldAgg;
@@ -152,7 +153,7 @@ public class AggCommonHelper { @@ -152,7 +153,7 @@ public class AggCommonHelper {
152 } 153 }
153 }); 154 });
154 if (productList.size() > viewNum) { 155 if (productList.size() > viewNum) {
155 - productList = productList.subList(0, viewNum); 156 + productList = CollectionUtils.safeSubList(productList, 0, viewNum);
156 } 157 }
157 return productList; 158 return productList;
158 } 159 }
@@ -16,6 +16,7 @@ import org.elasticsearch.search.sort.SortOrder; @@ -16,6 +16,7 @@ import org.elasticsearch.search.sort.SortOrder;
16 import org.springframework.beans.factory.annotation.Autowired; 16 import org.springframework.beans.factory.annotation.Autowired;
17 import org.springframework.stereotype.Component; 17 import org.springframework.stereotype.Component;
18 18
  19 +import com.yoho.search.base.utils.CollectionUtils;
19 import com.yoho.search.service.base.index.ProductIndexBaseService; 20 import com.yoho.search.service.base.index.ProductIndexBaseService;
20 21
21 @Component 22 @Component
@@ -102,7 +103,7 @@ public class AggProductListHelper { @@ -102,7 +103,7 @@ public class AggProductListHelper {
102 } 103 }
103 }); 104 });
104 if (productList.size() > viewNum) { 105 if (productList.size() > viewNum) {
105 - productList = productList.subList(0, viewNum); 106 + productList = CollectionUtils.safeSubList(productList, 0, viewNum);
106 } 107 }
107 return productList; 108 return productList;
108 } 109 }
@@ -20,6 +20,7 @@ import org.springframework.beans.factory.annotation.Autowired; @@ -20,6 +20,7 @@ import org.springframework.beans.factory.annotation.Autowired;
20 import org.springframework.stereotype.Service; 20 import org.springframework.stereotype.Service;
21 21
22 import com.alibaba.fastjson.JSONObject; 22 import com.alibaba.fastjson.JSONObject;
  23 +import com.yoho.search.base.utils.CollectionUtils;
23 import com.yoho.search.base.utils.ISearchConstants; 24 import com.yoho.search.base.utils.ISearchConstants;
24 import com.yoho.search.base.utils.ProductIndexEsField; 25 import com.yoho.search.base.utils.ProductIndexEsField;
25 import com.yoho.search.common.cache.CacheType; 26 import com.yoho.search.common.cache.CacheType;
@@ -75,8 +76,8 @@ public class ProductListServiceHelper { @@ -75,8 +76,8 @@ public class ProductListServiceHelper {
75 */ 76 */
76 public SearchApiResult productListForNewPersional(Map<String, String> paramMap) { 77 public SearchApiResult productListForNewPersional(Map<String, String> paramMap) {
77 // 1、召回整数页个skn[需要new一个对象出来,不然原有数据的顺序会变] 78 // 1、召回整数页个skn[需要new一个对象出来,不然原有数据的顺序会变]
78 - CommonRecallResult commonRecallResult = commonRecallSceneService.doCommonPageRecallBatch(paramMap);  
79 - commonRecallResult = new CommonRecallResult(commonRecallResult); 79 + CommonRecallResult cacheObject = commonRecallSceneService.doCommonPageRecallBatch(paramMap);
  80 + CommonRecallResult commonRecallResult = new CommonRecallResult(cacheObject);
80 // 2、获取分页参数 81 // 2、获取分页参数
81 int page = StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page")); 82 int page = StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page"));
82 int viewNum = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum")); 83 int viewNum = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum"));
@@ -123,7 +124,7 @@ public class ProductListServiceHelper { @@ -123,7 +124,7 @@ public class ProductListServiceHelper {
123 if (toIndex > recallSknList.size()) { 124 if (toIndex > recallSknList.size()) {
124 toIndex = recallSknList.size(); 125 toIndex = recallSknList.size();
125 } 126 }
126 - recallSknList = recallSknList.subList(fromIndex, toIndex); 127 + recallSknList = CollectionUtils.safeSubList(recallSknList, fromIndex, toIndex);
127 for (CommonRecallSkn commonRecallSkn : recallSknList) { 128 for (CommonRecallSkn commonRecallSkn : recallSknList) {
128 results.add(productInfoMap.getJSONObject(String.valueOf(commonRecallSkn.getProductSkn()))); 129 results.add(productInfoMap.getJSONObject(String.valueOf(commonRecallSkn.getProductSkn())));
129 } 130 }
@@ -18,6 +18,7 @@ import org.slf4j.LoggerFactory; @@ -18,6 +18,7 @@ import org.slf4j.LoggerFactory;
18 import org.springframework.beans.factory.annotation.Autowired; 18 import org.springframework.beans.factory.annotation.Autowired;
19 import org.springframework.stereotype.Component; 19 import org.springframework.stereotype.Component;
20 20
  21 +import com.yoho.search.base.utils.CollectionUtils;
21 import com.yoho.search.base.utils.ISearchConstants; 22 import com.yoho.search.base.utils.ISearchConstants;
22 import com.yoho.search.base.utils.ProductIndexEsField; 23 import com.yoho.search.base.utils.ProductIndexEsField;
23 import com.yoho.search.common.cache.CacheType; 24 import com.yoho.search.common.cache.CacheType;
@@ -50,7 +51,8 @@ public class CommonPageRecallService extends BaseRecallService { @@ -50,7 +51,8 @@ public class CommonPageRecallService extends BaseRecallService {
50 * @param paramMap 51 * @param paramMap
51 * @return 52 * @return
52 */ 53 */
53 - @SearchCacheAble(cacheInMinute = 10, needMd5=false,cacheName = "COMMON_PAGE_RECALL_BATCH",cacheType=CacheType.EHCACHE,returnClass = CommonRecallResult.class, excludeParams = { "uid", "order", "page" }) 54 + @SearchCacheAble(cacheInMinute = 10, needMd5 = false, cacheName = "COMMON_PAGE_RECALL_BATCH", cacheType = CacheType.EHCACHE, returnClass = CommonRecallResult.class, excludeParams = {
  55 + "uid", "order", "page" })
54 public CommonRecallResult doCommonPageRecallBatch(Map<String, String> paramMap) { 56 public CommonRecallResult doCommonPageRecallBatch(Map<String, String> paramMap) {
55 try { 57 try {
56 int viewNum = this.getViewNum(paramMap); 58 int viewNum = this.getViewNum(paramMap);
@@ -69,7 +71,7 @@ public class CommonPageRecallService extends BaseRecallService { @@ -69,7 +71,7 @@ public class CommonPageRecallService extends BaseRecallService {
69 return commonRecallResult; 71 return commonRecallResult;
70 } 72 }
71 List<CommonRecallSkn> recallSknList = commonRecallResult.getRecallSknList(); 73 List<CommonRecallSkn> recallSknList = commonRecallResult.getRecallSknList();
72 - commonRecallResult.setRecallSknList(new ArrayList<CommonRecallSkn>(recallSknList.subList(0, recallMaxPage * viewNum))); 74 + commonRecallResult.setRecallSknList(CollectionUtils.safeSubList(recallSknList, 0, recallMaxPage * viewNum));
73 return commonRecallResult; 75 return commonRecallResult;
74 } catch (Exception e) { 76 } catch (Exception e) {
75 logger.error(e.getMessage(), e); 77 logger.error(e.getMessage(), e);
@@ -138,7 +140,5 @@ public class CommonPageRecallService extends BaseRecallService { @@ -138,7 +140,5 @@ public class CommonPageRecallService extends BaseRecallService {
138 140
139 return this.buildSearchParam(paramMap, mustFilter, sortBuilders, size); 141 return this.buildSearchParam(paramMap, mustFilter, sortBuilders, size);
140 } 142 }
141 -  
142 -  
143 143
144 } 144 }
@@ -2,6 +2,7 @@ package com.yoho.search.service.scene; @@ -2,6 +2,7 @@ package com.yoho.search.service.scene;
2 2
3 import com.alibaba.fastjson.JSONObject; 3 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.CollectionUtils;
5 import com.yoho.search.base.utils.EventReportEnum; 6 import com.yoho.search.base.utils.EventReportEnum;
6 import com.yoho.search.base.utils.ISearchConstants; 7 import com.yoho.search.base.utils.ISearchConstants;
7 import com.yoho.search.base.utils.ProductIndexEsField; 8 import com.yoho.search.base.utils.ProductIndexEsField;
@@ -16,6 +17,7 @@ import com.yoho.search.service.base.index.ProductIndexBaseService; @@ -16,6 +17,7 @@ import com.yoho.search.service.base.index.ProductIndexBaseService;
16 import com.yoho.search.service.helper.SearchCommonHelper; 17 import com.yoho.search.service.helper.SearchCommonHelper;
17 import com.yoho.search.service.helper.SearchParamHelper; 18 import com.yoho.search.service.helper.SearchParamHelper;
18 import com.yoho.search.service.helper.SearchSortHelper; 19 import com.yoho.search.service.helper.SearchSortHelper;
  20 +
19 import org.apache.commons.lang.StringUtils; 21 import org.apache.commons.lang.StringUtils;
20 import org.elasticsearch.search.SearchHit; 22 import org.elasticsearch.search.SearchHit;
21 import org.elasticsearch.search.SearchHits; 23 import org.elasticsearch.search.SearchHits;
@@ -39,6 +41,7 @@ import org.springframework.context.ApplicationEventPublisherAware; @@ -39,6 +41,7 @@ import org.springframework.context.ApplicationEventPublisherAware;
39 import org.springframework.stereotype.Service; 41 import org.springframework.stereotype.Service;
40 42
41 import javax.annotation.PostConstruct; 43 import javax.annotation.PostConstruct;
  44 +
42 import java.util.*; 45 import java.util.*;
43 46
44 @Service 47 @Service
@@ -336,7 +339,7 @@ public class AggProductListService implements ApplicationEventPublisherAware { @@ -336,7 +339,7 @@ public class AggProductListService implements ApplicationEventPublisherAware {
336 } 339 }
337 }); 340 });
338 if (productList.size() > viewNum) { 341 if (productList.size() > viewNum) {
339 - productList = productList.subList(0, viewNum); 342 + productList = CollectionUtils.safeSubList(productList,0, viewNum);
340 } 343 }
341 return productList; 344 return productList;
342 } 345 }
@@ -25,6 +25,7 @@ import org.springframework.beans.factory.annotation.Autowired; @@ -25,6 +25,7 @@ import org.springframework.beans.factory.annotation.Autowired;
25 import org.springframework.stereotype.Service; 25 import org.springframework.stereotype.Service;
26 26
27 import com.alibaba.fastjson.JSONObject; 27 import com.alibaba.fastjson.JSONObject;
  28 +import com.yoho.search.base.utils.CollectionUtils;
28 import com.yoho.search.base.utils.ISearchConstants; 29 import com.yoho.search.base.utils.ISearchConstants;
29 import com.yoho.search.base.utils.ProductIndexEsField; 30 import com.yoho.search.base.utils.ProductIndexEsField;
30 import com.yoho.search.common.cache.aop.SearchCacheAble; 31 import com.yoho.search.common.cache.aop.SearchCacheAble;
@@ -124,7 +125,7 @@ public class NewGoodProductSceneService extends AbstractCacheAbleService { @@ -124,7 +125,7 @@ public class NewGoodProductSceneService extends AbstractCacheAbleService {
124 // 5、排序【recProductSkns插到1、5、8的位置】以及条数截取 125 // 5、排序【recProductSkns插到1、5、8的位置】以及条数截取
125 results = this.sortEsProductList(results, productSkns, recProductSkns); 126 results = this.sortEsProductList(results, productSkns, recProductSkns);
126 int pageSize = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum")); 127 int pageSize = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum"));
127 - results = results.size() > pageSize ? results.subList(0, pageSize) : results; 128 + results = CollectionUtils.safeSubList(results, 0, pageSize);
128 129
129 // 6、构造返回结果 130 // 6、构造返回结果
130 JSONObject dataMap = new JSONObject(); 131 JSONObject dataMap = new JSONObject();
@@ -15,6 +15,7 @@ import org.springframework.stereotype.Component; @@ -15,6 +15,7 @@ import org.springframework.stereotype.Component;
15 15
16 import com.alibaba.fastjson.JSONArray; 16 import com.alibaba.fastjson.JSONArray;
17 import com.alibaba.fastjson.JSONObject; 17 import com.alibaba.fastjson.JSONObject;
  18 +import com.yoho.search.base.utils.CollectionUtils;
18 import com.yoho.search.base.utils.ISearchConstants; 19 import com.yoho.search.base.utils.ISearchConstants;
19 import com.yoho.search.common.cache.aop.SearchCacheAble; 20 import com.yoho.search.common.cache.aop.SearchCacheAble;
20 import com.yoho.search.core.es.agg.IAggregation; 21 import com.yoho.search.core.es.agg.IAggregation;
@@ -134,7 +135,7 @@ public class SceneAggregationsHelper { @@ -134,7 +135,7 @@ public class SceneAggregationsHelper {
134 if (end > totalCount) { 135 if (end > totalCount) {
135 end = totalCount; 136 end = totalCount;
136 } 137 }
137 - return recommendPromotionList.subList(start, end); 138 + return CollectionUtils.safeSubList(recommendPromotionList, start, end);
138 } catch (Exception e) { 139 } catch (Exception e) {
139 logger.error(e.getMessage(), e); 140 logger.error(e.getMessage(), e);
140 return new ArrayList<Object>(); 141 return new ArrayList<Object>();
@@ -16,6 +16,7 @@ import org.springframework.stereotype.Service; @@ -16,6 +16,7 @@ import org.springframework.stereotype.Service;
16 16
17 import com.alibaba.fastjson.JSONObject; 17 import com.alibaba.fastjson.JSONObject;
18 import com.yoho.search.base.models.RecallType; 18 import com.yoho.search.base.models.RecallType;
  19 +import com.yoho.search.base.utils.CollectionUtils;
19 import com.yoho.search.base.utils.ProductIndexEsField; 20 import com.yoho.search.base.utils.ProductIndexEsField;
20 import com.yoho.search.common.cache.aop.SearchCacheAble; 21 import com.yoho.search.common.cache.aop.SearchCacheAble;
21 import com.yoho.search.models.SearchApiResult; 22 import com.yoho.search.models.SearchApiResult;
@@ -227,7 +228,7 @@ public class SortRecallSceneService extends AbstractRecallService { @@ -227,7 +228,7 @@ public class SortRecallSceneService extends AbstractRecallService {
227 if (maxLength > newProductList.size()) { 228 if (maxLength > newProductList.size()) {
228 maxLength = newProductList.size(); 229 maxLength = newProductList.size();
229 } 230 }
230 - recallResult.setProductList(newProductList.subList(0,maxLength)); 231 + recallResult.setProductList(CollectionUtils.safeSubList(newProductList, 0,maxLength));
231 return recallResult; 232 return recallResult;
232 } 233 }
233 234
@@ -291,7 +291,7 @@ public class RecallServiceHelper { @@ -291,7 +291,7 @@ public class RecallServiceHelper {
291 if (toIndex > recalledSknList.size()) { 291 if (toIndex > recalledSknList.size()) {
292 toIndex = recalledSknList.size(); 292 toIndex = recalledSknList.size();
293 } 293 }
294 - List<Integer> querySknList = recalledSknList.subList(fromIndex, toIndex); 294 + List<Integer> querySknList = CollectionUtils.safeSubList(recalledSknList, fromIndex, toIndex);
295 // 1.构造搜索参数 295 // 1.构造搜索参数
296 SearchParam searchParam = new SearchParam(); 296 SearchParam searchParam = new SearchParam();
297 searchParam.setFiter(QueryBuilders.boolQuery().must(QueryBuilders.termsQuery(ProductIndexEsField.productSkn, querySknList))); 297 searchParam.setFiter(QueryBuilders.boolQuery().must(QueryBuilders.termsQuery(ProductIndexEsField.productSkn, querySknList)));
1 package com.yoho.search.service.scene.searchlike; 1 package com.yoho.search.service.scene.searchlike;
2 2
3 import com.alibaba.fastjson.JSONObject; 3 import com.alibaba.fastjson.JSONObject;
  4 +import com.yoho.search.base.utils.CollectionUtils;
4 import com.yoho.search.base.utils.ProductIndexEsField; 5 import com.yoho.search.base.utils.ProductIndexEsField;
5 import com.yoho.search.common.cache.aop.SearchCacheAble; 6 import com.yoho.search.common.cache.aop.SearchCacheAble;
6 import com.yoho.search.core.es.model.SearchParam; 7 import com.yoho.search.core.es.model.SearchParam;
7 import com.yoho.search.models.SearchApiResult; 8 import com.yoho.search.models.SearchApiResult;
8 import com.yoho.search.service.base.SearchRequestParams; 9 import com.yoho.search.service.base.SearchRequestParams;
9 import com.yoho.search.service.base.index.ProductIndexBaseService; 10 import com.yoho.search.service.base.index.ProductIndexBaseService;
  11 +
10 import org.apache.commons.lang.StringUtils; 12 import org.apache.commons.lang.StringUtils;
11 import org.elasticsearch.index.query.BoolQueryBuilder; 13 import org.elasticsearch.index.query.BoolQueryBuilder;
12 import org.elasticsearch.index.query.QueryBuilder; 14 import org.elasticsearch.index.query.QueryBuilder;
@@ -69,7 +71,7 @@ public class SearchLikeInShopService { @@ -69,7 +71,7 @@ public class SearchLikeInShopService {
69 // 5、获取搜索结果[截取条数] 71 // 5、获取搜索结果[截取条数]
70 List<Map<String, Object>> tempProductList = searchLikeHelper.queryProductList(searchParams); 72 List<Map<String, Object>> tempProductList = searchLikeHelper.queryProductList(searchParams);
71 if (tempProductList.size() > pageSize) { 73 if (tempProductList.size() > pageSize) {
72 - tempProductList = tempProductList.subList(0, pageSize); 74 + tempProductList = CollectionUtils.safeSubList(tempProductList,0, pageSize);
73 } 75 }
74 76
75 // 6、构造真实返回结果 77 // 6、构造真实返回结果
1 package com.yoho.search.service.scene.searchlike; 1 package com.yoho.search.service.scene.searchlike;
2 2
3 import com.alibaba.fastjson.JSONObject; 3 import com.alibaba.fastjson.JSONObject;
  4 +import com.yoho.search.base.utils.CollectionUtils;
4 import com.yoho.search.base.utils.ProductIndexEsField; 5 import com.yoho.search.base.utils.ProductIndexEsField;
5 import com.yoho.search.common.cache.aop.SearchCacheAble; 6 import com.yoho.search.common.cache.aop.SearchCacheAble;
6 import com.yoho.search.core.es.model.SearchParam; 7 import com.yoho.search.core.es.model.SearchParam;
@@ -8,6 +9,7 @@ import com.yoho.search.models.SearchApiResult; @@ -8,6 +9,7 @@ import com.yoho.search.models.SearchApiResult;
8 import com.yoho.search.service.base.SearchDynamicConfigService; 9 import com.yoho.search.service.base.SearchDynamicConfigService;
9 import com.yoho.search.service.base.SearchRequestParams; 10 import com.yoho.search.service.base.SearchRequestParams;
10 import com.yoho.search.service.base.index.ProductIndexBaseService; 11 import com.yoho.search.service.base.index.ProductIndexBaseService;
  12 +
11 import org.apache.commons.lang.StringUtils; 13 import org.apache.commons.lang.StringUtils;
12 import org.elasticsearch.index.query.BoolQueryBuilder; 14 import org.elasticsearch.index.query.BoolQueryBuilder;
13 import org.elasticsearch.index.query.QueryBuilder; 15 import org.elasticsearch.index.query.QueryBuilder;
@@ -75,7 +77,7 @@ public class SearchLikeNotInShopService{ @@ -75,7 +77,7 @@ public class SearchLikeNotInShopService{
75 // 4、获取搜索结果[截取条数] 77 // 4、获取搜索结果[截取条数]
76 List<Map<String, Object>> tempProductList = searchLikeHelper.queryProductList(searchParams); 78 List<Map<String, Object>> tempProductList = searchLikeHelper.queryProductList(searchParams);
77 if (tempProductList.size() > pageSize) { 79 if (tempProductList.size() > pageSize) {
78 - tempProductList = tempProductList.subList(0, pageSize); 80 + tempProductList = CollectionUtils.safeSubList(tempProductList,0, pageSize);
79 } 81 }
80 82
81 // 5、构造真实返回结果 83 // 5、构造真实返回结果
1 package com.yoho.search.service.scene.searchlike; 1 package com.yoho.search.service.scene.searchlike;
2 2
3 import com.alibaba.fastjson.JSONObject; 3 import com.alibaba.fastjson.JSONObject;
  4 +import com.yoho.search.base.utils.CollectionUtils;
4 import com.yoho.search.base.utils.ProductIndexEsField; 5 import com.yoho.search.base.utils.ProductIndexEsField;
5 import com.yoho.search.common.cache.aop.SearchCacheAble; 6 import com.yoho.search.common.cache.aop.SearchCacheAble;
6 import com.yoho.search.core.es.model.SearchParam; 7 import com.yoho.search.core.es.model.SearchParam;
7 import com.yoho.search.models.SearchApiResult; 8 import com.yoho.search.models.SearchApiResult;
8 import com.yoho.search.service.base.SearchRequestParams; 9 import com.yoho.search.service.base.SearchRequestParams;
9 import com.yoho.search.service.base.index.ProductIndexBaseService; 10 import com.yoho.search.service.base.index.ProductIndexBaseService;
  11 +
10 import org.apache.commons.lang.StringUtils; 12 import org.apache.commons.lang.StringUtils;
11 import org.elasticsearch.index.query.BoolQueryBuilder; 13 import org.elasticsearch.index.query.BoolQueryBuilder;
12 import org.elasticsearch.index.query.QueryBuilder; 14 import org.elasticsearch.index.query.QueryBuilder;
@@ -70,7 +72,7 @@ public class SearchLikeSceneService { @@ -70,7 +72,7 @@ public class SearchLikeSceneService {
70 // 5、获取搜索结果[并截取条数] 72 // 5、获取搜索结果[并截取条数]
71 List<Map<String, Object>> tempProductList = searchLikeHelper.queryProductList(searchParams); 73 List<Map<String, Object>> tempProductList = searchLikeHelper.queryProductList(searchParams);
72 if (tempProductList.size() > pageSize) { 74 if (tempProductList.size() > pageSize) {
73 - tempProductList = tempProductList.subList(0, pageSize); 75 + tempProductList = CollectionUtils.safeSubList(tempProductList,0, pageSize);
74 } 76 }
75 77
76 // 6、构造真实返回结果 78 // 6、构造真实返回结果