Authored by 胡古飞

代码优化

@@ -4,28 +4,29 @@ import com.yoho.search.common.utils.HttpServletRequestUtils; @@ -4,28 +4,29 @@ import com.yoho.search.common.utils.HttpServletRequestUtils;
4 import com.yoho.search.models.SearchApiResult; 4 import com.yoho.search.models.SearchApiResult;
5 import com.yoho.search.service.scene.ufo.UfoBrandListService; 5 import com.yoho.search.service.scene.ufo.UfoBrandListService;
6 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.beans.factory.annotation.Autowired;
7 -import org.springframework.stereotype.Controller;  
8 import org.springframework.web.bind.annotation.RequestMapping; 7 import org.springframework.web.bind.annotation.RequestMapping;
9 import org.springframework.web.bind.annotation.RequestMethod; 8 import org.springframework.web.bind.annotation.RequestMethod;
10 -import org.springframework.web.bind.annotation.ResponseBody; 9 +import org.springframework.web.bind.annotation.RestController;
11 10
12 import javax.servlet.http.HttpServletRequest; 11 import javax.servlet.http.HttpServletRequest;
13 import java.util.Map; 12 import java.util.Map;
14 13
15 -/**  
16 - * @author wangnan  
17 - * @version 2018/12/13  
18 - */  
19 -@Controller 14 +@RestController
20 public class UfoBrandListController { 15 public class UfoBrandListController {
21 16
22 @Autowired 17 @Autowired
23 private UfoBrandListService ufoBrandListService; 18 private UfoBrandListService ufoBrandListService;
24 19
  20 + /**
  21 + * ufo聚合出来的品牌列表
  22 + *
  23 + * @param request
  24 + * @return
  25 + */
25 @RequestMapping(method = RequestMethod.GET, value = "/ufo/brandList") 26 @RequestMapping(method = RequestMethod.GET, value = "/ufo/brandList")
26 - @ResponseBody  
27 - public SearchApiResult brandList(HttpServletRequest request) { 27 + public SearchApiResult ufoBrandList(HttpServletRequest request) {
28 Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request); 28 Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
29 - return ufoBrandListService.aggAllBrandList(paramMap); 29 + return ufoBrandListService.ufoBrandList(paramMap);
30 } 30 }
  31 +
31 } 32 }
@@ -162,7 +162,6 @@ public class AggregationFactory { @@ -162,7 +162,6 @@ public class AggregationFactory {
162 return new ShopAndSknAggregation(aggCount); 162 return new ShopAndSknAggregation(aggCount);
163 } 163 }
164 164
165 -  
166 public IAggregation getUfoBrandAggregation(Map<String, String> paramMap) { 165 public IAggregation getUfoBrandAggregation(Map<String, String> paramMap) {
167 return new UfoBrandAggregation(ufoBrandIndexBaseService, paramMap); 166 return new UfoBrandAggregation(ufoBrandIndexBaseService, paramMap);
168 } 167 }
@@ -13,7 +13,6 @@ import com.yoho.search.core.es.model.SearchResult; @@ -13,7 +13,6 @@ import com.yoho.search.core.es.model.SearchResult;
13 import com.yoho.search.models.SearchApiResult; 13 import com.yoho.search.models.SearchApiResult;
14 import com.yoho.search.service.aggregations.impls.AggregationFactory; 14 import com.yoho.search.service.aggregations.impls.AggregationFactory;
15 import com.yoho.search.service.helper.UfoSearchQueryHelper; 15 import com.yoho.search.service.helper.UfoSearchQueryHelper;
16 -import org.elasticsearch.common.Nullable;  
17 import org.elasticsearch.index.query.BoolQueryBuilder; 16 import org.elasticsearch.index.query.BoolQueryBuilder;
18 import org.elasticsearch.index.query.QueryBuilder; 17 import org.elasticsearch.index.query.QueryBuilder;
19 import org.elasticsearch.search.aggregations.Aggregation; 18 import org.elasticsearch.search.aggregations.Aggregation;
@@ -25,10 +24,6 @@ import org.springframework.stereotype.Service; @@ -25,10 +24,6 @@ import org.springframework.stereotype.Service;
25 import java.util.Arrays; 24 import java.util.Arrays;
26 import java.util.Map; 25 import java.util.Map;
27 26
28 -/**  
29 - * @author wangnan  
30 - * @version 2018/9/17  
31 - */  
32 @Service 27 @Service
33 public class UfoBrandListService { 28 public class UfoBrandListService {
34 private static final Logger logger = LoggerFactory.getLogger(UfoBrandListService.class); 29 private static final Logger logger = LoggerFactory.getLogger(UfoBrandListService.class);
@@ -41,7 +36,7 @@ public class UfoBrandListService { @@ -41,7 +36,7 @@ public class UfoBrandListService {
41 private SearchCommonService searchCommonService; 36 private SearchCommonService searchCommonService;
42 37
43 @SearchCacheAble(cacheName = "UFO_BRAND_LIST", cacheInMinute = CacheInMinute.Minute_UFO) 38 @SearchCacheAble(cacheName = "UFO_BRAND_LIST", cacheInMinute = CacheInMinute.Minute_UFO)
44 - public SearchApiResult aggAllBrandList(Map<String, String> paramMap) { 39 + public SearchApiResult ufoBrandList(Map<String, String> paramMap) {
45 try { 40 try {
46 // 1、构造带filter和query的SearchParam 41 // 1、构造带filter和query的SearchParam
47 SearchParam searchParam = new SearchParam(); 42 SearchParam searchParam = new SearchParam();
@@ -55,8 +50,13 @@ public class UfoBrandListService { @@ -55,8 +50,13 @@ public class UfoBrandListService {
55 IAggregation brandAggregation = aggregationFactory.getUfoBrandAggregation(paramMap); 50 IAggregation brandAggregation = aggregationFactory.getUfoBrandAggregation(paramMap);
56 searchParam.setAggregationBuilders(Arrays.asList(brandAggregation.getBuilder())); 51 searchParam.setAggregationBuilders(Arrays.asList(brandAggregation.getBuilder()));
57 52
58 - // 3、调父类方法执行查询  
59 - JSONArray brandJSONArray = doRealQuery(searchParam, brandAggregation); 53 + // 3、执行搜索
  54 + SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_UFO_PRODUCT_INDEX, searchParam);
  55 + Map<String, Aggregation> aggMaps = searchResult.getAggMaps();
  56 + JSONObject jsonObject = brandAggregation.getAggNameAndResponse(aggMaps);
  57 + JSONArray brandJSONArray = jsonObject.getJSONArray(brandAggregation.aggName());
  58 +
  59 + // 4、返回结果
60 JSONObject dataMap = new JSONObject(); 60 JSONObject dataMap = new JSONObject();
61 dataMap.put("brand_list", brandJSONArray); 61 dataMap.put("brand_list", brandJSONArray);
62 return new SearchApiResult().setData(dataMap); 62 return new SearchApiResult().setData(dataMap);
@@ -65,18 +65,4 @@ public class UfoBrandListService { @@ -65,18 +65,4 @@ public class UfoBrandListService {
65 } 65 }
66 } 66 }
67 67
68 - private JSONArray doRealQuery(SearchParam searchParam, @Nullable Object... params) throws Exception {  
69 - // 1、从参数中获取IAggregation brandAggregation  
70 - IAggregation brandAggregation = (IAggregation) params[0];  
71 -  
72 - // 2、从ES中获取  
73 - SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_UFO_PRODUCT_INDEX, searchParam);  
74 - Map<String, Aggregation> aggMaps = searchResult.getAggMaps();  
75 - JSONObject jsonObject = brandAggregation.getAggNameAndResponse(aggMaps);  
76 -  
77 - // 3、生成结果  
78 - JSONArray brandJSONArray = jsonObject.getJSONArray(brandAggregation.aggName());  
79 - return brandJSONArray;  
80 -  
81 - }  
82 } 68 }