Authored by hugufei

fix sceneAggRecommendPromotion

package com.yoho.search.models;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
public class RecommendPromotionAggVO {
@JSONField(name = "promotion_info_id")
private int promotionInfoId;
private int promotion_info_id;
private String coverimg_url;
private String title;
@JSONField(name = "coverimg_url")
private String coverImgUrl;
private long total_count;
private String title;
public int getPromotion_info_id() {
return promotion_info_id;
}
public int getPromotionInfoId() {
return promotionInfoId;
}
public void setPromotion_info_id(int promotion_info_id) {
this.promotion_info_id = promotion_info_id;
}
public void setPromotionInfoId(int promotionInfoId) {
this.promotionInfoId = promotionInfoId;
}
public String getCoverimg_url() {
return coverimg_url;
}
public String getCoverImgUrl() {
return coverImgUrl;
}
public void setCoverimg_url(String coverimg_url) {
this.coverimg_url = coverimg_url;
}
public void setCoverImgUrl(String coverImgUrl) {
this.coverImgUrl = coverImgUrl;
}
public String getTitle() {
return title;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public void setTitle(String title) {
this.title = title;
}
public long getTotal_count() {
return total_count;
}
public static void main(String[] args) {
RecommendPromotionAggVO recommendPromotionAggVO = new RecommendPromotionAggVO();
recommendPromotionAggVO.setPromotionInfoId(1);
recommendPromotionAggVO.setCoverImgUrl("sdf");
recommendPromotionAggVO.setTitle("dfas");
public void setTotal_count(long total_count) {
this.total_count = total_count;
}
System.out.println(JSON.toJSONString(recommendPromotionAggVO));
;
}
}
... ...
package com.yoho.search.service.aggregations.impls;
import com.yoho.search.base.utils.DateUtil;
import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.base.utils.ProductIndexEsField;
import com.yoho.search.core.es.agg.AbstractAggregation;
import com.yoho.search.service.base.index.PromotionIndexBaseService;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
... ... @@ -15,10 +16,13 @@ import org.elasticsearch.search.aggregations.bucket.nested.InternalNested;
import org.elasticsearch.search.aggregations.bucket.nested.NestedAggregationBuilder;
import org.elasticsearch.search.aggregations.bucket.terms.LongTerms;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.springframework.util.CollectionUtils;
import java.util.*;
import java.util.stream.Collectors;
import com.yoho.search.base.utils.DateUtil;
import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.base.utils.ProductIndexEsField;
import com.yoho.search.core.es.agg.AbstractAggregation;
import com.yoho.search.models.RecommendPromotionAggVO;
import com.yoho.search.service.base.index.PromotionIndexBaseService;
public class RecommendPromotionAggregation extends AbstractAggregation {
private static final String TERM_AGGREGATION_NAME = "promotionIdAgg";
... ... @@ -66,15 +70,16 @@ public class RecommendPromotionAggregation extends AbstractAggregation {
@Override
public Object getAggregationResponseMap(Map<String, Aggregation> aggMaps) {
InternalNested aggregation = (InternalNested) aggMaps.get(aggName());
if (!CollectionUtils.isEmpty(aggregation.getAggregations().asList())) {
InternalFilter filter = (InternalFilter)aggregation.getAggregations().asList().get(0);
List<LongTerms.Bucket> longTerms = ((LongTerms)filter.getAggregations().asList().get(0)).getBucketsInternal();
List<Integer> ids = longTerms.stream().map(e -> e.getKeyAsNumber().intValue()).collect(Collectors.toList());
if (!CollectionUtils.isEmpty(ids)) {
return promotionIndexBaseService.getPromotionInfosByIds(ISearchConstants.INDEX_NAME_PROMOTIONINDEX, ids);
}
if(aggregation.getAggregations().asList().isEmpty()){
return new ArrayList<RecommendPromotionAggVO>();
}
InternalFilter filter = (InternalFilter)aggregation.getAggregations().asList().get(0);
List<LongTerms.Bucket> longTerms = ((LongTerms)filter.getAggregations().asList().get(0)).getBucketsInternal();
List<Integer> ids = longTerms.stream().map(e -> e.getKeyAsNumber().intValue()).collect(Collectors.toList());
if(ids==null || ids.isEmpty()){
return new ArrayList<RecommendPromotionAggVO>();
}
return null;
return promotionIndexBaseService.getPromotionInfosByIds(ISearchConstants.INDEX_NAME_PROMOTIONINDEX, ids);
}
}
... ...
... ... @@ -66,9 +66,10 @@ public class PromotionIndexBaseService {
return Collections.emptyList();
}
List<RecommendPromotionAggVO> resultList = new ArrayList<>();
long total = searchResult.getTotal();
for (Map<String, Object> esMap : searchResult.getResultList()) {
if (!CollectionUtils.isEmpty(esMap)) {
resultList.add(parsePromotionInfos(esMap));
resultList.add(parsePromotionInfos(esMap,total));
}
}
return resultList;
... ... @@ -103,11 +104,12 @@ public class PromotionIndexBaseService {
return map;
}
private RecommendPromotionAggVO parsePromotionInfos(Map<String, Object> esMap) {
private RecommendPromotionAggVO parsePromotionInfos(Map<String, Object> esMap,long total) {
RecommendPromotionAggVO recommendPromotionAggVO = new RecommendPromotionAggVO();
recommendPromotionAggVO.setPromotionInfoId(MapUtils.getIntValue(esMap, "id", 0));
recommendPromotionAggVO.setPromotion_info_id(MapUtils.getIntValue(esMap, "id", 0));
recommendPromotionAggVO.setTitle(MapUtils.getString(esMap,"title", ""));
recommendPromotionAggVO.setCoverImgUrl(MapUtils.getString(esMap, "commonBanner", ""));
recommendPromotionAggVO.setCoverimg_url(MapUtils.getString(esMap, "commonBanner", ""));
recommendPromotionAggVO.setTotal_count(total);
return recommendPromotionAggVO;
}
... ...
package com.yoho.search.service.scene;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
... ... @@ -88,7 +87,7 @@ public class FuzzySceneService extends AbstractSceneService {
// 4、获取自定义标签聚合结果
CompletableFuture<SearchApiResult> customizeTagFuture = CompletableFuture.supplyAsync(() -> sceneAggregationsHelper.sceneAggCustomizeTag(this.newParamMap(paramMap)), executor);
// 5、获取促销专题
CompletableFuture<SearchApiResult> recommendPromotionFuture = CompletableFuture.supplyAsync(() -> sceneAggregationsHelper.sceneAggRecommendPromotion(this.newParamMap(paramMap)),
CompletableFuture<SearchApiResult> promotionsFuture = CompletableFuture.supplyAsync(() -> sceneAggregationsHelper.sceneAggRecommendPromotion(this.newParamMap(paramMap)),
executor);
// 6、加入建议词
... ... @@ -103,14 +102,10 @@ public class FuzzySceneService extends AbstractSceneService {
}
// 8、组合结果
SearchApiResult customizeTags = customizeTagFuture.get();
SearchApiResult recommendProducts = recommendPromotionFuture.get();
SearchApiResult promotions = promotionsFuture.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 {
dataMap.put(RECOMMEND_PROMOTION_LIST, subList(recommendProducts.getData(), StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page")), 1));
}
dataMap.put(RECOMMEND_PROMOTION_LIST, sceneAggregationsHelper.subRecommendPromotions(promotions.getData(),this.getPage(paramMap),1));
return productListResult;
} catch (Exception e) {
logger.error(e.getMessage(), e);
... ...
package com.yoho.search.service.scene;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
... ... @@ -101,22 +100,16 @@ public class SortSceneService extends AbstractSceneService {
// 3、获取聚合结果
CompletableFuture<SearchApiResult> standardsFuture = CompletableFuture.supplyAsync(() -> sceneAggregationsHelper.sceneAggStandard(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);
CompletableFuture<SearchApiResult> promotionsFuture = CompletableFuture.supplyAsync(() -> sceneAggregationsHelper.sceneAggRecommendPromotion(this.newParamMap(paramMap)),executorService);
// 4、组合结果
SearchApiResult productList = productListFuture.get();
SearchApiResult standards = standardsFuture.get();
SearchApiResult customizeTags = customizeTagFuture.get();
SearchApiResult recommendProducts = recommendProductFuture.get();
SearchApiResult promotions = promotionsFuture.get();
JSONObject jsonObject = (JSONObject) productList.getData();
jsonObject.put("standard", standards.getData());
jsonObject.put(STANDARD, standards.getData());
jsonObject.put(CUSTOMIZE_TAG_LIST, customizeTags.getData());
if (recommendProducts.getData() == null) {
jsonObject.put(RECOMMEND_PROMOTION_LIST, Collections.EMPTY_LIST);
} else {
jsonObject.put(RECOMMEND_PROMOTION_LIST, subList(recommendProducts.getData(), StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page")), 1));
}
jsonObject.put(RECOMMEND_PROMOTION_LIST, sceneAggregationsHelper.subRecommendPromotions(promotions.getData(),this.getPage(paramMap),1));
return productList;
} catch (Exception e) {
logger.error(e.getMessage(), e);
... ...
package com.yoho.search.service.scene.aggregations;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.elasticsearch.search.aggregations.Aggregation;
... ... @@ -66,12 +69,12 @@ 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 sceneAggRecommendPromotion(Map<String, String> paramMap) {
try {
// 1、获取aggregation
IAggregation aggregation = aggregationFactoryService.getRecommendPromotionAggregation(100);
IAggregation aggregation = aggregationFactoryService.getRecommendPromotionAggregation(50);
Object respone = this.getAggregationResponse(aggregation, paramMap);
return new SearchApiResult().setData(respone);
} catch (Exception e) {
... ... @@ -80,4 +83,27 @@ public class SceneAggregationsHelper {
}
}
public List<?> subRecommendPromotions(Object recommendPromotions, int page, int count) {
try {
if (recommendPromotions == null || !(recommendPromotions instanceof List)) {
return new ArrayList<Object>();
}
List<?> recommendPromotionList = (List<?>) recommendPromotions;
int totalCount = recommendPromotionList.size();
int start = (page - 1) * count;
int end = start + count;
if (start >= totalCount) {
return Collections.emptyList();
}
if (end > totalCount) {
end = totalCount;
}
return recommendPromotionList.subList(start, end);
} catch (Exception e) {
logger.error(e.getMessage(), e);
return new ArrayList<Object>();
}
}
}
... ...
package com.yoho.search.service.scene.common;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yoho.search.common.utils.SearchKeyWordUtils;
import com.yoho.search.models.SearchApiResult;
import com.yoho.search.service.base.SearchRequestParams;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.function.Supplier;
import com.yoho.search.common.utils.SearchKeyWordUtils;
import com.yoho.search.models.SearchApiResult;
import com.yoho.search.service.base.SearchRequestParams;
public abstract class AbstractSceneService {
protected static final String RECOMMEND_PROMOTION_LIST = "recommend_promotion_list";
protected static final String CUSTOMIZE_TAG_LIST = "customize_tag";
protected Map<String, String> newParamMap(Map<String, String> paramMap) {
protected static final String CUSTOMIZE_TAG_LIST = "customize_tag";
protected static final String STANDARD = "standard";
protected Map<String, String> newParamMap(Map<String, String> paramMap) {
return new HashMap<String, String>(paramMap);
}
protected CompletableFuture<?> getFuture(Supplier<?> supplier,Executor executor){
return CompletableFuture.supplyAsync(() -> supplier,executor);
}
/**
* 场景化的默认参数
*
... ... @@ -42,45 +35,19 @@ public abstract class AbstractSceneService {
paramMap.put(SearchRequestParams.PARAM_SEARCH_ATTRIBUTE_NOT, "2");// 非赠品
paramMap.put(SearchRequestParams.PARAM_SEARCH_NEEDSMALLSORT, "1");// 品类聚合时带上小分类
paramMap.put(SearchRequestParams.PARAM_SEARCH_AGG_WITH_PARAM_BRAND, "Y");// 聚合时使用参数中自带的参数
//关键词反转码
String keyword = SearchKeyWordUtils.getParamKeyword(paramMap, SearchRequestParams.PARAM_SEARCH_QUERY);//转码
if(!StringUtils.isBlank(keyword)){
// 关键词反转码
String keyword = SearchKeyWordUtils.getParamKeyword(paramMap, SearchRequestParams.PARAM_SEARCH_QUERY);// 转码
if (!StringUtils.isBlank(keyword)) {
paramMap.put(SearchRequestParams.PARAM_SEARCH_QUERY, keyword);
paramMap.put("is_encode", "0");
}
}
protected int getPage(Map<String, String> paramMap) {
int page = StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page"));
return page;
}
protected List<Object> subList(Object source, int page, int count) {
try {
JSONArray sourceList = (JSONArray)source;
if (sourceList != null && !sourceList.isEmpty()) {
int totalCount = sourceList.size();
int start = (page - 1) * count;
int end = start + count;
if (start >= totalCount) {
return Collections.emptyList();
}
if (end > totalCount) {
end = totalCount;
}
List<Object> subList = sourceList.subList(start, end);
subList.forEach(e -> {
JSONObject jsonObject = (JSONObject)e;
jsonObject.put("totalCount", totalCount);
});
return subList;
}
} catch (Exception e) {
return Collections.emptyList();
}
return Collections.emptyList();
}
protected String getuid(Map<String, String> paramMap) {
return MapUtils.getString(paramMap, "uid", "0");
}
... ...