Authored by 胡古飞

代码优化

... ... @@ -4,28 +4,29 @@ import com.yoho.search.common.utils.HttpServletRequestUtils;
import com.yoho.search.models.SearchApiResult;
import com.yoho.search.service.scene.ufo.UfoBrandListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
/**
* @author wangnan
* @version 2018/12/13
*/
@Controller
@RestController
public class UfoBrandListController {
@Autowired
private UfoBrandListService ufoBrandListService;
/**
* ufo聚合出来的品牌列表
*
* @param request
* @return
*/
@RequestMapping(method = RequestMethod.GET, value = "/ufo/brandList")
@ResponseBody
public SearchApiResult brandList(HttpServletRequest request) {
public SearchApiResult ufoBrandList(HttpServletRequest request) {
Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
return ufoBrandListService.aggAllBrandList(paramMap);
return ufoBrandListService.ufoBrandList(paramMap);
}
}
... ...
... ... @@ -162,7 +162,6 @@ public class AggregationFactory {
return new ShopAndSknAggregation(aggCount);
}
public IAggregation getUfoBrandAggregation(Map<String, String> paramMap) {
return new UfoBrandAggregation(ufoBrandIndexBaseService, paramMap);
}
... ...
... ... @@ -13,7 +13,6 @@ import com.yoho.search.core.es.model.SearchResult;
import com.yoho.search.models.SearchApiResult;
import com.yoho.search.service.aggregations.impls.AggregationFactory;
import com.yoho.search.service.helper.UfoSearchQueryHelper;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.search.aggregations.Aggregation;
... ... @@ -25,10 +24,6 @@ import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.Map;
/**
* @author wangnan
* @version 2018/9/17
*/
@Service
public class UfoBrandListService {
private static final Logger logger = LoggerFactory.getLogger(UfoBrandListService.class);
... ... @@ -41,7 +36,7 @@ public class UfoBrandListService {
private SearchCommonService searchCommonService;
@SearchCacheAble(cacheName = "UFO_BRAND_LIST", cacheInMinute = CacheInMinute.Minute_UFO)
public SearchApiResult aggAllBrandList(Map<String, String> paramMap) {
public SearchApiResult ufoBrandList(Map<String, String> paramMap) {
try {
// 1、构造带filter和query的SearchParam
SearchParam searchParam = new SearchParam();
... ... @@ -55,8 +50,13 @@ public class UfoBrandListService {
IAggregation brandAggregation = aggregationFactory.getUfoBrandAggregation(paramMap);
searchParam.setAggregationBuilders(Arrays.asList(brandAggregation.getBuilder()));
// 3、调父类方法执行查询
JSONArray brandJSONArray = doRealQuery(searchParam, brandAggregation);
// 3、执行搜索
SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_UFO_PRODUCT_INDEX, searchParam);
Map<String, Aggregation> aggMaps = searchResult.getAggMaps();
JSONObject jsonObject = brandAggregation.getAggNameAndResponse(aggMaps);
JSONArray brandJSONArray = jsonObject.getJSONArray(brandAggregation.aggName());
// 4、返回结果
JSONObject dataMap = new JSONObject();
dataMap.put("brand_list", brandJSONArray);
return new SearchApiResult().setData(dataMap);
... ... @@ -65,18 +65,4 @@ public class UfoBrandListService {
}
}
private JSONArray doRealQuery(SearchParam searchParam, @Nullable Object... params) throws Exception {
// 1、从参数中获取IAggregation brandAggregation
IAggregation brandAggregation = (IAggregation) params[0];
// 2、从ES中获取
SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_UFO_PRODUCT_INDEX, searchParam);
Map<String, Aggregation> aggMaps = searchResult.getAggMaps();
JSONObject jsonObject = brandAggregation.getAggNameAndResponse(aggMaps);
// 3、生成结果
JSONArray brandJSONArray = jsonObject.getJSONArray(brandAggregation.aggName());
return brandJSONArray;
}
}
... ...