|
|
package com.yoho.search.service.aggregations.impls;
|
|
|
|
|
|
import com.yoho.search.base.utils.DateUtil;
|
|
|
import com.yoho.search.base.utils.ProductIndexEsField;
|
|
|
import com.yoho.search.core.es.agg.AbstractAggregation;
|
|
|
import com.yoho.search.models.RecommendPromotionAggVO;
|
|
|
import org.elasticsearch.index.query.BoolQueryBuilder;
|
|
|
import org.elasticsearch.index.query.QueryBuilders;
|
|
|
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
|
|
|
import org.elasticsearch.search.aggregations.Aggregation;
|
|
|
import org.elasticsearch.search.aggregations.AggregationBuilders;
|
|
|
import org.elasticsearch.search.aggregations.bucket.filter.InternalFilter;
|
|
|
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 java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
public class PromotionAggregation extends AbstractAggregation {
|
|
|
private static final String TERM_AGGREGATION_NAME = "promotionIdAgg";
|
|
|
private int promotionCount;
|
|
|
|
|
|
public PromotionAggregation(int promotionCount) {
|
|
|
this.promotionCount = promotionCount;
|
|
|
}
|
|
|
|
|
|
public int getPromotionCount() {
|
|
|
return promotionCount;
|
|
|
}
|
|
|
|
|
|
public void setPromotionCount(int promotionCount) {
|
|
|
this.promotionCount = promotionCount;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public String aggName() {
|
|
|
return "promotionAgg";
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public String filterName() {
|
|
|
return "cxfilter";
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public AbstractAggregationBuilder<?> getBuilder() {
|
|
|
BoolQueryBuilder boolFilter = QueryBuilders.boolQuery();
|
|
|
Date now = new Date();
|
|
|
boolFilter.must(QueryBuilders.rangeQuery(ProductIndexEsField.matchedPromotionsEndTime).gt(DateUtil.setHourSeonds(now, 1, 0, 0)));
|
|
|
boolFilter.must(QueryBuilders.rangeQuery(ProductIndexEsField.matchedPromotionsStartTime).lt(DateUtil.setHourSeonds(now, 0, 0, 0)));
|
|
|
boolFilter.mustNot(QueryBuilders.termsQuery(ProductIndexEsField.matchedPromotionsType, Arrays.asList("Needpaygift")));
|
|
|
|
|
|
AbstractAggregationBuilder<NestedAggregationBuilder> nestedAggregationBuilder =
|
|
|
AggregationBuilders.nested(aggName(), ProductIndexEsField.matchedPromotions)
|
|
|
.subAggregation(AggregationBuilders.filter(filterName(), boolFilter)
|
|
|
.subAggregation(AggregationBuilders.terms(TERM_AGGREGATION_NAME).field(ProductIndexEsField.matchedPromotionsId).size(getPromotionCount()).order(Terms.Order.aggregation("priority", false))
|
|
|
.subAggregation(AggregationBuilders.max("priority").field("matchedPromotions.priority"))));
|
|
|
|
|
|
|
|
|
return nestedAggregationBuilder;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public Object getAggregationResponseMap(Map<String, Aggregation> aggMaps) {
|
|
|
InternalNested aggregation = (InternalNested) aggMaps.get(aggName());
|
|
|
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 null;
|
|
|
}
|
|
|
return ids;
|
|
|
}
|
|
|
} |
...
|
...
|
|