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.FULLTEXT_FIELD;
022
023import java.util.Collection;
024
025import org.elasticsearch.index.query.QueryBuilder;
026import org.elasticsearch.search.aggregations.AggregationBuilder;
027import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation;
028import org.nuxeo.ecm.core.api.DocumentModel;
029import org.nuxeo.ecm.core.query.sql.NXQL;
030import org.nuxeo.ecm.platform.query.api.AggregateDefinition;
031import org.nuxeo.ecm.platform.query.api.Bucket;
032import org.nuxeo.ecm.platform.query.core.AggregateBase;
033
034/**
035 * @since 6.0
036 */
037public abstract class AggregateEsBase<B extends Bucket> extends AggregateBase<B> {
038
039    public final static char XPATH_SEP = '/';
040
041    public final static char ES_MUTLI_LEVEL_SEP = '.';
042
043    public AggregateEsBase(AggregateDefinition definition, DocumentModel searchDocument) {
044        super(definition, searchDocument);
045    }
046
047    /**
048     * Return the Elasticsearch aggregate builder
049     */
050    public abstract AggregationBuilder getEsAggregate();
051
052    /**
053     * Return the Elasticsearch aggregate filter corresponding to the selection
054     */
055    public abstract QueryBuilder getEsFilter();
056
057    /**
058     * Extract the buckets from the Elasticsearch response
059     */
060    public abstract void parseEsBuckets(Collection<? extends MultiBucketsAggregation.Bucket> buckets);
061
062    @Override
063    public String getField() {
064        String ret = super.getField();
065        if (NXQL.ECM_FULLTEXT.equals(ret)) {
066            ret = FULLTEXT_FIELD;
067        }
068        ret = ret.replace(XPATH_SEP, ES_MUTLI_LEVEL_SEP);
069        return ret;
070    }
071
072}