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_EXCLUDE_PROP;
022import static org.nuxeo.elasticsearch.ElasticSearchConstants.AGG_INCLUDE_PROP;
023import static org.nuxeo.elasticsearch.ElasticSearchConstants.AGG_MIN_DOC_COUNT_PROP;
024import static org.nuxeo.elasticsearch.ElasticSearchConstants.AGG_ORDER_COUNT_ASC;
025import static org.nuxeo.elasticsearch.ElasticSearchConstants.AGG_ORDER_COUNT_DESC;
026import static org.nuxeo.elasticsearch.ElasticSearchConstants.AGG_ORDER_PROP;
027import static org.nuxeo.elasticsearch.ElasticSearchConstants.AGG_ORDER_TERM_ASC;
028import static org.nuxeo.elasticsearch.ElasticSearchConstants.AGG_ORDER_TERM_DESC;
029import static org.nuxeo.elasticsearch.ElasticSearchConstants.AGG_SIZE_PROP;
030
031import java.util.ArrayList;
032import java.util.Collection;
033import java.util.List;
034import java.util.Map;
035
036import org.codehaus.jackson.annotate.JsonIgnore;
037import org.elasticsearch.index.query.FilterBuilders;
038import org.elasticsearch.index.query.TermsFilterBuilder;
039import org.elasticsearch.search.aggregations.AggregationBuilders;
040import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation;
041import org.elasticsearch.search.aggregations.bucket.terms.Terms;
042import org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder;
043import org.nuxeo.ecm.core.api.DocumentModel;
044import org.nuxeo.ecm.platform.query.api.AggregateDefinition;
045import org.nuxeo.ecm.platform.query.core.BucketTerm;
046
047/**
048 * @since 6.0
049 */
050public class TermAggregate extends AggregateEsBase<BucketTerm> {
051
052    public TermAggregate(AggregateDefinition definition, DocumentModel searchDocument) {
053        super(definition, searchDocument);
054    }
055
056    @JsonIgnore
057    @Override
058    public TermsBuilder getEsAggregate() {
059        TermsBuilder ret = AggregationBuilders.terms(getId()).field(getField());
060        Map<String, String> props = getProperties();
061        if (props.containsKey(AGG_SIZE_PROP)) {
062            ret.size(Integer.parseInt(props.get(AGG_SIZE_PROP)));
063        }
064        if (props.containsKey(AGG_MIN_DOC_COUNT_PROP)) {
065            ret.minDocCount(Long.parseLong(props.get(AGG_MIN_DOC_COUNT_PROP)));
066        }
067        if (props.containsKey(AGG_EXCLUDE_PROP)) {
068            ret.exclude(props.get(AGG_EXCLUDE_PROP));
069        }
070        if (props.containsKey(AGG_INCLUDE_PROP)) {
071            ret.include(props.get(AGG_INCLUDE_PROP));
072        }
073        if (props.containsKey(AGG_ORDER_PROP)) {
074            switch (props.get(AGG_ORDER_PROP).toLowerCase()) {
075            case AGG_ORDER_COUNT_DESC:
076                ret.order(Terms.Order.count(false));
077                break;
078            case AGG_ORDER_COUNT_ASC:
079                ret.order(Terms.Order.count(true));
080                break;
081            case AGG_ORDER_TERM_DESC:
082                ret.order(Terms.Order.term(false));
083                break;
084            case AGG_ORDER_TERM_ASC:
085                ret.order(Terms.Order.term(true));
086                break;
087            default:
088                throw new IllegalArgumentException("Invalid order: " + props.get(AGG_ORDER_PROP));
089            }
090        }
091        return ret;
092    }
093
094    @JsonIgnore
095    @Override
096    public TermsFilterBuilder getEsFilter() {
097        if (getSelection().isEmpty()) {
098            return null;
099        }
100        return FilterBuilders.termsFilter(getField(), getSelection());
101    }
102
103    @JsonIgnore
104    @Override
105    public void parseEsBuckets(Collection<? extends MultiBucketsAggregation.Bucket> buckets) {
106        List<BucketTerm> nxBuckets = new ArrayList<>(buckets.size());
107        for (MultiBucketsAggregation.Bucket bucket : buckets) {
108            nxBuckets.add(new BucketTerm(bucket.getKey(), bucket.getDocCount()));
109        }
110        this.buckets = nxBuckets;
111    }
112}