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