001/*
002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     bdelbosc
016 */
017package org.nuxeo.elasticsearch.aggregate;
018
019import static org.nuxeo.elasticsearch.ElasticSearchConstants.AGG_MIN_DOC_COUNT_PROP;
020import static org.nuxeo.elasticsearch.ElasticSearchConstants.AGG_SIZE_PROP;
021
022import java.util.ArrayList;
023import java.util.Collection;
024import java.util.List;
025import java.util.Map;
026
027import org.codehaus.jackson.annotate.JsonIgnore;
028import org.elasticsearch.index.query.FilterBuilders;
029import org.elasticsearch.index.query.TermsFilterBuilder;
030import org.elasticsearch.search.aggregations.AggregationBuilders;
031import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation;
032import org.elasticsearch.search.aggregations.bucket.significant.SignificantTermsBuilder;
033import org.nuxeo.ecm.core.api.DocumentModel;
034import org.nuxeo.ecm.platform.query.api.AggregateDefinition;
035import org.nuxeo.ecm.platform.query.core.BucketTerm;
036
037/**
038 * @since 6.0
039 */
040public class SignificantTermAggregate extends AggregateEsBase<BucketTerm> {
041
042    public SignificantTermAggregate(AggregateDefinition definition, DocumentModel searchDocument) {
043        super(definition, searchDocument);
044    }
045
046    @JsonIgnore
047    @Override
048    public SignificantTermsBuilder getEsAggregate() {
049        SignificantTermsBuilder ret = AggregationBuilders.significantTerms(getId()).field(getField());
050        Map<String, String> props = getProperties();
051        if (props.containsKey(AGG_SIZE_PROP)) {
052            ret.size(Integer.parseInt(props.get(AGG_SIZE_PROP)));
053        }
054        if (props.containsKey(AGG_MIN_DOC_COUNT_PROP)) {
055            ret.minDocCount(Integer.parseInt(props.get(AGG_MIN_DOC_COUNT_PROP)));
056        }
057        return ret;
058    }
059
060    @JsonIgnore
061    @Override
062    public TermsFilterBuilder getEsFilter() {
063        if (getSelection().isEmpty()) {
064            return null;
065        }
066        return FilterBuilders.termsFilter(getField(), getSelection());
067    }
068
069    @JsonIgnore
070    @Override
071    public void parseEsBuckets(Collection<? extends MultiBucketsAggregation.Bucket> buckets) {
072        List<BucketTerm> nxBuckets = new ArrayList<>(buckets.size());
073        for (MultiBucketsAggregation.Bucket bucket : buckets) {
074            nxBuckets.add(new BucketTerm(bucket.getKey(), bucket.getDocCount()));
075        }
076        this.buckets = nxBuckets;
077    }
078
079}