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_MIN_DOC_COUNT_PROP; 022import static org.nuxeo.elasticsearch.ElasticSearchConstants.AGG_SIZE_PROP; 023 024import java.util.ArrayList; 025import java.util.Collection; 026import java.util.List; 027import java.util.Map; 028 029import org.elasticsearch.index.query.QueryBuilder; 030import org.elasticsearch.index.query.QueryBuilders; 031import org.elasticsearch.search.aggregations.AggregationBuilders; 032import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation; 033import org.elasticsearch.search.aggregations.bucket.significant.SignificantTermsAggregationBuilder; 034import org.nuxeo.ecm.core.api.DocumentModel; 035import org.nuxeo.ecm.platform.query.api.AggregateDefinition; 036import org.nuxeo.ecm.platform.query.core.BucketTerm; 037 038import com.fasterxml.jackson.annotation.JsonIgnore; 039 040/** 041 * @since 6.0 042 */ 043public class SignificantTermAggregate extends MultiBucketAggregate<BucketTerm> { 044 045 public SignificantTermAggregate(AggregateDefinition definition, DocumentModel searchDocument) { 046 super(definition, searchDocument); 047 } 048 049 @JsonIgnore 050 @Override 051 public SignificantTermsAggregationBuilder getEsAggregate() { 052 SignificantTermsAggregationBuilder ret = AggregationBuilders.significantTerms(getId()).field(getField()); 053 Map<String, String> props = getProperties(); 054 if (props.containsKey(AGG_SIZE_PROP)) { 055 ret.size(getAggSize(props.get(AGG_SIZE_PROP))); 056 } 057 if (props.containsKey(AGG_MIN_DOC_COUNT_PROP)) { 058 ret.minDocCount(Integer.parseInt(props.get(AGG_MIN_DOC_COUNT_PROP))); 059 } 060 return ret; 061 } 062 063 @JsonIgnore 064 @Override 065 public QueryBuilder getEsFilter() { 066 if (getSelection().isEmpty()) { 067 return null; 068 } 069 return QueryBuilders.termsQuery(getField(), getSelection()); 070 } 071 072 @JsonIgnore 073 @Override 074 public void parseEsBuckets(Collection<? extends MultiBucketsAggregation.Bucket> buckets) { 075 List<BucketTerm> nxBuckets = new ArrayList<>(buckets.size()); 076 for (MultiBucketsAggregation.Bucket bucket : buckets) { 077 nxBuckets.add(new BucketTerm(bucket.getKeyAsString(), bucket.getDocCount())); 078 } 079 this.buckets = nxBuckets; 080 } 081 082}