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