Authored by hugufei

使用CollectionUtils.safeSubList重构sublist

... ... @@ -22,6 +22,7 @@ import org.elasticsearch.search.sort.SortBuilders;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.yoho.search.base.utils.CollectionUtils;
import com.yoho.search.service.aggregations.common.FieldSortOrder;
import com.yoho.search.service.aggregations.common.KeyTopHitModel;
import com.yoho.search.service.aggregations.common.SimpleFieldAgg;
... ... @@ -152,7 +153,7 @@ public class AggCommonHelper {
}
});
if (productList.size() > viewNum) {
productList = productList.subList(0, viewNum);
productList = CollectionUtils.safeSubList(productList, 0, viewNum);
}
return productList;
}
... ...
... ... @@ -16,6 +16,7 @@ import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.yoho.search.base.utils.CollectionUtils;
import com.yoho.search.service.base.index.ProductIndexBaseService;
@Component
... ... @@ -102,7 +103,7 @@ public class AggProductListHelper {
}
});
if (productList.size() > viewNum) {
productList = productList.subList(0, viewNum);
productList = CollectionUtils.safeSubList(productList, 0, viewNum);
}
return productList;
}
... ...
... ... @@ -20,6 +20,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONObject;
import com.yoho.search.base.utils.CollectionUtils;
import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.base.utils.ProductIndexEsField;
import com.yoho.search.common.cache.CacheType;
... ... @@ -75,8 +76,8 @@ public class ProductListServiceHelper {
*/
public SearchApiResult productListForNewPersional(Map<String, String> paramMap) {
// 1、召回整数页个skn[需要new一个对象出来,不然原有数据的顺序会变]
CommonRecallResult commonRecallResult = commonRecallSceneService.doCommonPageRecallBatch(paramMap);
commonRecallResult = new CommonRecallResult(commonRecallResult);
CommonRecallResult cacheObject = commonRecallSceneService.doCommonPageRecallBatch(paramMap);
CommonRecallResult commonRecallResult = new CommonRecallResult(cacheObject);
// 2、获取分页参数
int page = StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page"));
int viewNum = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum"));
... ... @@ -123,7 +124,7 @@ public class ProductListServiceHelper {
if (toIndex > recallSknList.size()) {
toIndex = recallSknList.size();
}
recallSknList = recallSknList.subList(fromIndex, toIndex);
recallSknList = CollectionUtils.safeSubList(recallSknList, fromIndex, toIndex);
for (CommonRecallSkn commonRecallSkn : recallSknList) {
results.add(productInfoMap.getJSONObject(String.valueOf(commonRecallSkn.getProductSkn())));
}
... ...
... ... @@ -18,6 +18,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.yoho.search.base.utils.CollectionUtils;
import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.base.utils.ProductIndexEsField;
import com.yoho.search.common.cache.CacheType;
... ... @@ -50,7 +51,8 @@ public class CommonPageRecallService extends BaseRecallService {
* @param paramMap
* @return
*/
@SearchCacheAble(cacheInMinute = 10, needMd5=false,cacheName = "COMMON_PAGE_RECALL_BATCH",cacheType=CacheType.EHCACHE,returnClass = CommonRecallResult.class, excludeParams = { "uid", "order", "page" })
@SearchCacheAble(cacheInMinute = 10, needMd5 = false, cacheName = "COMMON_PAGE_RECALL_BATCH", cacheType = CacheType.EHCACHE, returnClass = CommonRecallResult.class, excludeParams = {
"uid", "order", "page" })
public CommonRecallResult doCommonPageRecallBatch(Map<String, String> paramMap) {
try {
int viewNum = this.getViewNum(paramMap);
... ... @@ -69,7 +71,7 @@ public class CommonPageRecallService extends BaseRecallService {
return commonRecallResult;
}
List<CommonRecallSkn> recallSknList = commonRecallResult.getRecallSknList();
commonRecallResult.setRecallSknList(new ArrayList<CommonRecallSkn>(recallSknList.subList(0, recallMaxPage * viewNum)));
commonRecallResult.setRecallSknList(CollectionUtils.safeSubList(recallSknList, 0, recallMaxPage * viewNum));
return commonRecallResult;
} catch (Exception e) {
logger.error(e.getMessage(), e);
... ... @@ -138,7 +140,5 @@ public class CommonPageRecallService extends BaseRecallService {
return this.buildSearchParam(paramMap, mustFilter, sortBuilders, size);
}
}
... ...
... ... @@ -2,6 +2,7 @@ package com.yoho.search.service.scene;
import com.alibaba.fastjson.JSONObject;
import com.yoho.error.event.SearchEvent;
import com.yoho.search.base.utils.CollectionUtils;
import com.yoho.search.base.utils.EventReportEnum;
import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.base.utils.ProductIndexEsField;
... ... @@ -16,6 +17,7 @@ import com.yoho.search.service.base.index.ProductIndexBaseService;
import com.yoho.search.service.helper.SearchCommonHelper;
import com.yoho.search.service.helper.SearchParamHelper;
import com.yoho.search.service.helper.SearchSortHelper;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
... ... @@ -39,6 +41,7 @@ import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.util.*;
@Service
... ... @@ -336,7 +339,7 @@ public class AggProductListService implements ApplicationEventPublisherAware {
}
});
if (productList.size() > viewNum) {
productList = productList.subList(0, viewNum);
productList = CollectionUtils.safeSubList(productList,0, viewNum);
}
return productList;
}
... ...
... ... @@ -25,6 +25,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONObject;
import com.yoho.search.base.utils.CollectionUtils;
import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.base.utils.ProductIndexEsField;
import com.yoho.search.common.cache.aop.SearchCacheAble;
... ... @@ -124,7 +125,7 @@ public class NewGoodProductSceneService extends AbstractCacheAbleService {
// 5、排序【recProductSkns插到1、5、8的位置】以及条数截取
results = this.sortEsProductList(results, productSkns, recProductSkns);
int pageSize = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum"));
results = results.size() > pageSize ? results.subList(0, pageSize) : results;
results = CollectionUtils.safeSubList(results, 0, pageSize);
// 6、构造返回结果
JSONObject dataMap = new JSONObject();
... ...
... ... @@ -15,6 +15,7 @@ import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yoho.search.base.utils.CollectionUtils;
import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.common.cache.aop.SearchCacheAble;
import com.yoho.search.core.es.agg.IAggregation;
... ... @@ -134,7 +135,7 @@ public class SceneAggregationsHelper {
if (end > totalCount) {
end = totalCount;
}
return recommendPromotionList.subList(start, end);
return CollectionUtils.safeSubList(recommendPromotionList, start, end);
} catch (Exception e) {
logger.error(e.getMessage(), e);
return new ArrayList<Object>();
... ...
... ... @@ -16,6 +16,7 @@ import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONObject;
import com.yoho.search.base.models.RecallType;
import com.yoho.search.base.utils.CollectionUtils;
import com.yoho.search.base.utils.ProductIndexEsField;
import com.yoho.search.common.cache.aop.SearchCacheAble;
import com.yoho.search.models.SearchApiResult;
... ... @@ -227,7 +228,7 @@ public class SortRecallSceneService extends AbstractRecallService {
if (maxLength > newProductList.size()) {
maxLength = newProductList.size();
}
recallResult.setProductList(newProductList.subList(0,maxLength));
recallResult.setProductList(CollectionUtils.safeSubList(newProductList, 0,maxLength));
return recallResult;
}
... ...
... ... @@ -291,7 +291,7 @@ public class RecallServiceHelper {
if (toIndex > recalledSknList.size()) {
toIndex = recalledSknList.size();
}
List<Integer> querySknList = recalledSknList.subList(fromIndex, toIndex);
List<Integer> querySknList = CollectionUtils.safeSubList(recalledSknList, fromIndex, toIndex);
// 1.构造搜索参数
SearchParam searchParam = new SearchParam();
searchParam.setFiter(QueryBuilders.boolQuery().must(QueryBuilders.termsQuery(ProductIndexEsField.productSkn, querySknList)));
... ...
package com.yoho.search.service.scene.searchlike;
import com.alibaba.fastjson.JSONObject;
import com.yoho.search.base.utils.CollectionUtils;
import com.yoho.search.base.utils.ProductIndexEsField;
import com.yoho.search.common.cache.aop.SearchCacheAble;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.models.SearchApiResult;
import com.yoho.search.service.base.SearchRequestParams;
import com.yoho.search.service.base.index.ProductIndexBaseService;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
... ... @@ -69,7 +71,7 @@ public class SearchLikeInShopService {
// 5、获取搜索结果[截取条数]
List<Map<String, Object>> tempProductList = searchLikeHelper.queryProductList(searchParams);
if (tempProductList.size() > pageSize) {
tempProductList = tempProductList.subList(0, pageSize);
tempProductList = CollectionUtils.safeSubList(tempProductList,0, pageSize);
}
// 6、构造真实返回结果
... ...
package com.yoho.search.service.scene.searchlike;
import com.alibaba.fastjson.JSONObject;
import com.yoho.search.base.utils.CollectionUtils;
import com.yoho.search.base.utils.ProductIndexEsField;
import com.yoho.search.common.cache.aop.SearchCacheAble;
import com.yoho.search.core.es.model.SearchParam;
... ... @@ -8,6 +9,7 @@ import com.yoho.search.models.SearchApiResult;
import com.yoho.search.service.base.SearchDynamicConfigService;
import com.yoho.search.service.base.SearchRequestParams;
import com.yoho.search.service.base.index.ProductIndexBaseService;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
... ... @@ -75,7 +77,7 @@ public class SearchLikeNotInShopService{
// 4、获取搜索结果[截取条数]
List<Map<String, Object>> tempProductList = searchLikeHelper.queryProductList(searchParams);
if (tempProductList.size() > pageSize) {
tempProductList = tempProductList.subList(0, pageSize);
tempProductList = CollectionUtils.safeSubList(tempProductList,0, pageSize);
}
// 5、构造真实返回结果
... ...
package com.yoho.search.service.scene.searchlike;
import com.alibaba.fastjson.JSONObject;
import com.yoho.search.base.utils.CollectionUtils;
import com.yoho.search.base.utils.ProductIndexEsField;
import com.yoho.search.common.cache.aop.SearchCacheAble;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.models.SearchApiResult;
import com.yoho.search.service.base.SearchRequestParams;
import com.yoho.search.service.base.index.ProductIndexBaseService;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
... ... @@ -70,7 +72,7 @@ public class SearchLikeSceneService {
// 5、获取搜索结果[并截取条数]
List<Map<String, Object>> tempProductList = searchLikeHelper.queryProductList(searchParams);
if (tempProductList.size() > pageSize) {
tempProductList = tempProductList.subList(0, pageSize);
tempProductList = CollectionUtils.safeSubList(tempProductList,0, pageSize);
}
// 6、构造真实返回结果
... ...