Showing
6 changed files
with
89 additions
and
82 deletions
1 | package com.yoho.search.service.common; | 1 | package com.yoho.search.service.common; |
2 | 2 | ||
3 | 3 | ||
4 | -import ch.qos.logback.classic.Level; | ||
5 | -import ch.qos.logback.classic.Logger; | ||
6 | -import org.slf4j.LoggerFactory; | ||
7 | - | ||
8 | import java.util.Map; | 4 | import java.util.Map; |
9 | import java.util.concurrent.ConcurrentHashMap; | 5 | import java.util.concurrent.ConcurrentHashMap; |
10 | import java.util.concurrent.Executors; | 6 | import java.util.concurrent.Executors; |
11 | import java.util.concurrent.ScheduledExecutorService; | 7 | import java.util.concurrent.ScheduledExecutorService; |
12 | import java.util.concurrent.TimeUnit; | 8 | import java.util.concurrent.TimeUnit; |
13 | 9 | ||
10 | +import org.slf4j.LoggerFactory; | ||
11 | + | ||
12 | +import ch.qos.logback.classic.Level; | ||
13 | +import ch.qos.logback.classic.Logger; | ||
14 | + | ||
14 | /** | 15 | /** |
15 | * Created by ginozhang on 2017/3/30. | 16 | * Created by ginozhang on 2017/3/30. |
16 | */ | 17 | */ |
@@ -18,7 +18,7 @@ import com.yoho.search.service.downgrade.DownGradeService; | @@ -18,7 +18,7 @@ import com.yoho.search.service.downgrade.DownGradeService; | ||
18 | import com.yoho.search.service.monitor.PerformanceMonitor; | 18 | import com.yoho.search.service.monitor.PerformanceMonitor; |
19 | import com.yoho.search.service.utils.HttpServletRequestUtils; | 19 | import com.yoho.search.service.utils.HttpServletRequestUtils; |
20 | 20 | ||
21 | -public class ControllerCostInterceptor implements HandlerInterceptor{ | 21 | +public class ControllerCostInterceptor implements HandlerInterceptor { |
22 | 22 | ||
23 | private static final Logger CONTROLLER_COST = LoggerFactory.getLogger("CONTROLLER_COST"); | 23 | private static final Logger CONTROLLER_COST = LoggerFactory.getLogger("CONTROLLER_COST"); |
24 | 24 | ||
@@ -56,23 +56,15 @@ public class ControllerCostInterceptor implements HandlerInterceptor{ | @@ -56,23 +56,15 @@ public class ControllerCostInterceptor implements HandlerInterceptor{ | ||
56 | // 3、增加相应耗时 | 56 | // 3、增加相应耗时 |
57 | monitor.addCost(cost); | 57 | monitor.addCost(cost); |
58 | 58 | ||
59 | - // // 4、上报耗时 | ||
60 | - // this.publisher | ||
61 | - // .publishEvent(new | ||
62 | - // ServiceAccessEvent(HttpRequestUtils.getGatewayServiceName(request), | ||
63 | - // cost, response.getStatus(), | ||
64 | - // this.getRequestUrl(request).toString())); | ||
65 | - | ||
66 | - // 5、如果响应时间小于100ms,则直接返回 | 59 | + // 4、如果响应时间小于100ms,则直接返回 |
67 | if (cost <= 500) { | 60 | if (cost <= 500) { |
68 | return; | 61 | return; |
69 | } | 62 | } |
70 | - // 6、耗时超过1000ms,则试图去降级 | 63 | + // 5、耗时超过1000ms,则试图去降级 |
71 | if (cost >= 500) { | 64 | if (cost >= 500) { |
72 | downGradeService.tryDowngrade(); | 65 | downGradeService.tryDowngrade(); |
73 | } | 66 | } |
74 | - | ||
75 | - // 7、打印响应超过500ms的请求与参数 | 67 | + // 6、打印响应超过500ms的请求与参数 |
76 | CONTROLLER_COST.info("run more than 500ms ,cost [{}] ms, RequestURL: {}", cost, getRequestUrl(request)); | 68 | CONTROLLER_COST.info("run more than 500ms ,cost [{}] ms, RequestURL: {}", cost, getRequestUrl(request)); |
77 | 69 | ||
78 | } catch (Exception e) { | 70 | } catch (Exception e) { |
@@ -98,8 +90,4 @@ public class ControllerCostInterceptor implements HandlerInterceptor{ | @@ -98,8 +90,4 @@ public class ControllerCostInterceptor implements HandlerInterceptor{ | ||
98 | return sb; | 90 | return sb; |
99 | } | 91 | } |
100 | 92 | ||
101 | - Map<String, Object> getLogReqParams(final HttpServletRequest request, final HttpServletResponse response) { | ||
102 | - return HttpRequestUtils.getRequestParams(request); | ||
103 | - } | ||
104 | - | ||
105 | } | 93 | } |
1 | package com.yoho.search.service.servicenew.impl; | 1 | package com.yoho.search.service.servicenew.impl; |
2 | 2 | ||
3 | -import java.util.ArrayList; | ||
4 | -import java.util.Arrays; | ||
5 | import java.util.List; | 3 | import java.util.List; |
6 | import java.util.Map; | 4 | import java.util.Map; |
7 | 5 | ||
8 | import org.apache.commons.lang.StringUtils; | 6 | import org.apache.commons.lang.StringUtils; |
9 | import org.elasticsearch.index.query.BoolQueryBuilder; | 7 | import org.elasticsearch.index.query.BoolQueryBuilder; |
10 | import org.elasticsearch.index.query.QueryBuilders; | 8 | import org.elasticsearch.index.query.QueryBuilders; |
11 | -import org.elasticsearch.search.aggregations.AbstractAggregationBuilder; | ||
12 | import org.elasticsearch.search.aggregations.Aggregation; | 9 | import org.elasticsearch.search.aggregations.Aggregation; |
13 | import org.springframework.beans.factory.annotation.Autowired; | 10 | import org.springframework.beans.factory.annotation.Autowired; |
14 | 11 | ||
15 | import com.alibaba.fastjson.JSONObject; | 12 | import com.alibaba.fastjson.JSONObject; |
16 | import com.yoho.search.base.utils.ConvertUtils; | 13 | import com.yoho.search.base.utils.ConvertUtils; |
17 | import com.yoho.search.core.es.agg.IAggregation; | 14 | import com.yoho.search.core.es.agg.IAggregation; |
18 | -import com.yoho.search.core.es.model.SearchParam; | ||
19 | import com.yoho.search.service.service.helper.SearchParamHelper; | 15 | import com.yoho.search.service.service.helper.SearchParamHelper; |
20 | import com.yoho.search.service.service.helper.SearchSortHelper; | 16 | import com.yoho.search.service.service.helper.SearchSortHelper; |
21 | 17 | ||
@@ -102,33 +98,6 @@ public class BaseService { | @@ -102,33 +98,6 @@ public class BaseService { | ||
102 | int page = StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page")); | 98 | int page = StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page")); |
103 | return page; | 99 | return page; |
104 | } | 100 | } |
105 | - | ||
106 | - protected SearchParam buildProductListSearchParam(Map<String, String> paramMap) throws Exception { | ||
107 | - // 1)验证查询条数 | ||
108 | - int pageSize = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum")); | ||
109 | - int page = StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page")); | ||
110 | - if (page < 1 || pageSize < 0) { | ||
111 | - throw new IllegalArgumentException("分页参数不合法"); | ||
112 | - } | ||
113 | - if (pageSize > 500) { | ||
114 | - pageSize = 500; | ||
115 | - } | ||
116 | - // 2)构建基本查询参数 | ||
117 | - SearchParam searchParam = searchParamHelper.buildWithPersional(paramMap, true); | ||
118 | - setHighlight(paramMap, searchParam); | ||
119 | - searchParam.setAggregationBuilders(null); | ||
120 | - searchParam.setPage(page); | ||
121 | - searchParam.setOffset((page - 1) * pageSize); | ||
122 | - searchParam.setSize(pageSize); | ||
123 | - // 3)设置排序字段 | ||
124 | - searchParam.setSortBuilders(searchSortHelper.buildSortList(paramMap)); | ||
125 | - // 4)设置查询结果返回字段 | ||
126 | - if (StringUtils.isNotBlank(paramMap.get("resultFields"))) { | ||
127 | - String resultFields = paramMap.get("resultFields"); | ||
128 | - searchParam.setResultFields(Arrays.asList(resultFields.split(","))); | ||
129 | - } | ||
130 | - return searchParam; | ||
131 | - } | ||
132 | 101 | ||
133 | protected JSONObject getFilterResults(List<IAggregation> aggregations, Map<String, Aggregation> aggMaps) { | 102 | protected JSONObject getFilterResults(List<IAggregation> aggregations, Map<String, Aggregation> aggMaps) { |
134 | JSONObject filter = new JSONObject(); | 103 | JSONObject filter = new JSONObject(); |
@@ -142,12 +111,4 @@ public class BaseService { | @@ -142,12 +111,4 @@ public class BaseService { | ||
142 | return filter; | 111 | return filter; |
143 | } | 112 | } |
144 | 113 | ||
145 | - private void setHighlight(final Map<String, String> paramMap, SearchParam searchParam) { | ||
146 | - if (StringUtils.isNotBlank(paramMap.get("highlight")) && "1".equals(paramMap.get("highlight")) && StringUtils.isNotBlank(paramMap.get("query"))) { | ||
147 | - searchParam.setHighlight(true); | ||
148 | - List<String> highlightFields = new ArrayList<String>(); | ||
149 | - highlightFields.add("productName.productName_ansj"); | ||
150 | - searchParam.setHighlightFields(highlightFields); | ||
151 | - } | ||
152 | - } | ||
153 | } | 114 | } |
@@ -11,6 +11,7 @@ import org.springframework.stereotype.Service; | @@ -11,6 +11,7 @@ import org.springframework.stereotype.Service; | ||
11 | import com.alibaba.fastjson.JSONObject; | 11 | import com.alibaba.fastjson.JSONObject; |
12 | import com.yoho.search.base.utils.SearchPageIdDefine; | 12 | import com.yoho.search.base.utils.SearchPageIdDefine; |
13 | import com.yoho.search.service.service.SearchDynamicConfigService; | 13 | import com.yoho.search.service.service.SearchDynamicConfigService; |
14 | +import com.yoho.search.service.service.SearchKeyWordService; | ||
14 | import com.yoho.search.service.service.helper.SearchCommonHelper; | 15 | import com.yoho.search.service.service.helper.SearchCommonHelper; |
15 | import com.yoho.search.service.servicenew.ISearchRecommendService; | 16 | import com.yoho.search.service.servicenew.ISearchRecommendService; |
16 | import com.yoho.search.service.servicenew.scene.common.AbstractSceneService; | 17 | import com.yoho.search.service.servicenew.scene.common.AbstractSceneService; |
@@ -21,7 +22,7 @@ import com.yoho.search.service.utils.SearchRequestParams; | @@ -21,7 +22,7 @@ import com.yoho.search.service.utils.SearchRequestParams; | ||
21 | import com.yoho.search.service.vo.SearchApiResult; | 22 | import com.yoho.search.service.vo.SearchApiResult; |
22 | 23 | ||
23 | @Service | 24 | @Service |
24 | -public class FuzzySceneService extends AbstractSceneService{ | 25 | +public class FuzzySceneService extends AbstractSceneService { |
25 | 26 | ||
26 | private static final Logger logger = LoggerFactory.getLogger(FuzzySceneService.class); | 27 | private static final Logger logger = LoggerFactory.getLogger(FuzzySceneService.class); |
27 | 28 | ||
@@ -35,16 +36,17 @@ public class FuzzySceneService extends AbstractSceneService{ | @@ -35,16 +36,17 @@ public class FuzzySceneService extends AbstractSceneService{ | ||
35 | private ISearchRecommendService searchRecommendService; | 36 | private ISearchRecommendService searchRecommendService; |
36 | @Autowired | 37 | @Autowired |
37 | private SearchDynamicConfigService searchDynamicConfigService; | 38 | private SearchDynamicConfigService searchDynamicConfigService; |
39 | + @Autowired | ||
40 | + private SearchKeyWordService searchKeyWordService; | ||
38 | 41 | ||
39 | // 当少于20个商品时 返回智能搜索词提示 | 42 | // 当少于20个商品时 返回智能搜索词提示 |
40 | private static final int SMART_SUGGESTION_PRODUCT_LIMIT = 20; | 43 | private static final int SMART_SUGGESTION_PRODUCT_LIMIT = 20; |
41 | - | 44 | + |
42 | @Override | 45 | @Override |
43 | public String pageId() { | 46 | public String pageId() { |
44 | return SearchPageIdDefine.PAGE_ID_SEARCH; | 47 | return SearchPageIdDefine.PAGE_ID_SEARCH; |
45 | } | 48 | } |
46 | 49 | ||
47 | - | ||
48 | /** | 50 | /** |
49 | * @1、返回商品列表 | 51 | * @1、返回商品列表 |
50 | * @2、数量太多则返回建议词 | 52 | * @2、数量太多则返回建议词 |
@@ -54,8 +56,14 @@ public class FuzzySceneService extends AbstractSceneService{ | @@ -54,8 +56,14 @@ public class FuzzySceneService extends AbstractSceneService{ | ||
54 | try { | 56 | try { |
55 | // 1、获取商品列表 | 57 | // 1、获取商品列表 |
56 | SearchApiResult searchApiResult = splitProductListService.productList(paramMap); | 58 | SearchApiResult searchApiResult = splitProductListService.productList(paramMap); |
57 | - // 2、加入建议词 | 59 | + // 2)、加入建议词 |
58 | this.addSuggestion(searchApiResult, paramMap); | 60 | this.addSuggestion(searchApiResult, paramMap); |
61 | + // 3)模糊搜索页记录关键字对应的查询结果 | ||
62 | + String queryWord = paramMap.get("query"); | ||
63 | + if (!StringUtils.isBlank(queryWord) && !searchCommonHelper.isQuerySkn(queryWord)) { | ||
64 | + long total = ((JSONObject) searchApiResult.getData()).getLongValue("total"); | ||
65 | + searchKeyWordService.recordKeyWordByResultCount(queryWord, total); | ||
66 | + } | ||
59 | return searchApiResult; | 67 | return searchApiResult; |
60 | } catch (Exception e) { | 68 | } catch (Exception e) { |
61 | logger.error("[func=fuzzyProductList][params=" + paramMap + "]", e); | 69 | logger.error("[func=fuzzyProductList][params=" + paramMap + "]", e); |
@@ -9,6 +9,7 @@ import org.springframework.stereotype.Service; | @@ -9,6 +9,7 @@ import org.springframework.stereotype.Service; | ||
9 | 9 | ||
10 | import com.alibaba.fastjson.JSONObject; | 10 | import com.alibaba.fastjson.JSONObject; |
11 | import com.yoho.search.base.utils.SearchPageIdDefine; | 11 | import com.yoho.search.base.utils.SearchPageIdDefine; |
12 | +import com.yoho.search.service.service.helper.SearchCommonHelper; | ||
12 | import com.yoho.search.service.servicenew.scene.common.AbstractSceneService; | 13 | import com.yoho.search.service.servicenew.scene.common.AbstractSceneService; |
13 | import com.yoho.search.service.servicenew.scene.common.SceneProductListService; | 14 | import com.yoho.search.service.servicenew.scene.common.SceneProductListService; |
14 | import com.yoho.search.service.servicenew.scene.common.SceneSelectionsService; | 15 | import com.yoho.search.service.servicenew.scene.common.SceneSelectionsService; |
@@ -24,6 +25,8 @@ public class SortSceneService extends AbstractSceneService { | @@ -24,6 +25,8 @@ public class SortSceneService extends AbstractSceneService { | ||
24 | private SceneProductListService splitProductListService; | 25 | private SceneProductListService splitProductListService; |
25 | @Autowired | 26 | @Autowired |
26 | private SceneSelectionsService splitSelectionsService; | 27 | private SceneSelectionsService splitSelectionsService; |
28 | + @Autowired | ||
29 | + private SearchCommonHelper searchCommonHelper; | ||
27 | 30 | ||
28 | @Override | 31 | @Override |
29 | public String pageId() { | 32 | public String pageId() { |
@@ -33,13 +36,18 @@ public class SortSceneService extends AbstractSceneService { | @@ -33,13 +36,18 @@ public class SortSceneService extends AbstractSceneService { | ||
33 | @Override | 36 | @Override |
34 | public SearchApiResult productList(Map<String, String> paramMap) { | 37 | public SearchApiResult productList(Map<String, String> paramMap) { |
35 | try { | 38 | try { |
36 | - return splitProductListService.productList(paramMap); | 39 | + // 1、获取商品列表 |
40 | + SearchApiResult productList = splitProductListService.productList(paramMap); | ||
41 | + // 2、返回规则聚合结果 | ||
42 | + // TODO | ||
43 | + // 3、返回最终结果 | ||
44 | + return productList; | ||
37 | } catch (Exception e) { | 45 | } catch (Exception e) { |
38 | logger.error("[func=sortProductList][params=" + paramMap + "]", e); | 46 | logger.error("[func=sortProductList][params=" + paramMap + "]", e); |
39 | return SearchApiResultUtils.errorSearchApiResult("sortProductList", paramMap, e); | 47 | return SearchApiResultUtils.errorSearchApiResult("sortProductList", paramMap, e); |
40 | } | 48 | } |
41 | } | 49 | } |
42 | - | 50 | + |
43 | @Override | 51 | @Override |
44 | public SearchApiResult aggregations(Map<String, String> paramMap) { | 52 | public SearchApiResult aggregations(Map<String, String> paramMap) { |
45 | // 1、获取通用筛选项 | 53 | // 1、获取通用筛选项 |
1 | package com.yoho.search.service.servicenew.scene.common; | 1 | package com.yoho.search.service.servicenew.scene.common; |
2 | 2 | ||
3 | import java.util.ArrayList; | 3 | import java.util.ArrayList; |
4 | +import java.util.Arrays; | ||
4 | import java.util.Iterator; | 5 | import java.util.Iterator; |
5 | import java.util.List; | 6 | import java.util.List; |
6 | import java.util.Map; | 7 | import java.util.Map; |
@@ -21,9 +22,10 @@ import com.yoho.search.service.cache.SearchCacheMatchLogger; | @@ -21,9 +22,10 @@ import com.yoho.search.service.cache.SearchCacheMatchLogger; | ||
21 | import com.yoho.search.service.cache.model.SearchCache; | 22 | import com.yoho.search.service.cache.model.SearchCache; |
22 | import com.yoho.search.service.service.SearchCacheService; | 23 | import com.yoho.search.service.service.SearchCacheService; |
23 | import com.yoho.search.service.service.SearchCommonService; | 24 | import com.yoho.search.service.service.SearchCommonService; |
24 | -import com.yoho.search.service.service.SearchKeyWordService; | ||
25 | import com.yoho.search.service.service.base.ProductIndexBaseService; | 25 | import com.yoho.search.service.service.base.ProductIndexBaseService; |
26 | import com.yoho.search.service.service.helper.SearchCommonHelper; | 26 | import com.yoho.search.service.service.helper.SearchCommonHelper; |
27 | +import com.yoho.search.service.service.helper.SearchParamHelper; | ||
28 | +import com.yoho.search.service.service.helper.SearchSortHelper; | ||
27 | import com.yoho.search.service.servicenew.impl.BaseService; | 29 | import com.yoho.search.service.servicenew.impl.BaseService; |
28 | import com.yoho.search.service.vo.SearchApiResult; | 30 | import com.yoho.search.service.vo.SearchApiResult; |
29 | 31 | ||
@@ -35,19 +37,21 @@ public class SceneProductListService extends BaseService { | @@ -35,19 +37,21 @@ public class SceneProductListService extends BaseService { | ||
35 | @Autowired | 37 | @Autowired |
36 | private SearchCommonService searchCommonService; | 38 | private SearchCommonService searchCommonService; |
37 | @Autowired | 39 | @Autowired |
38 | - private SearchKeyWordService searchKeyWordService; | ||
39 | - @Autowired | ||
40 | private ProductIndexBaseService productIndexBaseService; | 40 | private ProductIndexBaseService productIndexBaseService; |
41 | @Autowired | 41 | @Autowired |
42 | private SearchCacheService searchCacheService; | 42 | private SearchCacheService searchCacheService; |
43 | @Autowired | 43 | @Autowired |
44 | private SearchCacheFactory searchCacheFactory; | 44 | private SearchCacheFactory searchCacheFactory; |
45 | + @Autowired | ||
46 | + private SearchParamHelper searchParamHelper; | ||
47 | + @Autowired | ||
48 | + private SearchSortHelper searchSortHelper; | ||
45 | 49 | ||
46 | - private SearchCache productListSearchCache; | 50 | + private SearchCache searchCache; |
47 | 51 | ||
48 | @PostConstruct | 52 | @PostConstruct |
49 | void init() { | 53 | void init() { |
50 | - productListSearchCache = searchCacheFactory.getProductListSearchCache(); | 54 | + searchCache = searchCacheFactory.getProductListSearchCache(); |
51 | } | 55 | } |
52 | 56 | ||
53 | /** | 57 | /** |
@@ -63,7 +67,7 @@ public class SceneProductListService extends BaseService { | @@ -63,7 +67,7 @@ public class SceneProductListService extends BaseService { | ||
63 | 67 | ||
64 | // 2)从缓存中获取数据 | 68 | // 2)从缓存中获取数据 |
65 | final String indexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX; | 69 | final String indexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX; |
66 | - JSONObject cacheObject = searchCacheService.getJSONObjectFromCache(productListSearchCache, indexName, searchParam); | 70 | + JSONObject cacheObject = searchCacheService.getJSONObjectFromCache(searchCache, indexName, searchParam); |
67 | if (cacheObject != null) { | 71 | if (cacheObject != null) { |
68 | SearchCacheMatchLogger.doSearchCacheMatchLog("/split/productList.json", paramMap); | 72 | SearchCacheMatchLogger.doSearchCacheMatchLog("/split/productList.json", paramMap); |
69 | return new SearchApiResult().setData(cacheObject); | 73 | return new SearchApiResult().setData(cacheObject); |
@@ -75,26 +79,27 @@ public class SceneProductListService extends BaseService { | @@ -75,26 +79,27 @@ public class SceneProductListService extends BaseService { | ||
75 | return new SearchApiResult().setCode(500).setMessage("execption"); | 79 | return new SearchApiResult().setCode(500).setMessage("execption"); |
76 | } | 80 | } |
77 | 81 | ||
78 | - // 4)记录关键字对应的查询结果 | ||
79 | - String queryWord = paramMap.get("query"); | ||
80 | - if (!StringUtils.isBlank(queryWord) && !searchCommonHelper.isQuerySkn(queryWord)) { | ||
81 | - searchKeyWordService.recordKeyWordByResultCount(queryWord, searchResult.getTotal()); | ||
82 | - } | ||
83 | - | ||
84 | - // 5)构造返回结果 | 82 | + // 4)构造返回结果 |
85 | JSONObject dataMap = new JSONObject(); | 83 | JSONObject dataMap = new JSONObject(); |
86 | dataMap.put("total", searchResult.getTotal()); | 84 | dataMap.put("total", searchResult.getTotal()); |
87 | dataMap.put("page", searchResult.getPage()); | 85 | dataMap.put("page", searchResult.getPage()); |
88 | dataMap.put("page_size", searchParam.getSize()); | 86 | dataMap.put("page_size", searchParam.getSize()); |
89 | dataMap.put("page_total", searchResult.getTotalPage()); | 87 | dataMap.put("page_total", searchResult.getTotalPage()); |
90 | List<Map<String, Object>> product_list = productIndexBaseService.getProductListWithPricePlan(searchResult.getResultList(), null); | 88 | List<Map<String, Object>> product_list = productIndexBaseService.getProductListWithPricePlan(searchResult.getResultList(), null); |
91 | - dataMap.put("product_list", this.moveProductListSort(paramMap, product_list));// 处理一下商品的顺序 | ||
92 | - | ||
93 | - // 6)将结果存进缓存 | ||
94 | - searchCacheService.addJSONObjectToCache(productListSearchCache, indexName, searchParam, dataMap); | 89 | + dataMap.put("product_list", this.moveProductListSort(paramMap, product_list)); |
90 | + | ||
91 | + // 5)将结果存进缓存 | ||
92 | + searchCacheService.addJSONObjectToCache(searchCache, indexName, searchParam, dataMap); | ||
95 | return new SearchApiResult().setData(dataMap); | 93 | return new SearchApiResult().setData(dataMap); |
96 | } | 94 | } |
97 | 95 | ||
96 | + /** | ||
97 | + * 品类列表页针对第一页的商品,将价格高的往前面平移8个 | ||
98 | + * | ||
99 | + * @param paramMap | ||
100 | + * @param product_list | ||
101 | + * @return | ||
102 | + */ | ||
98 | private List<Map<String, Object>> moveProductListSort(Map<String, String> paramMap, List<Map<String, Object>> product_list) { | 103 | private List<Map<String, Object>> moveProductListSort(Map<String, String> paramMap, List<Map<String, Object>> product_list) { |
99 | // 判断页面合法性 | 104 | // 判断页面合法性 |
100 | if (!searchCommonHelper.isSortPageDefault(paramMap)) { | 105 | if (!searchCommonHelper.isSortPageDefault(paramMap)) { |
@@ -133,4 +138,40 @@ public class SceneProductListService extends BaseService { | @@ -133,4 +138,40 @@ public class SceneProductListService extends BaseService { | ||
133 | } | 138 | } |
134 | return results; | 139 | return results; |
135 | } | 140 | } |
141 | + | ||
142 | + private SearchParam buildProductListSearchParam(Map<String, String> paramMap) throws Exception { | ||
143 | + // 1)验证查询条数 | ||
144 | + int pageSize = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum")); | ||
145 | + int page = StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page")); | ||
146 | + if (page < 1 || pageSize < 0) { | ||
147 | + throw new IllegalArgumentException("分页参数不合法"); | ||
148 | + } | ||
149 | + if (pageSize > 500) { | ||
150 | + pageSize = 500; | ||
151 | + } | ||
152 | + // 2)构建基本查询参数 | ||
153 | + SearchParam searchParam = searchParamHelper.buildWithPersional(paramMap, true); | ||
154 | + setHighlight(paramMap, searchParam); | ||
155 | + searchParam.setAggregationBuilders(null); | ||
156 | + searchParam.setPage(page); | ||
157 | + searchParam.setOffset((page - 1) * pageSize); | ||
158 | + searchParam.setSize(pageSize); | ||
159 | + // 3)设置排序字段 | ||
160 | + searchParam.setSortBuilders(searchSortHelper.buildSortList(paramMap)); | ||
161 | + // 4)设置查询结果返回字段 | ||
162 | + if (StringUtils.isNotBlank(paramMap.get("resultFields"))) { | ||
163 | + String resultFields = paramMap.get("resultFields"); | ||
164 | + searchParam.setResultFields(Arrays.asList(resultFields.split(","))); | ||
165 | + } | ||
166 | + return searchParam; | ||
167 | + } | ||
168 | + | ||
169 | + private void setHighlight(final Map<String, String> paramMap, SearchParam searchParam) { | ||
170 | + if (StringUtils.isNotBlank(paramMap.get("highlight")) && "1".equals(paramMap.get("highlight")) && StringUtils.isNotBlank(paramMap.get("query"))) { | ||
171 | + searchParam.setHighlight(true); | ||
172 | + List<String> highlightFields = new ArrayList<String>(); | ||
173 | + highlightFields.add("productName.productName_ansj"); | ||
174 | + searchParam.setHighlightFields(highlightFields); | ||
175 | + } | ||
176 | + } | ||
136 | } | 177 | } |
-
Please register or login to post a comment