Authored by hugufei

代码优化

package com.yoho.search.service.scene;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import com.alibaba.fastjson.JSONArray;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
... ... @@ -88,9 +86,9 @@ public class FuzzySceneService extends AbstractSceneService {
// 3、获取商品列表
CompletableFuture<SearchApiResult> productListuture = CompletableFuture.supplyAsync(() -> sceneProductListService.productList(this.newParamMap(paramMap)), executor);
// 4、获取自定义标签聚合结果
CompletableFuture<SearchApiResult> customizeTagFuture = CompletableFuture.supplyAsync(() -> sceneAggregationsHelper.secneAggCustomizeTag(this.newParamMap(paramMap)), executor);
CompletableFuture<SearchApiResult> customizeTagFuture = CompletableFuture.supplyAsync(() -> sceneAggregationsHelper.sceneAggCustomizeTag(this.newParamMap(paramMap)), executor);
// 5、获取促销专题
CompletableFuture<SearchApiResult> recommendPromotionFuture = CompletableFuture.supplyAsync(() -> sceneAggregationsHelper.secneAggRecommendPromotion(this.newParamMap(paramMap)),
CompletableFuture<SearchApiResult> recommendPromotionFuture = CompletableFuture.supplyAsync(() -> sceneAggregationsHelper.sceneAggRecommendPromotion(this.newParamMap(paramMap)),
executor);
// 6、加入建议词
... ... @@ -108,7 +106,6 @@ public class FuzzySceneService extends AbstractSceneService {
SearchApiResult recommendProducts = recommendPromotionFuture.get();
JSONObject dataMap = (JSONObject) productListResult.getData();
dataMap.put(CUSTOMIZE_TAG_LIST, customizeTags.getData());
if (recommendProducts.getData() == null) {
dataMap.put(RECOMMEND_PROMOTION_LIST, Collections.EMPTY_LIST);
} else {
... ...
package com.yoho.search.service.scene;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import com.alibaba.fastjson.JSONArray;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
... ... @@ -103,10 +100,8 @@ public class SortSceneService extends AbstractSceneService {
}
// 3、获取聚合结果
CompletableFuture<SearchApiResult> standardsFuture = CompletableFuture.supplyAsync(() -> sceneAggregationsHelper.sceneAggStandard(this.newParamMap(paramMap)), executorService);
CompletableFuture<SearchApiResult> customizeTagFuture = CompletableFuture.supplyAsync(() -> sceneAggregationsHelper.secneAggCustomizeTag(this.newParamMap(paramMap)),
executorService);
CompletableFuture<SearchApiResult> recommendProductFuture = CompletableFuture.supplyAsync(() -> sceneAggregationsHelper.secneAggRecommendPromotion(this.newParamMap(paramMap)),
executorService);
CompletableFuture<SearchApiResult> customizeTagFuture = CompletableFuture.supplyAsync(() -> sceneAggregationsHelper.sceneAggCustomizeTag(this.newParamMap(paramMap)),executorService);
CompletableFuture<SearchApiResult> recommendProductFuture = CompletableFuture.supplyAsync(() -> sceneAggregationsHelper.sceneAggRecommendPromotion(this.newParamMap(paramMap)),executorService);
// 4、组合结果
SearchApiResult productList = productListFuture.get();
SearchApiResult standards = standardsFuture.get();
... ...
package com.yoho.search.service.scene.aggregations;
import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yoho.search.service.base.SearchRequestParams;
import org.elasticsearch.search.aggregations.Aggregation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
... ... @@ -37,7 +33,7 @@ public class SceneAggregationsHelper {
private Object getAggregationResponse(IAggregation aggregation, Map<String, String> paramMap) throws Exception {
// 1、构造SearchParam
SearchParam searchParam = searchParamHelper.buildWithFilterParam(paramMap, null);
SearchParam searchParam = searchParamHelper.buildDefault(paramMap);
searchParam.setAggregationBuilders(Arrays.asList(aggregation.getBuilder()));
searchParam.setOffset(0);
searchParam.setSize(0);
... ... @@ -60,7 +56,7 @@ public class SceneAggregationsHelper {
}
@SearchCacheAble(cacheName = "SCENE_AGG_CUSTOMIZETAG", cacheInMinute = 30, excludeParams = { "uid", "order", "page", "viewNum", "yh_channel" })
public SearchApiResult secneAggCustomizeTag(Map<String, String> paramMap) {
public SearchApiResult sceneAggCustomizeTag(Map<String, String> paramMap) {
try {
IAggregation customizeAggregation = aggregationFactoryService.getCustomizeTagAggregation(paramMap);
Object respone = this.getAggregationResponse(customizeAggregation, paramMap);
... ... @@ -70,32 +66,14 @@ public class SceneAggregationsHelper {
return new SearchApiResult().setCode(500).setMessage("secneAggCustomizeTag Exception");
}
}
@SearchCacheAble(cacheName = "SCENE_AGG_RECOMMENDPROMOTION", cacheInMinute = 30, excludeParams = { "uid", "order", "page", "viewNum", "yh_channel" })
public SearchApiResult secneAggRecommendPromotion(Map<String, String> paramMap) {
public SearchApiResult sceneAggRecommendPromotion(Map<String, String> paramMap) {
try {
// 1、获取aggregation
IAggregation aggregation = aggregationFactoryService.getRecommendPromotionAggregation(100);
// 1.1添加默认参数
paramMap.remove(SearchRequestParams.PARAM_SEARCH_ORDER);// 此接口不支持order
paramMap.put(SearchRequestParams.PARAM_SEARCH_GLOBAL_FILTER_BRAND, "Y");// 页面屏蔽
paramMap.put(SearchRequestParams.PARAM_SEARCH_STATUS, "1");// 上架
paramMap.put(SearchRequestParams.PARAM_SEARCH_STOCKNUM, "1");// 有库存
paramMap.put(SearchRequestParams.PARAM_SEARCH_ISOUTLETS, "2");// 非奥莱
paramMap.put(SearchRequestParams.PARAM_SEARCH_ATTRIBUTE_NOT, "2");// 非赠品
// 2、构建带queryBuilder和filter的SearchParam
SearchParam searchParam = searchParamHelper.buildWithPersional(paramMap, false);
searchParam.setAggregationBuilders(Arrays.asList(aggregation.getBuilder()));
// 4、构建offset
searchParam.setOffset(100);// justForCache
// 6、从ES中获取
SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
Map<String, Aggregation> aggMaps = searchResult.getAggMaps();
JSONObject respone = aggregation.getAggNameAndResponse(aggMaps);
if (respone == null) {
return new SearchApiResult().setData(Collections.emptyList());
}
JSONArray recommendPromotions = respone.getJSONArray("recommendPromotionAgg");
return new SearchApiResult().setData(recommendPromotions);
Object respone = this.getAggregationResponse(aggregation, paramMap);
return new SearchApiResult().setData(respone);
} catch (Exception e) {
logger.error(e.getMessage(), e);
return new SearchApiResult().setCode(500).setMessage("secneAggrecommendPromotion Exception");
... ...
package com.yoho.search.service.service;
import java.util.List;
import java.util.Map;
import org.elasticsearch.index.query.BoolQueryBuilder;
... ...
... ... @@ -2,7 +2,6 @@ package com.yoho.search.service.service.impl;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
... ...