001/*
002 * (C) Copyright 2018 Nuxeo (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 *     Gethin James
018 */
019package org.nuxeo.elasticsearch.aggregate;
020
021import static org.nuxeo.elasticsearch.ElasticSearchConstants.AGG_AVG;
022import static org.nuxeo.elasticsearch.ElasticSearchConstants.AGG_CARDINALITY;
023import static org.nuxeo.elasticsearch.ElasticSearchConstants.AGG_COUNT;
024import static org.nuxeo.elasticsearch.ElasticSearchConstants.AGG_MAX;
025import static org.nuxeo.elasticsearch.ElasticSearchConstants.AGG_MIN;
026import static org.nuxeo.elasticsearch.ElasticSearchConstants.AGG_SUM;
027
028import java.util.Collections;
029
030import org.elasticsearch.index.query.QueryBuilder;
031import org.elasticsearch.index.query.QueryBuilders;
032import org.elasticsearch.search.aggregations.AggregationBuilder;
033import org.elasticsearch.search.aggregations.AggregationBuilders;
034import org.elasticsearch.search.aggregations.metrics.NumericMetricsAggregation;
035import org.nuxeo.ecm.core.api.DocumentModel;
036import org.nuxeo.ecm.platform.query.api.AggregateDefinition;
037import org.nuxeo.ecm.platform.query.core.BucketTerm;
038
039import com.fasterxml.jackson.annotation.JsonIgnore;
040
041/**
042 * An aggregate that returns a single value.
043 *
044 * @since 10.3
045 */
046public class SingleValueMetricAggregate extends AggregateEsBase<NumericMetricsAggregation.SingleValue, BucketTerm> {
047
048    protected final AggregationBuilder aggregationBuilder;
049
050    protected Double value;
051
052    public SingleValueMetricAggregate(AggregateDefinition definition, DocumentModel searchDocument) {
053        super(definition, searchDocument);
054        this.aggregationBuilder = toBuilder(definition.getType());
055    }
056
057    /**
058     * Creates an AggregationBuilder for the supplied type
059     */
060    public AggregationBuilder toBuilder(String type) {
061        switch (type) {
062        case AGG_CARDINALITY:
063            return AggregationBuilders.cardinality(getId()).field(getField());
064        case AGG_COUNT:
065            return AggregationBuilders.count(getId()).field(getField());
066        case AGG_SUM:
067            return AggregationBuilders.sum(getId()).field(getField());
068        case AGG_AVG:
069            return AggregationBuilders.avg(getId()).field(getField());
070        case AGG_MAX:
071            return AggregationBuilders.max(getId()).field(getField());
072        case AGG_MIN:
073            return AggregationBuilders.min(getId()).field(getField());
074        default:
075            throw new IllegalArgumentException("Unknown aggregate type: " + type);
076        }
077    }
078
079    @JsonIgnore
080    @Override
081    public AggregationBuilder getEsAggregate() {
082        return aggregationBuilder;
083    }
084
085    @JsonIgnore
086    @Override
087    public QueryBuilder getEsFilter() {
088        if (getSelection().isEmpty()) {
089            return null;
090        }
091        return QueryBuilders.termsQuery(getField(), getSelection());
092    }
093
094    @Override
095    public void parseAggregation(NumericMetricsAggregation.SingleValue aggregation) {
096        this.value = aggregation.value();
097        this.buckets = Collections.singletonList(new BucketTerm(definition.getType(), value.longValue()));
098    }
099
100    public Double getValue() {
101        return value;
102    }
103}