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_EXCLUDE_PROP; 022import static org.nuxeo.elasticsearch.ElasticSearchConstants.AGG_INCLUDE_PROP; 023import static org.nuxeo.elasticsearch.ElasticSearchConstants.AGG_MIN_DOC_COUNT_PROP; 024import static org.nuxeo.elasticsearch.ElasticSearchConstants.AGG_ORDER_COUNT_ASC; 025import static org.nuxeo.elasticsearch.ElasticSearchConstants.AGG_ORDER_COUNT_DESC; 026import static org.nuxeo.elasticsearch.ElasticSearchConstants.AGG_ORDER_PROP; 027import static org.nuxeo.elasticsearch.ElasticSearchConstants.AGG_ORDER_TERM_ASC; 028import static org.nuxeo.elasticsearch.ElasticSearchConstants.AGG_ORDER_TERM_DESC; 029import static org.nuxeo.elasticsearch.ElasticSearchConstants.AGG_SIZE_PROP; 030 031import java.util.ArrayList; 032import java.util.Collection; 033import java.util.List; 034import java.util.Map; 035 036import org.codehaus.jackson.annotate.JsonIgnore; 037import org.elasticsearch.index.query.QueryBuilder; 038import org.elasticsearch.index.query.QueryBuilders; 039import org.elasticsearch.search.aggregations.AggregationBuilders; 040import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation; 041import org.elasticsearch.search.aggregations.bucket.terms.Terms; 042import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder; 043import org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude; 044import org.nuxeo.ecm.core.api.DocumentModel; 045import org.nuxeo.ecm.platform.query.api.AggregateDefinition; 046import org.nuxeo.ecm.platform.query.core.BucketTerm; 047 048/** 049 * @since 6.0 050 */ 051public class TermAggregate extends AggregateEsBase<BucketTerm> { 052 053 public TermAggregate(AggregateDefinition definition, DocumentModel searchDocument) { 054 super(definition, searchDocument); 055 } 056 057 @JsonIgnore 058 @Override 059 public TermsAggregationBuilder getEsAggregate() { 060 TermsAggregationBuilder ret = AggregationBuilders.terms(getId()).field(getField()); 061 Map<String, String> props = getProperties(); 062 if (props.containsKey(AGG_SIZE_PROP)) { 063 ret.size(getAggSize(props.get(AGG_SIZE_PROP))); 064 } 065 if (props.containsKey(AGG_MIN_DOC_COUNT_PROP)) { 066 ret.minDocCount(Long.parseLong(props.get(AGG_MIN_DOC_COUNT_PROP))); 067 } 068 if (props.containsKey(AGG_EXCLUDE_PROP) || props.containsKey(AGG_INCLUDE_PROP)) { 069 String include = props.get(AGG_INCLUDE_PROP); 070 String exclude = props.get(AGG_EXCLUDE_PROP); 071 ret.includeExclude(new IncludeExclude(include, exclude)); 072 } 073 if (props.containsKey(AGG_ORDER_PROP)) { 074 switch (props.get(AGG_ORDER_PROP).toLowerCase()) { 075 case AGG_ORDER_COUNT_DESC: 076 ret.order(Terms.Order.count(false)); 077 break; 078 case AGG_ORDER_COUNT_ASC: 079 ret.order(Terms.Order.count(true)); 080 break; 081 case AGG_ORDER_TERM_DESC: 082 ret.order(Terms.Order.term(false)); 083 break; 084 case AGG_ORDER_TERM_ASC: 085 ret.order(Terms.Order.term(true)); 086 break; 087 default: 088 throw new IllegalArgumentException("Invalid order: " + props.get(AGG_ORDER_PROP)); 089 } 090 } 091 return ret; 092 } 093 094 @JsonIgnore 095 @Override 096 public QueryBuilder getEsFilter() { 097 if (getSelection().isEmpty()) { 098 return null; 099 } 100 return QueryBuilders.termsQuery(getField(), getSelection()); 101 } 102 103 @JsonIgnore 104 @Override 105 public void parseEsBuckets(Collection<? extends MultiBucketsAggregation.Bucket> buckets) { 106 List<BucketTerm> nxBuckets = new ArrayList<>(buckets.size()); 107 for (MultiBucketsAggregation.Bucket bucket : buckets) { 108 nxBuckets.add(new BucketTerm(bucket.getKeyAsString(), bucket.getDocCount())); 109 } 110 this.buckets = nxBuckets; 111 } 112}