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_MIN_DOC_COUNT_PROP;
022import static org.nuxeo.elasticsearch.ElasticSearchConstants.AGG_SIZE_PROP;
023
024import java.util.ArrayList;
025import java.util.Collection;
026import java.util.List;
027import java.util.Map;
028
029import org.codehaus.jackson.annotate.JsonIgnore;
030import org.elasticsearch.index.query.QueryBuilder;
031import org.elasticsearch.index.query.QueryBuilders;
032import org.elasticsearch.search.aggregations.AggregationBuilders;
033import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation;
034import org.elasticsearch.search.aggregations.bucket.significant.SignificantTermsBuilder;
035import org.nuxeo.ecm.core.api.DocumentModel;
036import org.nuxeo.ecm.platform.query.api.AggregateDefinition;
037import org.nuxeo.ecm.platform.query.core.BucketTerm;
038
039/**
040 * @since 6.0
041 */
042public class SignificantTermAggregate extends AggregateEsBase<BucketTerm> {
043
044    public SignificantTermAggregate(AggregateDefinition definition, DocumentModel searchDocument) {
045        super(definition, searchDocument);
046    }
047
048    @JsonIgnore
049    @Override
050    public SignificantTermsBuilder getEsAggregate() {
051        SignificantTermsBuilder ret = AggregationBuilders.significantTerms(getId()).field(getField());
052        Map<String, String> props = getProperties();
053        if (props.containsKey(AGG_SIZE_PROP)) {
054            ret.size(Integer.parseInt(props.get(AGG_SIZE_PROP)));
055        }
056        if (props.containsKey(AGG_MIN_DOC_COUNT_PROP)) {
057            ret.minDocCount(Integer.parseInt(props.get(AGG_MIN_DOC_COUNT_PROP)));
058        }
059        return ret;
060    }
061
062    @JsonIgnore
063    @Override
064    public QueryBuilder getEsFilter() {
065        if (getSelection().isEmpty()) {
066            return null;
067        }
068        return QueryBuilders.termsQuery(getField(), getSelection());
069    }
070
071    @JsonIgnore
072    @Override
073    public void parseEsBuckets(Collection<? extends MultiBucketsAggregation.Bucket> buckets) {
074        List<BucketTerm> nxBuckets = new ArrayList<>(buckets.size());
075        for (MultiBucketsAggregation.Bucket bucket : buckets) {
076            nxBuckets.add(new BucketTerm(bucket.getKeyAsString(), bucket.getDocCount()));
077        }
078        this.buckets = nxBuckets;
079    }
080
081}