001/* 002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl-2.1.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * bdelbosc 016 */ 017package org.nuxeo.elasticsearch.aggregate; 018 019import static org.nuxeo.elasticsearch.ElasticSearchConstants.FULLTEXT_FIELD; 020 021import java.util.Collection; 022 023import org.elasticsearch.index.query.FilterBuilder; 024import org.elasticsearch.search.aggregations.AggregationBuilder; 025import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation; 026import org.joda.time.DateTime; 027import org.nuxeo.ecm.core.api.DocumentModel; 028import org.nuxeo.ecm.core.query.sql.NXQL; 029import org.nuxeo.ecm.platform.query.api.AggregateDefinition; 030import org.nuxeo.ecm.platform.query.api.Bucket; 031import org.nuxeo.ecm.platform.query.core.AggregateBase; 032 033/** 034 * @since 6.0 035 */ 036public abstract class AggregateEsBase<B extends Bucket> extends AggregateBase<B> { 037 038 public final static char XPATH_SEP = '/'; 039 040 public final static char ES_MUTLI_LEVEL_SEP = '.'; 041 042 public AggregateEsBase(AggregateDefinition definition, DocumentModel searchDocument) { 043 super(definition, searchDocument); 044 } 045 046 /** 047 * Return the Elasticsearch aggregate builder 048 */ 049 public abstract AggregationBuilder getEsAggregate(); 050 051 /** 052 * Return the Elasticsearch aggregate filter corresponding to the selection 053 */ 054 public abstract FilterBuilder getEsFilter(); 055 056 /** 057 * Extract the buckets from the Elasticsearch response 058 */ 059 public abstract void parseEsBuckets(Collection<? extends MultiBucketsAggregation.Bucket> buckets); 060 061 @Override 062 public String getField() { 063 String ret = super.getField(); 064 if (NXQL.ECM_FULLTEXT.equals(ret)) { 065 ret = FULLTEXT_FIELD; 066 } 067 ret = ret.replace(XPATH_SEP, ES_MUTLI_LEVEL_SEP); 068 return ret; 069 } 070 071 /** 072 * Convert embedded Elasticsearch DateTime to joda DateTime 073 */ 074 protected DateTime getDateTime(org.elasticsearch.common.joda.time.DateTime date) { 075 if (date == null) { 076 return null; 077 } 078 return new DateTime(date.getMillis()); 079 } 080 081}