Authored by hugufei

单skn查询时也使用缓存

1 package com.yoho.search.recall.scene; 1 package com.yoho.search.recall.scene;
2 2
3 import com.alibaba.fastjson.JSONObject; 3 import com.alibaba.fastjson.JSONObject;
  4 +import com.yoho.search.base.utils.ISearchConstants;
  5 +import com.yoho.search.base.utils.ProductIndexEsField;
  6 +import com.yoho.search.core.es.model.SearchParam;
  7 +import com.yoho.search.core.es.model.SearchResult;
4 import com.yoho.search.models.SearchApiResult; 8 import com.yoho.search.models.SearchApiResult;
5 import com.yoho.search.recall.scene.component.*; 9 import com.yoho.search.recall.scene.component.*;
  10 +import com.yoho.search.recall.scene.helper.SortBuilderHelper;
6 import com.yoho.search.recall.scene.models.*; 11 import com.yoho.search.recall.scene.models.*;
7 import com.yoho.search.recall.scene.persional.PersionalFactor; 12 import com.yoho.search.recall.scene.persional.PersionalFactor;
8 import com.yoho.search.recall.scene.persional.RecallPersionalService; 13 import com.yoho.search.recall.scene.persional.RecallPersionalService;
  14 +import com.yoho.search.service.base.SearchCommonService;
  15 +import com.yoho.search.service.base.index.ProductIndexBaseService;
9 import com.yoho.search.service.helper.SearchCommonHelper; 16 import com.yoho.search.service.helper.SearchCommonHelper;
10 import org.apache.commons.collections.MapUtils; 17 import org.apache.commons.collections.MapUtils;
  18 +import org.elasticsearch.index.query.BoolQueryBuilder;
  19 +import org.elasticsearch.index.query.QueryBuilders;
  20 +import org.elasticsearch.search.sort.SortBuilder;
11 import org.slf4j.Logger; 21 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory; 22 import org.slf4j.LoggerFactory;
13 import org.springframework.beans.factory.annotation.Autowired; 23 import org.springframework.beans.factory.annotation.Autowired;
14 import org.springframework.stereotype.Component; 24 import org.springframework.stereotype.Component;
15 25
  26 +import java.util.ArrayList;
16 import java.util.List; 27 import java.util.List;
17 import java.util.Map; 28 import java.util.Map;
18 29
@@ -35,6 +46,12 @@ public class SceneRecallService { @@ -35,6 +46,12 @@ public class SceneRecallService {
35 private RecallResultBuilder recallResultBuilder; 46 private RecallResultBuilder recallResultBuilder;
36 @Autowired 47 @Autowired
37 private SearchCommonHelper searchCommonHelper; 48 private SearchCommonHelper searchCommonHelper;
  49 + @Autowired
  50 + private ProductIndexBaseService productIndexBaseService;
  51 + @Autowired
  52 + private SearchCommonService searchCommonService;
  53 + @Autowired
  54 + private BacthQueryBySknComponent bacthQueryBySknComponent;
38 55
39 public SearchApiResult sceneRecall(Map<String, String> paramMap) { 56 public SearchApiResult sceneRecall(Map<String, String> paramMap) {
40 try { 57 try {
@@ -50,14 +67,15 @@ public class SceneRecallService { @@ -50,14 +67,15 @@ public class SceneRecallService {
50 PersionalFactor persionalFactor = recallPersionalService.queryPersionalFactor(recallParams); 67 PersionalFactor persionalFactor = recallPersionalService.queryPersionalFactor(recallParams);
51 //4、执行召回 68 //4、执行召回
52 RecallResult recallResult = this.doRecall(recallParams, persionalFactor); 69 RecallResult recallResult = this.doRecall(recallParams, persionalFactor);
53 - //TODO  
54 - //5、构造返回结果 70 + //5、根据召回结果查询商品信息
  71 + List<Map<String, Object>> productList = this.queryProductList(recallParams,recallResult);
  72 + //6、构造返回结果
55 JSONObject dataMap = new JSONObject(); 73 JSONObject dataMap = new JSONObject();
56 dataMap.put("total", recallResult.getTotal()); 74 dataMap.put("total", recallResult.getTotal());
57 dataMap.put("page", recallParams.getPage()); 75 dataMap.put("page", recallParams.getPage());
58 dataMap.put("page_size", recallParams.getPageSize()); 76 dataMap.put("page_size", recallParams.getPageSize());
59 - dataMap.put("page_total", searchCommonHelper.getTotalPage(recallResult.getTotal(),recallParams.getPageSize()));  
60 - dataMap.put("product_list",recallResult); 77 + dataMap.put("page_total", searchCommonHelper.getTotalPage(recallResult.getTotal(), recallParams.getPageSize()));
  78 + dataMap.put("product_list", productList);
61 return new SearchApiResult().setData(dataMap); 79 return new SearchApiResult().setData(dataMap);
62 } catch (Exception e) { 80 } catch (Exception e) {
63 logger.error(e.getMessage(), e); 81 logger.error(e.getMessage(), e);
@@ -65,6 +83,13 @@ public class SceneRecallService { @@ -65,6 +83,13 @@ public class SceneRecallService {
65 } 83 }
66 } 84 }
67 85
  86 + /**
  87 + * 召回入口
  88 + *
  89 + * @param param
  90 + * @param persionalFactor
  91 + * @return
  92 + */
68 private RecallResult doRecall(RecallParams param, PersionalFactor persionalFactor) { 93 private RecallResult doRecall(RecallParams param, PersionalFactor persionalFactor) {
69 //1、构造请求 94 //1、构造请求
70 List<RecallRequest> batchRequests = batchRequestsBuilder.buildBatchRequests(param, persionalFactor); 95 List<RecallRequest> batchRequests = batchRequestsBuilder.buildBatchRequests(param, persionalFactor);
@@ -77,4 +102,48 @@ public class SceneRecallService { @@ -77,4 +102,48 @@ public class SceneRecallService {
77 return recallResult; 102 return recallResult;
78 } 103 }
79 104
  105 + private List<Map<String, Object>> queryProductList(RecallParams recallParams,RecallResult recallResult) {
  106 + //1、
  107 + List<Integer> productSkns = recallResult.getSknList();
  108 + if (productSkns != null && !productSkns.isEmpty()) {
  109 + return bacthQueryBySknComponent.queryProductListBySkn(productSkns,productSkns.size());
  110 + }else{
  111 + return this.queryProductByFilterSkn(recallResult.getNotProductSkn(),recallResult.getRealPage(),recallParams.getPageSize());
  112 + }
  113 + }
  114 +
  115 + /**
  116 + * 过滤掉已召回的skn,并按人气排序
  117 + * @param notProductSkns
  118 + * @param page
  119 + * @param pageSize
  120 + * @return
  121 + */
  122 + private List<Map<String, Object>> queryProductByFilterSkn(List<Integer> notProductSkns,int page,int pageSize){
  123 + SearchParam searchParam = new SearchParam();
  124 + //设置分页参数
  125 + searchParam.setOffset((page-1)*pageSize);
  126 + searchParam.setSize(pageSize);
  127 +
  128 + //设置filter
  129 + BoolQueryBuilder filter = QueryBuilders.boolQuery();
  130 + filter.mustNot(QueryBuilders.termsQuery(ProductIndexEsField.productSkn,notProductSkns));
  131 +
  132 + //设置IncludeFields
  133 + searchParam.setIncludeFields(productIndexBaseService.getProductIndexIncludeFields());
  134 +
  135 + //设置排序
  136 + List<SortBuilder<?>> sortBuilders = new ArrayList<>();
  137 + sortBuilders.add(SortBuilderHelper.getHeatValueDescSort());
  138 + sortBuilders.add(SortBuilderHelper.getSevendayMoneyDescSort());
  139 + sortBuilders.add(SortBuilderHelper.getIdDescSort());
  140 + searchParam.setSortBuilders(sortBuilders);
  141 + SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX,searchParam);
  142 + return productIndexBaseService.getProductListWithPricePlan(searchResult.getResultList());
  143 + }
  144 +
  145 +
  146 +
  147 +
  148 +
80 } 149 }
  1 +package com.yoho.search.recall.scene.component;
  2 +
  3 +
  4 +import com.alibaba.fastjson.JSON;
  5 +import com.alibaba.fastjson.JSONObject;
  6 +import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
  7 +import com.yoho.search.base.utils.ISearchConstants;
  8 +import com.yoho.search.base.utils.ProductIndexEsField;
  9 +import com.yoho.search.base.utils.Transfer;
  10 +import com.yoho.search.common.cache.impls.SearchRedis;
  11 +import com.yoho.search.core.es.model.SearchParam;
  12 +import com.yoho.search.core.es.model.SearchResult;
  13 +import com.yoho.search.recall.scene.models.CacheRequestResponse;
  14 +import com.yoho.search.service.base.SearchCommonService;
  15 +import com.yoho.search.service.base.index.ProductIndexBaseService;
  16 +import org.apache.commons.collections.MapUtils;
  17 +import org.apache.commons.lang.StringUtils;
  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.Component;
  23 +
  24 +import java.util.*;
  25 +
  26 +@Component
  27 +public class BacthQueryBySknComponent {
  28 +
  29 + private static final Logger logger = LoggerFactory.getLogger(BacthQueryBySknComponent.class);
  30 +
  31 + @Autowired
  32 + private SearchRedis searchRedis;
  33 + @Autowired
  34 + private SearchCommonService searchCommonService;
  35 + @Autowired
  36 + private ProductIndexBaseService productIndexBaseService;
  37 +
  38 + /**
  39 + * 按skn查询并按顺序返回
  40 + *
  41 + * @param productSkns
  42 + * @return
  43 + */
  44 + public List<Map<String, Object>> queryProductListBySkn(List<Integer> productSkns,int size){
  45 + //1、构建请求
  46 + List<CacheRequestResponse<Integer, Map<String, Object>>> sknInfoRequestResponses = new ArrayList<>();
  47 + for (Integer productSkn : productSkns) {
  48 + sknInfoRequestResponses.add(new CacheRequestResponse(productSkn,redisKeyBuilderTransfer(),toValueTransfer(),fromValueTransfer()));
  49 + }
  50 +
  51 + //2、批量从缓存中获取
  52 + sknInfoRequestResponses = this.batchQueryFromCache(sknInfoRequestResponses);
  53 +
  54 + //3、获取未命中缓存的skn
  55 + List<Integer> notCacheSkns = new ArrayList<>();
  56 + for (CacheRequestResponse<Integer, Map<String, Object>> cacheRequestResponse : sknInfoRequestResponses) {
  57 + if(cacheRequestResponse.getValueObject()==null){
  58 + notCacheSkns.add(cacheRequestResponse.getKeyObject());
  59 + }
  60 +
  61 + }
  62 +
  63 + //4、执行批量查询
  64 + Map<Integer,Map<String, Object>> queryResults = this.batchQuery(notCacheSkns);
  65 +
  66 + //5、填充查询结果
  67 + for (CacheRequestResponse<Integer, Map<String, Object>> cacheRequestResponse : sknInfoRequestResponses) {
  68 + if(cacheRequestResponse.getValueObject()==null){
  69 + cacheRequestResponse.setValueObject(queryResults.get(cacheRequestResponse.getKeyObject()));
  70 + cacheRequestResponse.setNeedRecache(true);
  71 + }
  72 + }
  73 +
  74 + //6、将CacheRequestResponse中需要缓存的key加入缓存
  75 + this.batchAddToCache(sknInfoRequestResponses,10*60);//缓存10分钟
  76 +
  77 + //7、构造返回结果
  78 + Map<Integer,Map<String, Object>> productInfoMap = new HashMap<>();
  79 + for (CacheRequestResponse<Integer, Map<String, Object>> sknInfoRequestResponse: sknInfoRequestResponses) {
  80 + productInfoMap.put(sknInfoRequestResponse.getKeyObject(),sknInfoRequestResponse.getValueObject());
  81 + }
  82 + List<Map<String, Object>> finalResults = new ArrayList<>();
  83 + for (Integer productSkn : productSkns) {
  84 + if(productInfoMap.get(productSkn)!=null){
  85 + finalResults.add(productInfoMap.get(productSkn));
  86 + }
  87 + if(finalResults.size()>=size){
  88 + break;
  89 + }
  90 + }
  91 + return finalResults;
  92 + }
  93 +
  94 +
  95 + private <K, V> List<CacheRequestResponse<K, V>> batchQueryFromCache(List<CacheRequestResponse<K, V>> cacheRequestResponses) {
  96 + try {
  97 + Collection<RedisKeyBuilder> keys = new ArrayList<>();
  98 + for (CacheRequestResponse<K, V> requestResponse : cacheRequestResponses) {
  99 + RedisKeyBuilder redisKeyBuilder = requestResponse.getRedisKeyBuilderTransfer().transfer(requestResponse.getKeyObject());
  100 + keys.add(redisKeyBuilder);
  101 + }
  102 + List<String> cachedValues = searchRedis.searchValueOperations.multiGet(keys);
  103 + for (int i = 0; i < cacheRequestResponses.size(); i++) {
  104 + CacheRequestResponse<K, V> requestResponse = cacheRequestResponses.get(i);
  105 + String cachedValue = cachedValues.get(i);
  106 + if (!StringUtils.isBlank(cachedValue)) {
  107 + requestResponse.setValueObject(requestResponse.getToValueTransfer().transfer(cachedValue));
  108 + requestResponse.setNeedRecache(false);
  109 + }
  110 + }
  111 + return cacheRequestResponses;
  112 + }catch (Exception e){
  113 + logger.error(e.getMessage(),e);
  114 + return cacheRequestResponses;
  115 + }
  116 + }
  117 +
  118 + private <K, V> void batchAddToCache(List<CacheRequestResponse<K, V>> cacheRequestResponses,long timeOutInSecond){
  119 + try {
  120 + Map<RedisKeyBuilder, String> toCacheMap = new HashMap<>();
  121 + for (CacheRequestResponse<K, V> requestResponse : cacheRequestResponses) {
  122 + if(requestResponse.isNeedRecache() && requestResponse.getValueObject()!=null){
  123 + RedisKeyBuilder redisKeyBuilder = requestResponse.getRedisKeyBuilderTransfer().transfer(requestResponse.getKeyObject());
  124 + V value = requestResponse.getValueObject();
  125 + String cacheValue = requestResponse.getFromValueTransfer().transfer(value);
  126 + toCacheMap.put(redisKeyBuilder,cacheValue);
  127 + }
  128 + }
  129 + if(!toCacheMap.isEmpty()){
  130 + searchRedis.searchRedisTemplate.mset(toCacheMap,timeOutInSecond);
  131 + }
  132 + }catch (Exception e){
  133 + logger.error(e.getMessage(),e);
  134 + }
  135 + }
  136 +
  137 +
  138 + private Transfer<Integer, RedisKeyBuilder> redisKeyBuilderTransfer() {
  139 + return new Transfer<Integer, RedisKeyBuilder>() {
  140 + @Override
  141 + public RedisKeyBuilder transfer(Integer productSkn) {
  142 + return RedisKeyBuilder.newInstance().appendFixed("YOHOSEARCH:").appendFixed("SKN:").appendVar(productSkn);
  143 + }
  144 + };
  145 + }
  146 +
  147 + private Transfer<String, Map<String, Object>> toValueTransfer() {
  148 + return new Transfer<String, Map<String, Object>>() {
  149 + @Override
  150 + public Map<String, Object> transfer(String value) {
  151 + Map<String, Object> product = new HashMap<>();
  152 + product.putAll(JSONObject.parseObject(value));
  153 + return product;
  154 + }
  155 + };
  156 + }
  157 +
  158 + private Transfer<Map<String, Object>, String> fromValueTransfer() {
  159 + return new Transfer<Map<String, Object>, String>() {
  160 + @Override
  161 + public String transfer( Map<String, Object> product) {
  162 + return JSON.toJSONString(product);
  163 + }
  164 + };
  165 + }
  166 +
  167 +
  168 +
  169 + private Map<Integer,Map<String, Object>> batchQuery(List<Integer> productSkns) {
  170 + SearchParam searchParam = new SearchParam();
  171 + searchParam.setOffset(0);
  172 + searchParam.setSize(productSkns.size());
  173 + searchParam.setFiter(QueryBuilders.termsQuery(ProductIndexEsField.productSkn, productSkns));
  174 + searchParam.setIncludeFields(productIndexBaseService.getProductIndexIncludeFields());
  175 + SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
  176 + List<Map<String, Object>> productList = productIndexBaseService.getProductListWithPricePlan(searchResult.getResultList());
  177 + Map<Integer,Map<String, Object>> results = new HashMap<>();
  178 + for (Map<String, Object> product: productList) {
  179 + results.put(MapUtils.getIntValue(product,"product_skn",0),product);
  180 + }
  181 + return results;
  182 + }
  183 +
  184 +}
1 package com.yoho.search.recall.scene.component; 1 package com.yoho.search.recall.scene.component;
2 2
3 -import com.yoho.search.recall.scene.models.*;  
4 -import com.yoho.search.recall.scene.persional.PersionalFactor; 3 +import com.yoho.search.recall.scene.models.RecallRequest;
  4 +import com.yoho.search.recall.scene.models.RecallRequestResponse;
  5 +import com.yoho.search.recall.scene.models.RecallResponse;
  6 +import com.yoho.search.recall.scene.models.RecallResponseBatch;
5 import com.yoho.search.recall.scene.strategy.StrategyNameEnum; 7 import com.yoho.search.recall.scene.strategy.StrategyNameEnum;
6 import org.springframework.stereotype.Component; 8 import org.springframework.stereotype.Component;
7 9
@@ -8,7 +8,6 @@ import com.yoho.search.recall.scene.persional.PersionalFactor; @@ -8,7 +8,6 @@ import com.yoho.search.recall.scene.persional.PersionalFactor;
8 import com.yoho.search.recall.scene.strategy.StrategyNameEnum; 8 import com.yoho.search.recall.scene.strategy.StrategyNameEnum;
9 import com.yoho.search.service.base.ProductListSortKey; 9 import com.yoho.search.service.base.ProductListSortKey;
10 import com.yoho.search.service.base.ProductListSortService; 10 import com.yoho.search.service.base.ProductListSortService;
11 -import com.yoho.search.service.helper.SearchCommonHelper;  
12 import org.springframework.beans.factory.annotation.Autowired; 11 import org.springframework.beans.factory.annotation.Autowired;
13 import org.springframework.stereotype.Component; 12 import org.springframework.stereotype.Component;
14 13
@@ -21,8 +20,6 @@ import java.util.List; @@ -21,8 +20,6 @@ import java.util.List;
21 public class RecallResultBuilder { 20 public class RecallResultBuilder {
22 21
23 @Autowired 22 @Autowired
24 - private SearchCommonHelper searchCommonHelper;  
25 - @Autowired  
26 private ProductListSortService productListSortService; 23 private ProductListSortService productListSortService;
27 24
28 public RecallResult builderRecallResult(RecallResponseBatch recallResponseBatch, RecallParams param, PersionalFactor persionalFactor){ 25 public RecallResult builderRecallResult(RecallResponseBatch recallResponseBatch, RecallParams param, PersionalFactor persionalFactor){
@@ -9,8 +9,8 @@ import org.elasticsearch.search.sort.SortOrder; @@ -9,8 +9,8 @@ import org.elasticsearch.search.sort.SortOrder;
9 9
10 public class SortBuilderHelper { 10 public class SortBuilderHelper {
11 11
12 - public static SortBuilder<?> getIdAscSort(){  
13 - return SortBuilders.fieldSort(ProductIndexEsField.id).order(SortOrder.ASC); 12 + public static SortBuilder<?> getIdDescSort(){
  13 + return SortBuilders.fieldSort(ProductIndexEsField.id).order(SortOrder.DESC);
14 } 14 }
15 15
16 public static SortBuilder<?> getRandomSort(){ 16 public static SortBuilder<?> getRandomSort(){
@@ -29,4 +29,8 @@ public class SortBuilderHelper { @@ -29,4 +29,8 @@ public class SortBuilderHelper {
29 return SortBuilders.fieldSort(ProductIndexEsField.lastReducePriceTime).order(SortOrder.DESC); 29 return SortBuilders.fieldSort(ProductIndexEsField.lastReducePriceTime).order(SortOrder.DESC);
30 } 30 }
31 31
  32 + public static SortBuilder<?> getSevendayMoneyDescSort(){
  33 + return SortBuilders.fieldSort(ProductIndexEsField.sevendayMoney).order(SortOrder.DESC);
  34 + }
  35 +
32 } 36 }
  1 +package com.yoho.search.recall.scene.models;
  2 +
  3 +import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
  4 +import com.yoho.search.base.utils.Transfer;
  5 +
  6 +public class CacheRequestResponse<K,V> {
  7 + private K keyObject;
  8 + private Transfer<K,RedisKeyBuilder> redisKeyBuilderTransfer;
  9 + private V valueObject;
  10 + private Transfer<String,V> toValueTransfer;
  11 + private Transfer<V,String> fromValueTransfer;
  12 + private boolean needRecache;
  13 +
  14 + public CacheRequestResponse(K keyObject,Transfer<K,RedisKeyBuilder> redisKeyBuilderTransfer,Transfer<String,V> toValueTransfer,Transfer<V,String> fromValueTransfer){
  15 + this.keyObject = keyObject;
  16 + this.redisKeyBuilderTransfer = redisKeyBuilderTransfer;
  17 + this.toValueTransfer = toValueTransfer;
  18 + this.fromValueTransfer = fromValueTransfer;
  19 + }
  20 +
  21 + public K getKeyObject() {
  22 + return keyObject;
  23 + }
  24 +
  25 + public V getValueObject() {
  26 + return valueObject;
  27 + }
  28 +
  29 + public Transfer<K, RedisKeyBuilder> getRedisKeyBuilderTransfer() {
  30 + return redisKeyBuilderTransfer;
  31 + }
  32 +
  33 + public Transfer<String, V> getToValueTransfer() {
  34 + return toValueTransfer;
  35 + }
  36 +
  37 + public Transfer<V, String> getFromValueTransfer() {
  38 + return fromValueTransfer;
  39 + }
  40 +
  41 + public void setValueObject(V valueObject) {
  42 + this.valueObject = valueObject;
  43 + }
  44 +
  45 + public boolean isNeedRecache() {
  46 + return needRecache;
  47 + }
  48 +
  49 + public void setNeedRecache(boolean needRecache) {
  50 + this.needRecache = needRecache;
  51 + }
  52 +}
@@ -7,8 +7,8 @@ public class RecallResult { @@ -7,8 +7,8 @@ public class RecallResult {
7 private final long recallTotal; 7 private final long recallTotal;
8 private final long recallPageTotal; 8 private final long recallPageTotal;
9 private final int realPage; 9 private final int realPage;
10 - private final List<Integer> sknList;  
11 - private final List<Integer> notProductSkn; 10 + private final List<Integer> sknList;//查询的页码在召回的范围内,则返回这些skn
  11 + private final List<Integer> notProductSkn;//查询的页码不在召回的范围内,则返回全部召回的skn,外层过滤这些skn
12 12
13 public RecallResult(long total,long recallTotal,long recallPageTotal, int realPage,List<Integer> sknList,List<Integer> notProductSkn) { 13 public RecallResult(long total,long recallTotal,long recallPageTotal, int realPage,List<Integer> sknList,List<Integer> notProductSkn) {
14 this.total = total; 14 this.total = total;
@@ -13,7 +13,7 @@ public interface IStrategy { @@ -13,7 +13,7 @@ public interface IStrategy {
13 13
14 SortBuilder<?> sortBuilder();// 排序策略 14 SortBuilder<?> sortBuilder();// 排序策略
15 15
16 - String strategyCacheKey(); 16 + String strategyCacheKey();// 可用于缓存的key
17 17
18 int cacheTimeInSecond();//缓存时间 18 int cacheTimeInSecond();//缓存时间
19 19
@@ -38,7 +38,7 @@ public class CommonFirstSknStrategy implements IStrategy { @@ -38,7 +38,7 @@ public class CommonFirstSknStrategy implements IStrategy {
38 38
39 @Override 39 @Override
40 public SortBuilder<?> sortBuilder() { 40 public SortBuilder<?> sortBuilder() {
41 - return SortBuilderHelper.getIdAscSort(); 41 + return SortBuilderHelper.getIdDescSort();
42 } 42 }
43 43
44 @Override 44 @Override
1 package com.yoho.search.service.base; 1 package com.yoho.search.service.base;
2 2
  3 +import com.yoho.search.base.utils.CollectionUtils;
  4 +import com.yoho.search.base.utils.Transfer;
3 import org.apache.commons.collections.MapUtils; 5 import org.apache.commons.collections.MapUtils;
  6 +import org.springframework.beans.BeanUtils;
4 import org.springframework.stereotype.Service; 7 import org.springframework.stereotype.Service;
5 8
6 import java.util.*; 9 import java.util.*;
@@ -53,4 +56,26 @@ public class ProductListSortService { @@ -53,4 +56,26 @@ public class ProductListSortService {
53 return tempResults; 56 return tempResults;
54 } 57 }
55 58
  59 + public <K> List<Map<String,Object>> sortProductListByProductSkn(List<Map<String,Object>> productList, List<K> productSkns, int size,Transfer<Map<String,Object>,K> transfer){
  60 + List<Map<String,Object>> results = new ArrayList<>();
  61 + if(productList==null || productList.isEmpty()){
  62 + return results;
  63 + }
  64 + if(productSkns==null || productSkns.isEmpty() || size<=0){
  65 + return results;
  66 + }
  67 + Map<K, Map<String,Object>> productMap = CollectionUtils.toMap(productList, transfer);
  68 + for (K productSkn : productSkns){
  69 + if(productMap.containsKey(productSkn)){
  70 + Map<String,Object> product = productMap.get(productSkn);
  71 + results.add(new HashMap<>(product));//注意循环引用
  72 + }
  73 + if(results.size()>=size){
  74 + break;
  75 + }
  76 + }
  77 + return results;
  78 + }
  79 +
  80 +
56 } 81 }