Authored by hugufei

找相似优化

1 -package com.yoho.search.restapi;  
2 -  
3 -import com.yoho.search.common.utils.HttpServletRequestUtils;  
4 -import com.yoho.search.models.SearchApiResult;  
5 -import com.yoho.search.service.service.IProductListWithSupplyService;  
6 -  
7 -import org.springframework.beans.factory.annotation.Autowired;  
8 -import org.springframework.stereotype.Controller;  
9 -import org.springframework.web.bind.annotation.RequestMapping;  
10 -import org.springframework.web.bind.annotation.RequestMethod;  
11 -import org.springframework.web.bind.annotation.ResponseBody;  
12 -  
13 -import javax.servlet.http.HttpServletRequest;  
14 -  
15 -import java.util.Map;  
16 -  
17 -/**  
18 - * Created by wangnan on 2017/5/26.  
19 - */  
20 -@Controller  
21 -public class ProductListWithSupplyController {  
22 -  
23 - @Autowired  
24 - private IProductListWithSupplyService productListWithSupplyService;  
25 -  
26 - /**  
27 - * 获取商品列表,支持如果传入skn无效,补充相似skn[使用本地缓存]  
28 - */  
29 - @RequestMapping(method = RequestMethod.GET, value = "/productindex/productListWithSupply")  
30 - @ResponseBody  
31 - public SearchApiResult productList(HttpServletRequest request) {  
32 - Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);  
33 - return productListWithSupplyService.productListWithSupply(paramMap);  
34 - }  
35 -}  
1 -package com.yoho.search.restapi;  
2 -  
3 -import com.yoho.search.common.utils.HttpServletRequestUtils;  
4 -import com.yoho.search.models.SearchApiResult;  
5 -import com.yoho.search.service.service.ISimilarProductService;  
6 -  
7 -import org.springframework.beans.factory.annotation.Autowired;  
8 -import org.springframework.stereotype.Controller;  
9 -import org.springframework.web.bind.annotation.RequestMapping;  
10 -import org.springframework.web.bind.annotation.RequestMethod;  
11 -import org.springframework.web.bind.annotation.ResponseBody;  
12 -  
13 -import javax.servlet.http.HttpServletRequest;  
14 -  
15 -import java.util.Map;  
16 -  
17 -/**  
18 - * Created by wangnan on 2017/4/24.  
19 - */  
20 -@Controller  
21 -public class SimilarProductListController {  
22 -  
23 - @Autowired  
24 - private ISimilarProductService similarProductService;  
25 -  
26 - /**  
27 - * 根据一堆skn找相似的skn  
28 - *  
29 - * @param request  
30 - * @return  
31 - */  
32 - @RequestMapping(method = RequestMethod.GET, value = "/productindex/similarProductList")  
33 - @ResponseBody  
34 - public SearchApiResult similarProductList(HttpServletRequest request) {  
35 - Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);  
36 - return similarProductService.similarProductList(paramMap);  
37 - }  
38 -}  
@@ -15,6 +15,8 @@ import com.yoho.search.models.SearchApiResult; @@ -15,6 +15,8 @@ import com.yoho.search.models.SearchApiResult;
15 import com.yoho.search.service.scene.searchlike.SearchLikeInShopService; 15 import com.yoho.search.service.scene.searchlike.SearchLikeInShopService;
16 import com.yoho.search.service.scene.searchlike.SearchLikeNotInShopService; 16 import com.yoho.search.service.scene.searchlike.SearchLikeNotInShopService;
17 import com.yoho.search.service.scene.searchlike.SearchLikeSceneService; 17 import com.yoho.search.service.scene.searchlike.SearchLikeSceneService;
  18 +import com.yoho.search.service.service.IProductListWithSupplyService;
  19 +import com.yoho.search.service.service.ISimilarProductService;
18 20
19 /** 21 /**
20 * 找相似相关功能 22 * 找相似相关功能
@@ -31,6 +33,10 @@ public class SearchLikeSecneController { @@ -31,6 +33,10 @@ public class SearchLikeSecneController {
31 private SearchLikeInShopService searchLikeInShopService; 33 private SearchLikeInShopService searchLikeInShopService;
32 @Autowired 34 @Autowired
33 private SearchLikeNotInShopService searchLikeNotInShopService; 35 private SearchLikeNotInShopService searchLikeNotInShopService;
  36 + @Autowired
  37 + private IProductListWithSupplyService productListWithSupplyService;
  38 + @Autowired
  39 + private ISimilarProductService similarProductService;
34 40
35 @RequestMapping(method = RequestMethod.GET, value = "/searchLike") 41 @RequestMapping(method = RequestMethod.GET, value = "/searchLike")
36 @ResponseBody 42 @ResponseBody
@@ -53,4 +59,27 @@ public class SearchLikeSecneController { @@ -53,4 +59,27 @@ public class SearchLikeSecneController {
53 return searchLikeNotInShopService.searchLikeNotInShop(paramMap); 59 return searchLikeNotInShopService.searchLikeNotInShop(paramMap);
54 } 60 }
55 61
  62 + /**
  63 + * 获取商品列表,支持如果传入skn无效,补充相似skn
  64 + */
  65 + @RequestMapping(method = RequestMethod.GET, value = "/productindex/productListWithSupply")
  66 + @ResponseBody
  67 + public SearchApiResult productList(HttpServletRequest request) {
  68 + Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
  69 + return productListWithSupplyService.productListWithSupply(paramMap);
  70 + }
  71 +
  72 + /**
  73 + * 根据一堆skn找相似的skn
  74 + *
  75 + * @param request
  76 + * @return
  77 + */
  78 + @RequestMapping(method = RequestMethod.GET, value = "/productindex/similarProductList")
  79 + @ResponseBody
  80 + public SearchApiResult similarProductList(HttpServletRequest request) {
  81 + Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
  82 + return similarProductService.similarProductList(paramMap);
  83 + }
  84 +
56 } 85 }
@@ -8,10 +8,7 @@ import java.util.List; @@ -8,10 +8,7 @@ import java.util.List;
8 import java.util.Map; 8 import java.util.Map;
9 import java.util.Set; 9 import java.util.Set;
10 10
11 -import javax.annotation.PostConstruct;  
12 -  
13 import org.apache.commons.collections.CollectionUtils; 11 import org.apache.commons.collections.CollectionUtils;
14 -import org.apache.commons.lang.StringUtils;  
15 import org.elasticsearch.index.query.BoolQueryBuilder; 12 import org.elasticsearch.index.query.BoolQueryBuilder;
16 import org.elasticsearch.index.query.QueryBuilders; 13 import org.elasticsearch.index.query.QueryBuilders;
17 import org.slf4j.Logger; 14 import org.slf4j.Logger;
@@ -22,19 +19,15 @@ import org.springframework.stereotype.Service; @@ -22,19 +19,15 @@ import org.springframework.stereotype.Service;
22 import com.alibaba.fastjson.JSONObject; 19 import com.alibaba.fastjson.JSONObject;
23 import com.yoho.search.base.utils.ISearchConstants; 20 import com.yoho.search.base.utils.ISearchConstants;
24 import com.yoho.search.base.utils.ProductIndexEsField; 21 import com.yoho.search.base.utils.ProductIndexEsField;
25 -import com.yoho.search.common.cache.SearchCacheFactory;  
26 -import com.yoho.search.common.cache.SearchCacheMatchLogger;  
27 -import com.yoho.search.common.cache.model.SearchCache; 22 +import com.yoho.search.common.cache.aop.SearchCacheAble;
28 import com.yoho.search.common.utils.SearchApiResultUtils; 23 import com.yoho.search.common.utils.SearchApiResultUtils;
29 import com.yoho.search.core.es.model.SearchParam; 24 import com.yoho.search.core.es.model.SearchParam;
30 import com.yoho.search.core.es.model.SearchResult; 25 import com.yoho.search.core.es.model.SearchResult;
31 import com.yoho.search.models.SearchApiResult; 26 import com.yoho.search.models.SearchApiResult;
32 -import com.yoho.search.service.base.SearchCacheService;  
33 import com.yoho.search.service.base.SearchCommonService; 27 import com.yoho.search.service.base.SearchCommonService;
34 import com.yoho.search.service.base.SearchRequestParams; 28 import com.yoho.search.service.base.SearchRequestParams;
35 import com.yoho.search.service.base.index.ProductIndexBaseService; 29 import com.yoho.search.service.base.index.ProductIndexBaseService;
36 import com.yoho.search.service.helper.SearchCommonHelper; 30 import com.yoho.search.service.helper.SearchCommonHelper;
37 -import com.yoho.search.service.helper.SearchKeyWordHelper;  
38 import com.yoho.search.service.helper.SearchParamHelper; 31 import com.yoho.search.service.helper.SearchParamHelper;
39 import com.yoho.search.service.helper.SearchSortHelper; 32 import com.yoho.search.service.helper.SearchSortHelper;
40 import com.yoho.search.service.service.IProductListWithSupplyService; 33 import com.yoho.search.service.service.IProductListWithSupplyService;
@@ -55,70 +48,35 @@ public class ProductListWithSupplyServiceImpl implements IProductListWithSupplyS @@ -55,70 +48,35 @@ public class ProductListWithSupplyServiceImpl implements IProductListWithSupplyS
55 @Autowired 48 @Autowired
56 private SearchCommonService searchCommonService; 49 private SearchCommonService searchCommonService;
57 @Autowired 50 @Autowired
58 - private SearchKeyWordHelper searchKeyWordService;  
59 - @Autowired  
60 private ProductIndexBaseService productIndexBaseService; 51 private ProductIndexBaseService productIndexBaseService;
61 @Autowired 52 @Autowired
62 private ISimilarProductService similarProductService; 53 private ISimilarProductService similarProductService;
63 @Autowired 54 @Autowired
64 - private SearchCacheService searchCacheService;  
65 - @Autowired  
66 - private SearchCacheFactory searchCacheFactory;  
67 - @Autowired  
68 private SearchParamHelper searchParamHelper; 55 private SearchParamHelper searchParamHelper;
69 56
70 - private SearchCache productListSearchCache;  
71 -  
72 - @PostConstruct  
73 - void init() {  
74 - productListSearchCache = searchCacheFactory.getProductListSearchCache();  
75 - }  
76 -  
77 /** 57 /**
78 - * 不支持分页,最大传入skn数100,大于100只返回100条 58 + * 不支持分页,最多返回100个
79 */ 59 */
80 @Override 60 @Override
  61 + @SearchCacheAble(cacheInMinute = 30, cacheName = "PRODUCT_LIST_WITH_SUPPLY")
81 public SearchApiResult productListWithSupply(Map<String, String> paramMap) { 62 public SearchApiResult productListWithSupply(Map<String, String> paramMap) {
82 try { 63 try {
83 - SearchApiResult searchResult = innerProductList(paramMap);  
84 - return searchResult;  
85 - } catch (Exception e) {  
86 - logger.error("[func=productListWithSupply][params=" + paramMap + "]", e);  
87 - return SearchApiResultUtils.errorSearchApiResult("productListWithSupply", paramMap, e);  
88 - }  
89 - }  
90 -  
91 - private SearchApiResult innerProductList(Map<String, String> paramMap) throws Exception {  
92 - long begin = System.currentTimeMillis();  
93 - logger.info("[func=productListWithSupply][param={}][begin={}]", paramMap, begin);  
94 -  
95 - // 1)构造搜索参数  
96 - SearchParam searchParam = buildProductListSearchParam(paramMap);  
97 -  
98 - // 2)从缓存中获取数据  
99 - final String indexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;  
100 - JSONObject cacheObject = searchCacheService.getJSONObjectFromCache(productListSearchCache, indexName, searchParam);  
101 - if (cacheObject != null) {  
102 - SearchCacheMatchLogger.doSearchCacheMatchLog("/productindex/productListWithSupply.json", paramMap);  
103 - return new SearchApiResult().setData(cacheObject);  
104 - } 64 + // 1)构建基本查询参数
  65 + SearchParam searchParam = searchParamHelper.buildDefault(paramMap);
  66 + searchParam.setAggregationBuilders(null);
  67 + searchParam.setOffset(0);
  68 + searchParam.setSize(100);
105 69
106 - // 3)查询ES  
107 - SearchResult searchResult = searchCommonService.doSearch(indexName, searchParam); 70 + // 2)查询ES
  71 + SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
108 if (searchResult == null) { 72 if (searchResult == null) {
109 return new SearchApiResult().setCode(500).setMessage("execption"); 73 return new SearchApiResult().setCode(500).setMessage("execption");
110 } 74 }
111 75
112 - // 4)记录关键字对应的查询结果  
113 - String queryWord = paramMap.get("query");  
114 - if (!StringUtils.isBlank(queryWord) && !searchCommonHelper.isQuerySknOrSku(queryWord)) {  
115 - searchKeyWordService.recordKeyWordByResultCount(queryWord, searchResult.getTotal());  
116 - }  
117 -  
118 // 5)补充SKN逻辑:记录传入的skn列表,调用搜索,把返回的product列表中的skn记录到一个列表,找出无效的skn,用这些skn查找相似的,加入到之前的返回列表中返回 76 // 5)补充SKN逻辑:记录传入的skn列表,调用搜索,把返回的product列表中的skn记录到一个列表,找出无效的skn,用这些skn查找相似的,加入到之前的返回列表中返回
119 List<Map<String, Object>> productList = supplySkn(paramMap, searchResult, searchParam); 77 List<Map<String, Object>> productList = supplySkn(paramMap, searchResult, searchParam);
120 78
121 - // 6)构造返回结果 79 + // 7)构造返回结果
122 JSONObject dataMap = new JSONObject(); 80 JSONObject dataMap = new JSONObject();
123 dataMap.put("total", productList.size()); 81 dataMap.put("total", productList.size());
124 dataMap.put("page", 1); 82 dataMap.put("page", 1);
@@ -126,10 +84,11 @@ public class ProductListWithSupplyServiceImpl implements IProductListWithSupplyS @@ -126,10 +84,11 @@ public class ProductListWithSupplyServiceImpl implements IProductListWithSupplyS
126 dataMap.put("page_total", 1); 84 dataMap.put("page_total", 1);
127 dataMap.put("product_list", productList); 85 dataMap.put("product_list", productList);
128 86
129 - // 7)将结果存进缓存  
130 - searchCacheService.addJSONObjectToCache(productListSearchCache, indexName, searchParam, dataMap);  
131 - logger.info("[func=productListWithSupply][cost={}]", System.currentTimeMillis() - begin);  
132 return new SearchApiResult().setData(dataMap); 87 return new SearchApiResult().setData(dataMap);
  88 + } catch (Exception e) {
  89 + logger.error("[func=productListWithSupply][params=" + paramMap + "]", e);
  90 + return SearchApiResultUtils.errorSearchApiResult("productListWithSupply", paramMap, e);
  91 + }
133 } 92 }
134 93
135 /** 94 /**
@@ -216,24 +175,4 @@ public class ProductListWithSupplyServiceImpl implements IProductListWithSupplyS @@ -216,24 +175,4 @@ public class ProductListWithSupplyServiceImpl implements IProductListWithSupplyS
216 } 175 }
217 return trueInvalidSknList; 176 return trueInvalidSknList;
218 } 177 }
219 -  
220 - private SearchParam buildProductListSearchParam(Map<String, String> paramMap) throws Exception {  
221 - // 1)验证查询条数  
222 - int pageSize = StringUtils.isBlank(paramMap.get("viewNum")) ? 100 : Integer.parseInt(paramMap.get("viewNum"));  
223 - int page = 1;  
224 - if (page < 1 || pageSize < 0) {  
225 - throw new IllegalArgumentException("分页参数不合法");  
226 - }  
227 - if (pageSize > 100) {  
228 - pageSize = 100;  
229 - }  
230 - // 2)构建基本查询参数  
231 - SearchParam searchParam = searchParamHelper.buildDefault(paramMap);  
232 - searchParam.setAggregationBuilders(null);  
233 - searchParam.setOffset((page - 1) * pageSize);  
234 - searchParam.setSize(pageSize);  
235 - // 3)设置排序字段  
236 - searchParam.setSortBuilders(searchSortHelper.buildSortList(paramMap));  
237 - return searchParam;  
238 - }  
239 } 178 }