001/*
002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bdelbosc
018 */
019package org.nuxeo.elasticsearch.aggregate;
020
021import static org.nuxeo.elasticsearch.ElasticSearchConstants.AGG_FORMAT_PROP;
022
023import java.time.ZonedDateTime;
024import java.util.ArrayList;
025import java.util.Collection;
026import java.util.Comparator;
027import java.util.List;
028import java.util.Map;
029
030import org.elasticsearch.index.query.BoolQueryBuilder;
031import org.elasticsearch.index.query.QueryBuilder;
032import org.elasticsearch.index.query.QueryBuilders;
033import org.elasticsearch.index.query.RangeQueryBuilder;
034import org.elasticsearch.search.aggregations.AggregationBuilders;
035import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation;
036import org.elasticsearch.search.aggregations.bucket.range.DateRangeAggregationBuilder;
037import org.elasticsearch.search.aggregations.bucket.range.Range;
038import org.nuxeo.ecm.core.api.DocumentModel;
039import org.nuxeo.ecm.platform.query.api.AggregateDefinition;
040import org.nuxeo.ecm.platform.query.api.AggregateRangeDateDefinition;
041import org.nuxeo.ecm.platform.query.core.BucketRangeDate;
042
043import com.fasterxml.jackson.annotation.JsonIgnore;
044
045/**
046 * @since 6.0
047 */
048public class DateRangeAggregate extends MultiBucketAggregate<BucketRangeDate> {
049
050    public DateRangeAggregate(AggregateDefinition definition, DocumentModel searchDocument) {
051        super(definition, searchDocument);
052    }
053
054    @JsonIgnore
055    @Override
056    public DateRangeAggregationBuilder getEsAggregate() {
057        DateRangeAggregationBuilder ret = AggregationBuilders.dateRange(getId()).field(getField());
058        for (AggregateRangeDateDefinition range : getDateRanges()) {
059            if (range.getFromAsString() != null) {
060                if (range.getToAsString() != null) {
061                    ret.addRange(range.getKey(), range.getFromAsString(), range.getToAsString());
062                } else {
063                    ret.addUnboundedFrom(range.getKey(), range.getFromAsString());
064                }
065            } else if (range.getToAsString() != null) {
066                ret.addUnboundedTo(range.getKey(), range.getToAsString());
067            }
068        }
069        Map<String, String> props = getProperties();
070        if (props.containsKey(AGG_FORMAT_PROP)) {
071            ret.format(props.get(AGG_FORMAT_PROP));
072        }
073        return ret;
074    }
075
076    @JsonIgnore
077    @Override
078    public QueryBuilder getEsFilter() {
079        if (getSelection().isEmpty()) {
080            return null;
081        }
082        BoolQueryBuilder ret = QueryBuilders.boolQuery();
083        for (AggregateRangeDateDefinition range : getDateRanges()) {
084            if (getSelection().contains(range.getKey())) {
085                RangeQueryBuilder rangeFilter = QueryBuilders.rangeQuery(getField());
086                if (range.getFromAsString() != null) {
087                    rangeFilter.gte(range.getFromAsString());
088                }
089                if (range.getToAsString() != null) {
090                    rangeFilter.lt(range.getToAsString());
091                }
092                ret.should(rangeFilter);
093            }
094        }
095        return ret;
096    }
097
098    @JsonIgnore
099    @Override
100    public void parseEsBuckets(Collection<? extends MultiBucketsAggregation.Bucket> buckets) {
101        List<BucketRangeDate> nxBuckets = new ArrayList<>(buckets.size());
102        for (MultiBucketsAggregation.Bucket bucket : buckets) {
103            Range.Bucket rangeBucket = (Range.Bucket) bucket;
104            ZonedDateTime fromZDT = (ZonedDateTime) rangeBucket.getFrom();
105            ZonedDateTime toZDT = (ZonedDateTime) rangeBucket.getTo();
106            nxBuckets.add(new BucketRangeDate(bucket.getKeyAsString(), fromZDT, toZDT, rangeBucket.getDocCount()));
107        }
108        nxBuckets.sort(new BucketRangeDateComparator());
109        this.buckets = nxBuckets;
110    }
111
112    protected class BucketRangeDateComparator implements Comparator<BucketRangeDate> {
113        @Override
114        public int compare(BucketRangeDate arg0, BucketRangeDate arg1) {
115            return definition.getAggregateDateRangeDefinitionOrderMap()
116                             .get(arg0.getKey())
117                             .compareTo(definition.getAggregateDateRangeDefinitionOrderMap().get(arg1.getKey()));
118        }
119    }
120
121}