Authored by zhaojun2

fix promotion

@@ -105,6 +105,10 @@ public class AggregationFactoryService { @@ -105,6 +105,10 @@ public class AggregationFactoryService {
105 return new RecommendPromotionAggregation(promotionIndexBaseService, promotionCount); 105 return new RecommendPromotionAggregation(promotionIndexBaseService, promotionCount);
106 } 106 }
107 107
  108 + public IAggregation getPromotionAggregation(int promotionCount) {
  109 + return new PromotionAggregation(promotionCount);
  110 + }
  111 +
108 public IAggregation getRecentShelveDayAggregation() { 112 public IAggregation getRecentShelveDayAggregation() {
109 return new RecentShelveDayAggregation(); 113 return new RecentShelveDayAggregation();
110 } 114 }
  1 +package com.yoho.search.service.aggregations.impls;
  2 +
  3 +import com.yoho.search.base.utils.DateUtil;
  4 +import com.yoho.search.base.utils.ProductIndexEsField;
  5 +import com.yoho.search.core.es.agg.AbstractAggregation;
  6 +import com.yoho.search.models.RecommendPromotionAggVO;
  7 +import org.elasticsearch.index.query.BoolQueryBuilder;
  8 +import org.elasticsearch.index.query.QueryBuilders;
  9 +import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
  10 +import org.elasticsearch.search.aggregations.Aggregation;
  11 +import org.elasticsearch.search.aggregations.AggregationBuilders;
  12 +import org.elasticsearch.search.aggregations.bucket.filter.InternalFilter;
  13 +import org.elasticsearch.search.aggregations.bucket.nested.InternalNested;
  14 +import org.elasticsearch.search.aggregations.bucket.nested.NestedAggregationBuilder;
  15 +import org.elasticsearch.search.aggregations.bucket.terms.LongTerms;
  16 +import org.elasticsearch.search.aggregations.bucket.terms.Terms;
  17 +
  18 +import java.util.*;
  19 +import java.util.stream.Collectors;
  20 +
  21 +public class PromotionAggregation extends AbstractAggregation {
  22 + private static final String TERM_AGGREGATION_NAME = "promotionIdAgg";
  23 + private int promotionCount;
  24 +
  25 + public PromotionAggregation(int promotionCount) {
  26 + this.promotionCount = promotionCount;
  27 + }
  28 +
  29 + public int getPromotionCount() {
  30 + return promotionCount;
  31 + }
  32 +
  33 + public void setPromotionCount(int promotionCount) {
  34 + this.promotionCount = promotionCount;
  35 + }
  36 +
  37 + @Override
  38 + public String aggName() {
  39 + return "promotionAgg";
  40 + }
  41 +
  42 + @Override
  43 + public String filterName() {
  44 + return "cxfilter";
  45 + }
  46 +
  47 + @Override
  48 + public AbstractAggregationBuilder<?> getBuilder() {
  49 + BoolQueryBuilder boolFilter = QueryBuilders.boolQuery();
  50 + Date now = new Date();
  51 + boolFilter.must(QueryBuilders.rangeQuery(ProductIndexEsField.matchedPromotionsEndTime).gt(DateUtil.setHourSeonds(now, 1, 0, 0)));
  52 + boolFilter.must(QueryBuilders.rangeQuery(ProductIndexEsField.matchedPromotionsStartTime).lt(DateUtil.setHourSeonds(now, 0, 0, 0)));
  53 + boolFilter.mustNot(QueryBuilders.termsQuery(ProductIndexEsField.matchedPromotionsType, Arrays.asList("Needpaygift")));
  54 +
  55 + AbstractAggregationBuilder<NestedAggregationBuilder> nestedAggregationBuilder =
  56 + AggregationBuilders.nested(aggName(), ProductIndexEsField.matchedPromotions)
  57 + .subAggregation(AggregationBuilders.filter(filterName(), boolFilter)
  58 + .subAggregation(AggregationBuilders.terms(TERM_AGGREGATION_NAME).field(ProductIndexEsField.matchedPromotionsId).size(getPromotionCount()).order(Terms.Order.aggregation("priority", false))
  59 + .subAggregation(AggregationBuilders.max("priority").field("matchedPromotions.priority"))));
  60 +
  61 +
  62 + return nestedAggregationBuilder;
  63 + }
  64 +
  65 + @Override
  66 + public Object getAggregationResponseMap(Map<String, Aggregation> aggMaps) {
  67 + InternalNested aggregation = (InternalNested) aggMaps.get(aggName());
  68 + if(aggregation.getAggregations().asList().isEmpty()){
  69 + return new ArrayList<RecommendPromotionAggVO>();
  70 + }
  71 + InternalFilter filter = (InternalFilter)aggregation.getAggregations().asList().get(0);
  72 + List<LongTerms.Bucket> longTerms = ((LongTerms)filter.getAggregations().asList().get(0)).getBucketsInternal();
  73 + List<Integer> ids = longTerms.stream().map(e -> e.getKeyAsNumber().intValue()).collect(Collectors.toList());
  74 + if(ids==null || ids.isEmpty()){
  75 + return null;
  76 + }
  77 + return ids;
  78 + }
  79 +}
@@ -121,7 +121,7 @@ public class SceneAggregationsHelper { @@ -121,7 +121,7 @@ public class SceneAggregationsHelper {
121 @SearchCacheAble(cacheName = "AGG_PROMOTION", cacheInMinute = 15, excludeParams = { "uid", "udid", "order", "page", "viewNum", "yh_channel"}) 121 @SearchCacheAble(cacheName = "AGG_PROMOTION", cacheInMinute = 15, excludeParams = { "uid", "udid", "order", "page", "viewNum", "yh_channel"})
122 public List<RecommendPromotionAggVO> aggPromotion(Map<String, String> paramMap) { 122 public List<RecommendPromotionAggVO> aggPromotion(Map<String, String> paramMap) {
123 try { 123 try {
124 - IAggregation aggregation = aggregationFactoryService.getRecommendPromotionAggregation(50); 124 + IAggregation aggregation = aggregationFactoryService.getPromotionAggregation(50);
125 Object ids = this.getAggregationResponse(aggregation, paramMap); 125 Object ids = this.getAggregationResponse(aggregation, paramMap);
126 if(ids == null){ 126 if(ids == null){
127 return Collections.emptyList(); 127 return Collections.emptyList();