diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/InternalTerms.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/InternalTerms.java index 4c4b2e8371d..9cc1f603123 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/InternalTerms.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/InternalTerms.java @@ -24,6 +24,7 @@ import com.google.common.collect.Multimap; import org.elasticsearch.common.io.stream.Streamable; import org.elasticsearch.common.xcontent.ToXContent; +import org.elasticsearch.search.aggregations.AggregationExecutionException; import org.elasticsearch.search.aggregations.Aggregations; import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.search.aggregations.InternalAggregations; @@ -170,8 +171,25 @@ public abstract class InternalTerms buckets = ArrayListMultimap.create(); long sumDocCountError = 0; long otherDocCount = 0; + InternalTerms referenceTerms = null; for (InternalAggregation aggregation : aggregations) { InternalTerms terms = (InternalTerms) aggregation; + if (referenceTerms == null && !terms.getClass().equals(UnmappedTerms.class)) { + referenceTerms = (InternalTerms) aggregation; + } + if (referenceTerms != null && + !referenceTerms.getClass().equals(terms.getClass()) && + !terms.getClass().equals(UnmappedTerms.class)) { + // control gets into this loop when the same field name against which the query is executed + // is of different types in different indices. + throw new AggregationExecutionException("Merging/Reducing the aggregations failed " + + "when computing the aggregation [ Name: " + + referenceTerms.getName() + ", Type: " + + referenceTerms.type() + " ]" + " because: " + + "the field you gave in the aggregation query " + + "existed as two different types " + + "in two different indices"); + } otherDocCount += terms.getSumOfOtherDocCounts(); final long thisAggDocCountError; if (terms.buckets.size() < this.shardSize || this.order == InternalOrder.TERM_ASC || this.order == InternalOrder.TERM_DESC) {