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_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_MISSING;
027import static org.nuxeo.elasticsearch.ElasticSearchConstants.AGG_SUM;
028import static org.nuxeo.elasticsearch.ElasticSearchConstants.AGG_TYPE_DATE_HISTOGRAM;
029import static org.nuxeo.elasticsearch.ElasticSearchConstants.AGG_TYPE_DATE_RANGE;
030import static org.nuxeo.elasticsearch.ElasticSearchConstants.AGG_TYPE_HISTOGRAM;
031import static org.nuxeo.elasticsearch.ElasticSearchConstants.AGG_TYPE_RANGE;
032import static org.nuxeo.elasticsearch.ElasticSearchConstants.AGG_TYPE_SIGNIFICANT_TERMS;
033import static org.nuxeo.elasticsearch.ElasticSearchConstants.AGG_TYPE_TERMS;
034
035import org.elasticsearch.search.aggregations.Aggregation;
036import org.nuxeo.ecm.core.api.DocumentModel;
037import org.nuxeo.ecm.platform.query.api.AggregateDefinition;
038import org.nuxeo.ecm.platform.query.api.Bucket;
039
040/**
041 * @since 6.0
042 */
043public final class AggregateFactory {
044
045    private AggregateFactory() {
046    }
047
048    public static AggregateEsBase<? extends Aggregation, ? extends Bucket> create(AggregateDefinition def,
049            DocumentModel searchDocumentModel) {
050        switch (def.getType()) {
051        case AGG_TYPE_TERMS:
052            return new TermAggregate(def, searchDocumentModel);
053        case AGG_TYPE_RANGE:
054            return new RangeAggregate(def, searchDocumentModel);
055        case AGG_TYPE_DATE_HISTOGRAM:
056            return new DateHistogramAggregate(def, searchDocumentModel);
057        case AGG_TYPE_SIGNIFICANT_TERMS:
058            return new SignificantTermAggregate(def, searchDocumentModel);
059        case AGG_TYPE_HISTOGRAM:
060            return new HistogramAggregate(def, searchDocumentModel);
061        case AGG_TYPE_DATE_RANGE:
062            return new DateRangeAggregate(def, searchDocumentModel);
063        case AGG_CARDINALITY:
064        case AGG_COUNT:
065        case AGG_SUM:
066        case AGG_AVG:
067        case AGG_MAX:
068        case AGG_MIN:
069            return new SingleValueMetricAggregate(def, searchDocumentModel);
070        case AGG_MISSING:
071            return new MissingAggregate(def, searchDocumentModel);
072        default:
073            throw new IllegalArgumentException("Unknown aggregate type: " + def.getType());
074        }
075
076    }
077}