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.util.ArrayList;
024import java.util.Collection;
025import java.util.Comparator;
026import java.util.List;
027import java.util.Map;
028
029import org.codehaus.jackson.annotate.JsonIgnore;
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.Range;
037import org.elasticsearch.search.aggregations.bucket.range.date.DateRangeAggregationBuilder;
038import org.joda.time.DateTime;
039import org.nuxeo.ecm.core.api.DocumentModel;
040import org.nuxeo.ecm.platform.query.api.AggregateDefinition;
041import org.nuxeo.ecm.platform.query.api.AggregateRangeDateDefinition;
042import org.nuxeo.ecm.platform.query.core.BucketRangeDate;
043
044/**
045 * @since 6.0
046 */
047public class DateRangeAggregate extends AggregateEsBase<BucketRangeDate> {
048
049    public DateRangeAggregate(AggregateDefinition definition, DocumentModel searchDocument) {
050        super(definition, searchDocument);
051    }
052
053    @JsonIgnore
054    @Override
055    public DateRangeAggregationBuilder getEsAggregate() {
056        DateRangeAggregationBuilder ret = AggregationBuilders.dateRange(getId()).field(getField());
057        for (AggregateRangeDateDefinition range : getDateRanges()) {
058            if (range.getFromAsString() != null) {
059                if (range.getToAsString() != null) {
060                    ret.addRange(range.getKey(), range.getFromAsString(), range.getToAsString());
061                } else {
062                    ret.addUnboundedFrom(range.getKey(), range.getFromAsString());
063                }
064            } else if (range.getToAsString() != null) {
065                ret.addUnboundedTo(range.getKey(), range.getToAsString());
066            }
067        }
068        Map<String, String> props = getProperties();
069        if (props.containsKey(AGG_FORMAT_PROP)) {
070            ret.format(props.get(AGG_FORMAT_PROP));
071        }
072        return ret;
073    }
074
075    @JsonIgnore
076    @Override
077    public QueryBuilder getEsFilter() {
078        if (getSelection().isEmpty()) {
079            return null;
080        }
081        BoolQueryBuilder ret = QueryBuilders.boolQuery();
082        for (AggregateRangeDateDefinition range : getDateRanges()) {
083            if (getSelection().contains(range.getKey())) {
084                RangeQueryBuilder rangeFilter = QueryBuilders.rangeQuery(getField());
085                if (range.getFromAsString() != null) {
086                    rangeFilter.gte(range.getFromAsString());
087                }
088                if (range.getToAsString() != null) {
089                    rangeFilter.lt(range.getToAsString());
090                }
091                ret.should(rangeFilter);
092            }
093        }
094        return ret;
095    }
096
097    @JsonIgnore
098    @Override
099    public void parseEsBuckets(Collection<? extends MultiBucketsAggregation.Bucket> buckets) {
100        List<BucketRangeDate> nxBuckets = new ArrayList<>(buckets.size());
101        for (MultiBucketsAggregation.Bucket bucket : buckets) {
102            Range.Bucket rangeBucket = (Range.Bucket) bucket;
103            nxBuckets.add(new BucketRangeDate(bucket.getKeyAsString(), (DateTime) rangeBucket.getFrom(),
104                    (DateTime) rangeBucket.getTo(), rangeBucket.getDocCount()));
105        }
106        nxBuckets.sort(new BucketRangeDateComparator());
107        this.buckets = nxBuckets;
108    }
109
110    protected class BucketRangeDateComparator implements Comparator<BucketRangeDate> {
111        @Override
112        public int compare(BucketRangeDate arg0, BucketRangeDate arg1) {
113            return definition.getAggregateDateRangeDefinitionOrderMap().get(arg0.getKey()).compareTo(
114                    definition.getAggregateDateRangeDefinitionOrderMap().get(arg1.getKey()));
115        }
116    }
117
118}