Authored by hugufei

使用泛型重构ProductListSortService

1 package com.yoho.search.recall.common; 1 package com.yoho.search.recall.common;
2 2
3 -import java.util.ArrayList;  
4 -import java.util.Arrays;  
5 -import java.util.Collections;  
6 -import java.util.Comparator;  
7 -import java.util.List;  
8 -import java.util.Map;  
9 -  
10 -import org.apache.commons.collections.MapUtils;  
11 -import org.apache.commons.lang.StringUtils;  
12 -import org.elasticsearch.index.query.BoolQueryBuilder;  
13 -import org.elasticsearch.index.query.QueryBuilders;  
14 -import org.elasticsearch.search.sort.SortBuilder;  
15 -import org.elasticsearch.search.sort.SortBuilders;  
16 -import org.elasticsearch.search.sort.SortOrder;  
17 -import org.slf4j.Logger;  
18 -import org.slf4j.LoggerFactory;  
19 -import org.springframework.beans.factory.annotation.Autowired;  
20 -import org.springframework.stereotype.Service;  
21 -  
22 import com.alibaba.fastjson.JSONObject; 3 import com.alibaba.fastjson.JSONObject;
23 import com.yoho.search.base.utils.CollectionUtils; 4 import com.yoho.search.base.utils.CollectionUtils;
24 import com.yoho.search.base.utils.ISearchConstants; 5 import com.yoho.search.base.utils.ISearchConstants;
@@ -29,6 +10,11 @@ import com.yoho.search.core.es.model.SearchParam; @@ -29,6 +10,11 @@ import com.yoho.search.core.es.model.SearchParam;
29 import com.yoho.search.core.es.model.SearchResult; 10 import com.yoho.search.core.es.model.SearchResult;
30 import com.yoho.search.core.personalized.PersonalizedSearch; 11 import com.yoho.search.core.personalized.PersonalizedSearch;
31 import com.yoho.search.models.SearchApiResult; 12 import com.yoho.search.models.SearchApiResult;
  13 +import com.yoho.search.recall.common.cacheable.CacheAbleServiceHelper;
  14 +import com.yoho.search.recall.common.cacheable.CommonPageRecallService;
  15 +import com.yoho.search.recall.common.model.CommonRecallParam;
  16 +import com.yoho.search.recall.common.model.CommonRecallResult;
  17 +import com.yoho.search.recall.common.model.CommonRecallSkn;
32 import com.yoho.search.service.base.ProductListSortKey; 18 import com.yoho.search.service.base.ProductListSortKey;
33 import com.yoho.search.service.base.ProductListSortService; 19 import com.yoho.search.service.base.ProductListSortService;
34 import com.yoho.search.service.base.SearchCommonService; 20 import com.yoho.search.service.base.SearchCommonService;
@@ -36,12 +22,20 @@ import com.yoho.search.service.base.SearchRequestParams; @@ -36,12 +22,20 @@ import com.yoho.search.service.base.SearchRequestParams;
36 import com.yoho.search.service.base.index.ProductIndexBaseService; 22 import com.yoho.search.service.base.index.ProductIndexBaseService;
37 import com.yoho.search.service.helper.SearchParamHelper; 23 import com.yoho.search.service.helper.SearchParamHelper;
38 import com.yoho.search.service.helper.SearchSortHelper; 24 import com.yoho.search.service.helper.SearchSortHelper;
39 -import com.yoho.search.recall.common.cacheable.CacheAbleServiceHelper;  
40 -import com.yoho.search.recall.common.cacheable.CommonPageRecallService;  
41 -import com.yoho.search.recall.common.model.CommonRecallParam;  
42 -import com.yoho.search.recall.common.model.CommonRecallResult;  
43 -import com.yoho.search.recall.common.model.CommonRecallSkn;  
44 import com.yoho.search.service.scorer.personal.PersonalVectorFeatureSearch; 25 import com.yoho.search.service.scorer.personal.PersonalVectorFeatureSearch;
  26 +import org.apache.commons.collections.MapUtils;
  27 +import org.apache.commons.lang.StringUtils;
  28 +import org.elasticsearch.index.query.BoolQueryBuilder;
  29 +import org.elasticsearch.index.query.QueryBuilders;
  30 +import org.elasticsearch.search.sort.SortBuilder;
  31 +import org.elasticsearch.search.sort.SortBuilders;
  32 +import org.elasticsearch.search.sort.SortOrder;
  33 +import org.slf4j.Logger;
  34 +import org.slf4j.LoggerFactory;
  35 +import org.springframework.beans.factory.annotation.Autowired;
  36 +import org.springframework.stereotype.Service;
  37 +
  38 +import java.util.*;
45 39
46 @Service 40 @Service
47 public class ProductListServiceHelper { 41 public class ProductListServiceHelper {
@@ -129,18 +123,16 @@ public class ProductListServiceHelper { @@ -129,18 +123,16 @@ public class ProductListServiceHelper {
129 results.add(productInfoMap.getJSONObject(String.valueOf(commonRecallSkn.getProductSkn()))); 123 results.add(productInfoMap.getJSONObject(String.valueOf(commonRecallSkn.getProductSkn())));
130 } 124 }
131 // 4、品牌打散 125 // 4、品牌打散
132 - ProductListSortKey productSortKey = new ProductListSortKey() { 126 + results = productListSortService.sortProductList(results, new ProductListSortKey<JSONObject>() {
133 @Override 127 @Override
134 - public String getSortKey(Map<?, ?> product) { 128 + public String getSortKey(JSONObject product) {
135 return MapUtils.getString(product, "brand_id", "0"); 129 return MapUtils.getString(product, "brand_id", "0");
136 } 130 }
137 -  
138 @Override 131 @Override
139 public int getMaxCount() { 132 public int getMaxCount() {
140 return 2; 133 return 2;
141 } 134 }
142 - };  
143 - results = productListSortService.sortProductJSONObjectList(results, productSortKey); 135 + });
144 return results; 136 return results;
145 } 137 }
146 138
@@ -262,7 +254,7 @@ public class ProductListServiceHelper { @@ -262,7 +254,7 @@ public class ProductListServiceHelper {
262 dataMap.put("page_size", searchParam.getSize()); 254 dataMap.put("page_size", searchParam.getSize());
263 dataMap.put("page_total", searchResult.getTotalPage()); 255 dataMap.put("page_total", searchResult.getTotalPage());
264 List<Map<String, Object>> product_list = productIndexBaseService.getProductListWithPricePlan(searchResult.getResultList()); 256 List<Map<String, Object>> product_list = productIndexBaseService.getProductListWithPricePlan(searchResult.getResultList());
265 - dataMap.put("product_list", productListSortService.sortProductList(product_list, paramMap));// 处理一下商品的顺序; 257 + dataMap.put("product_list", productListSortService.sortProductList(product_list));// 处理一下商品的顺序;
266 return new SearchApiResult().setData(dataMap); 258 return new SearchApiResult().setData(dataMap);
267 } catch (Exception e) { 259 } catch (Exception e) {
268 logger.error(e.getMessage(), e); 260 logger.error(e.getMessage(), e);
@@ -111,7 +111,7 @@ public class CacheAbleServiceHelper { @@ -111,7 +111,7 @@ public class CacheAbleServiceHelper {
111 SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam); 111 SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
112 // 5)构造返回结果 112 // 5)构造返回结果
113 List<Map<String, Object>> product_list = productIndexBaseService.getProductListWithPricePlan(searchResult.getResultList()); 113 List<Map<String, Object>> product_list = productIndexBaseService.getProductListWithPricePlan(searchResult.getResultList());
114 - product_list = productListSortService.sortProductList(product_list, paramMap);// 处理一下商品的顺序; 114 + product_list = productListSortService.sortProductList(product_list);// 处理一下商品的顺序;
115 return new SearchApiResult().setData(product_list); 115 return new SearchApiResult().setData(product_list);
116 } catch (Exception e) { 116 } catch (Exception e) {
117 logger.error(e.getMessage(),e); 117 logger.error(e.getMessage(),e);
@@ -2,9 +2,9 @@ package com.yoho.search.service.base; @@ -2,9 +2,9 @@ package com.yoho.search.service.base;
2 2
3 import java.util.Map; 3 import java.util.Map;
4 4
5 -public interface ProductListSortKey { 5 +public interface ProductListSortKey<T> {
6 6
7 - public String getSortKey(Map<?, ?> product); 7 + public String getSortKey(T product);
8 8
9 public int getMaxCount(); 9 public int getMaxCount();
10 10
1 package com.yoho.search.service.base; 1 package com.yoho.search.service.base;
2 2
3 -import java.util.ArrayList;  
4 -import java.util.HashMap;  
5 -import java.util.Iterator;  
6 -import java.util.List;  
7 -import java.util.Map;  
8 -  
9 import org.apache.commons.collections.MapUtils; 3 import org.apache.commons.collections.MapUtils;
10 -import org.apache.commons.lang3.StringUtils;  
11 import org.springframework.stereotype.Service; 4 import org.springframework.stereotype.Service;
12 5
13 -import com.alibaba.fastjson.JSONObject; 6 +import java.util.*;
14 7
15 @Service 8 @Service
16 public class ProductListSortService { 9 public class ProductListSortService {
17 10
18 - private String getSortKey(Map<String, Object> product) {  
19 - int brandId = MapUtils.getInteger(product, "brand_id", 0);  
20 - int small_sort_id = MapUtils.getInteger(product, "small_sort_id", 0);  
21 - return brandId + "_" + small_sort_id;  
22 - }  
23 -  
24 - /**  
25 - * 第一页的商品按品类+品牌维度分组,每组取第一个放前面  
26 - *  
27 - * @param product_list  
28 - * @param paramMap  
29 - * @return  
30 - */  
31 - public List<Map<String, Object>> sortProductList(List<Map<String, Object>> product_list, Map<String, String> paramMap) {  
32 - if (product_list == null || product_list.isEmpty()) {  
33 - return product_list;  
34 - }  
35 - if (StringUtils.isNotBlank(paramMap.get("order"))) {  
36 - return product_list;  
37 - }  
38 - int page = StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page"));  
39 - if (page != 1) {  
40 - return product_list;  
41 - }  
42 - List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();  
43 - Map<String, Integer> keyCount = new HashMap<String, Integer>();  
44 - Iterator<Map<String, Object>> iterator = product_list.iterator();  
45 - while (iterator.hasNext()) {  
46 - Map<String, Object> product = iterator.next();  
47 - String key = this.getSortKey(product);  
48 - int count = keyCount.getOrDefault(key, 1);  
49 - if (count <= 2) {  
50 - results.add(product);  
51 - iterator.remove(); 11 + public List<Map<String, Object>> sortProductList(List<Map<String, Object>> product_list) {
  12 + ProductListSortKey<Map<String, Object>> productSortKey = new ProductListSortKey<Map<String, Object>>() {
  13 + @Override
  14 + public String getSortKey(Map<String, Object> product) {
  15 + int brandId = MapUtils.getInteger(product, "brand_id", 0);
  16 + int small_sort_id = MapUtils.getInteger(product, "small_sort_id", 0);
  17 + return brandId + "_" + small_sort_id;
52 } 18 }
53 - keyCount.put(key, count + 1);  
54 - }  
55 - if (!product_list.isEmpty()) {  
56 - results.addAll(product_list);  
57 - }  
58 - return results; 19 + @Override
  20 + public int getMaxCount() {
  21 + return 2;
  22 + }
  23 + };
  24 + return this.sortProductList(product_list,productSortKey);
59 } 25 }
60 26
61 - /**  
62 - * 品牌打散  
63 - *  
64 - * @param product_list  
65 - * @param paramMap  
66 - * @return  
67 - */  
68 - public List<Map<String, Object>> sortProductMapList(List<Map<String, Object>> product_list, ProductListSortKey productSortKey) { 27 +
  28 + public <T> List<T> sortProductList(List<T> product_list, ProductListSortKey<T> productSortKey) {
69 if (product_list == null || product_list.isEmpty()) { 29 if (product_list == null || product_list.isEmpty()) {
70 return product_list; 30 return product_list;
71 } 31 }
72 - List<Map<String, Object>> results = new ArrayList<Map<String, Object>>(); 32 + List<T> results = new ArrayList<T>();
73 while (!product_list.isEmpty()) { 33 while (!product_list.isEmpty()) {
74 results.addAll(getTempProductMapList(product_list, productSortKey)); 34 results.addAll(getTempProductMapList(product_list, productSortKey));
75 } 35 }
76 return results; 36 return results;
77 } 37 }
78 38
79 - private List<Map<String, Object>> getTempProductMapList(List<Map<String, Object>> product_list, ProductListSortKey productSortKey) {  
80 - List<Map<String, Object>> tempResults = new ArrayList<Map<String, Object>>(); 39 + private <T> List<T> getTempProductMapList(List<T> product_list, ProductListSortKey<T> productSortKey) {
  40 + List<T> tempResults = new ArrayList<T>();
81 Map<String, Integer> tempKeyCount = new HashMap<String, Integer>(); 41 Map<String, Integer> tempKeyCount = new HashMap<String, Integer>();
82 - Iterator<Map<String, Object>> iterator = product_list.iterator(); 42 + Iterator<T> iterator = product_list.iterator();
83 while (iterator.hasNext()) { 43 while (iterator.hasNext()) {
84 - Map<String, Object> product = iterator.next(); 44 + T product = iterator.next();
85 String sortKey = productSortKey.getSortKey(product); 45 String sortKey = productSortKey.getSortKey(product);
86 int count = tempKeyCount.getOrDefault(sortKey, 1); 46 int count = tempKeyCount.getOrDefault(sortKey, 1);
87 if (count <= productSortKey.getMaxCount()) { 47 if (count <= productSortKey.getMaxCount()) {
@@ -92,40 +52,5 @@ public class ProductListSortService { @@ -92,40 +52,5 @@ public class ProductListSortService {
92 } 52 }
93 return tempResults; 53 return tempResults;
94 } 54 }
95 -  
96 - /**  
97 - * 品牌打散  
98 - *  
99 - * @param product_list  
100 - * @param paramMap  
101 - * @return  
102 - */  
103 - public List<JSONObject> sortProductJSONObjectList(List<JSONObject> product_list, ProductListSortKey productListSortKey) {  
104 - if (product_list == null || product_list.isEmpty()) {  
105 - return product_list;  
106 - }  
107 - List<JSONObject> results = new ArrayList<JSONObject>();  
108 - while (!product_list.isEmpty()) {  
109 - results.addAll(getTempProductJSONObjectList(product_list, productListSortKey));  
110 - }  
111 - return results;  
112 - }  
113 -  
114 - private List<JSONObject> getTempProductJSONObjectList(List<JSONObject> product_list, ProductListSortKey productListSortKey) {  
115 - List<JSONObject> tempResults = new ArrayList<JSONObject>();  
116 - Map<String, Integer> tempKeyCount = new HashMap<String, Integer>();  
117 - Iterator<JSONObject> iterator = product_list.iterator();  
118 - while (iterator.hasNext()) {  
119 - JSONObject product = iterator.next();  
120 - String sortKey = productListSortKey.getSortKey(product);  
121 - int count = tempKeyCount.getOrDefault(sortKey, 1);  
122 - if (count <= productListSortKey.getMaxCount()) {  
123 - tempResults.add(product);  
124 - iterator.remove();  
125 - }  
126 - tempKeyCount.put(sortKey, count + 1);  
127 - }  
128 - return tempResults;  
129 - }  
130 55
131 } 56 }
@@ -133,7 +133,7 @@ public class WebProductListService extends AbstractCacheAbleService { @@ -133,7 +133,7 @@ public class WebProductListService extends AbstractCacheAbleService {
133 dataMap.put("page_total", searchResult.getTotalPage()); 133 dataMap.put("page_total", searchResult.getTotalPage());
134 134
135 List<Map<String, Object>> product_list = webProductIndexBaseService.getProductListWithPricePlan(searchResult.getResultList()); 135 List<Map<String, Object>> product_list = webProductIndexBaseService.getProductListWithPricePlan(searchResult.getResultList());
136 - dataMap.put("product_list", productListSortService.sortProductList(product_list, paramMap));// 处理一下商品的顺序 136 + dataMap.put("product_list", productListSortService.sortProductList(product_list));// 处理一下商品的顺序
137 137
138 // 9)将结果存进缓存 138 // 9)将结果存进缓存
139 searchCacheService.addJSONObjectToCache(this.searchCache, indexName, searchParam, dataMap); 139 searchCacheService.addJSONObjectToCache(this.searchCache, indexName, searchParam, dataMap);
@@ -75,7 +75,7 @@ public class FuzzySceneProductListService extends AbstractCacheAbleService { @@ -75,7 +75,7 @@ public class FuzzySceneProductListService extends AbstractCacheAbleService {
75 dataMap.put("page_size", searchParam.getSize()); 75 dataMap.put("page_size", searchParam.getSize());
76 dataMap.put("page_total", searchResult.getTotalPage()); 76 dataMap.put("page_total", searchResult.getTotalPage());
77 List<Map<String, Object>> product_list = productIndexBaseService.getProductListWithPricePlan(searchResult.getResultList()); 77 List<Map<String, Object>> product_list = productIndexBaseService.getProductListWithPricePlan(searchResult.getResultList());
78 - dataMap.put("product_list", productListSortService.sortProductList(product_list, paramMap));// 处理一下商品的顺序; 78 + dataMap.put("product_list", productListSortService.sortProductList(product_list));// 处理一下商品的顺序;
79 79
80 // 5)将结果存进缓存 80 // 5)将结果存进缓存
81 searchCacheService.addJSONObjectToCache(this.searchCache, indexName, searchParam, dataMap); 81 searchCacheService.addJSONObjectToCache(this.searchCache, indexName, searchParam, dataMap);
@@ -134,7 +134,7 @@ public class ProductListServiceImpl implements IProductListService { @@ -134,7 +134,7 @@ public class ProductListServiceImpl implements IProductListService {
134 dataMap.put("page_size", searchParam.getSize()); 134 dataMap.put("page_size", searchParam.getSize());
135 dataMap.put("page_total", searchResult.getTotalPage()); 135 dataMap.put("page_total", searchResult.getTotalPage());
136 List<Map<String, Object>> product_list = productIndexBaseService.getProductListWithPricePlan(searchResult.getResultList()); 136 List<Map<String, Object>> product_list = productIndexBaseService.getProductListWithPricePlan(searchResult.getResultList());
137 - dataMap.put("product_list", productListSortService.sortProductList(product_list, paramMap));// 处理一下商品的顺序 137 + dataMap.put("product_list", productListSortService.sortProductList(product_list));// 处理一下商品的顺序
138 return new SearchApiResult().setData(dataMap); 138 return new SearchApiResult().setData(dataMap);
139 } catch (Exception e) { 139 } catch (Exception e) {
140 logger.error("[func=productListBySkn][params=" + paramMap + "]", e); 140 logger.error("[func=productListBySkn][params=" + paramMap + "]", e);