1

I was using elasticsearch 5.5 where I have one index and 3 types like below,

Index Name : Main

Types : TypeA, TypeB, TypeC

In 5.5, I was running aggregation like this,

AggregationBuilder ag = AggregationBuilders.terms("aggregatekey").field("field1").order(Terms.Order.aggregation("datafield", false)).size(100);

SearchResponse response = eswrapper.prepareSearch("Main").addAggregation(ag).setSize(0)
                    .execute().actionGet();

so It searches across the single index where 3 types were there.

Then I migrated to 7.8 where I have 3 indexes now (since there is no type concept in 7.8) like below

Index Names : TypeA, TypeB, TypeC

Now In 7.8, I run the same query but across 3 indices,

AggregationBuilder ag = AggregationBuilders.terms("aggregatekey").field("field1").order(BucketOrder.aggregation("datafield", false)).size(100);
    
    SearchResponse response = eswrapper.prepareSearch("TypeA","TypeB","TypeC").addAggregation(ag).setSize(0)
                        .execute().actionGet();

Now I am facing the following serious issue,

AggregationExecutionException[Merging/Reducing the aggregations failed when computing the aggregation [aggregatekey] because the field you gave in the aggregation query existed as two different types in two different indices]

Exception in thread "main" Failed to execute phase [fetch], 
    at org.elasticsearch.action.search.AbstractSearchAsyncAction.onPhaseFailure(AbstractSearchAsyncAction.java:551)
    at org.elasticsearch.action.search.FetchSearchPhase$1.onFailure(FetchSearchPhase.java:100)
    at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:39)
    at org.elasticsearch.common.util.concurrent.TimedRunnable.doRun(TimedRunnable.java:44)
    at org.elasticsearch.common.util.concurrent.ThreadContext$ContextPreservingAbstractRunnable.doRun(ThreadContext.java:695)
    at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630)
    at java.lang.Thread.run(Thread.java:832)
Caused by: AggregationExecutionException[Merging/Reducing the aggregations failed when computing the aggregation [aggregatekey] because the field you gave in the aggregation query existed as two different types in two different indices]
    at org.elasticsearch.search.aggregations.bucket.terms.InternalTerms.reduce(InternalTerms.java:210)
    at org.elasticsearch.search.aggregations.InternalAggregations.reduce(InternalAggregations.java:251)
    at org.elasticsearch.search.aggregations.InternalAggregations.topLevelReduce(InternalAggregations.java:195)
    at org.elasticsearch.action.search.SearchPhaseController.reduceAggs(SearchPhaseController.java:543)
    at org.elasticsearch.action.search.SearchPhaseController.reducedQueryPhase(SearchPhaseController.java:519)
    at org.elasticsearch.action.search.SearchPhaseController.reducedQueryPhase(SearchPhaseController.java:423)
    at org.elasticsearch.action.search.SearchPhaseController$2.reduce(SearchPhaseController.java:807)
    at org.elasticsearch.action.search.FetchSearchPhase.innerRun(FetchSearchPhase.java:116)
    at org.elasticsearch.action.search.FetchSearchPhase.access$000(FetchSearchPhase.java:47)
    at org.elasticsearch.action.search.FetchSearchPhase$1.doRun(FetchSearchPhase.java:95)
    at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37)
    ... 6 more

The reason for this failure is "Aggregating across the indices" So How do I aggregate across indices?

Thanks,
Harry

Harry
  • 3,072
  • 6
  • 43
  • 100
  • Hey Harry before I go into the Java code, hope you know that aggregation on 3 different indices won't give you a consolidated aggregation. Rather if you specify multiple indexes for e.g. `POST test_index1, test_index2/_search` and perform say aggregation on it, you'd get **three different sets of aggregation results**. Hope that is what you are looking for. – Kamal Kunjapur Jul 09 '20 at 18:03

0 Answers0