diff --git a/public/app/plugins/datasource/elasticsearch/datasource.ts b/public/app/plugins/datasource/elasticsearch/datasource.ts index e3815bd9e6e..408f1d4ce95 100644 --- a/public/app/plugins/datasource/elasticsearch/datasource.ts +++ b/public/app/plugins/datasource/elasticsearch/datasource.ts @@ -35,6 +35,7 @@ import { import { bucketAggregationConfig } from './components/QueryEditor/BucketAggregationsEditor/utils'; import { BucketAggregation, + BucketAggregationWithField, isBucketAggregationWithField, } from './components/QueryEditor/BucketAggregationsEditor/aggregations'; import { generate, Observable, of, throwError } from 'rxjs'; @@ -390,7 +391,23 @@ export class ElasticDatasource extends DataSourceApi ({ + ...q, + bucketAggs: q.bucketAggs?.map((bucketAgg, aggIndex) => { + if (isBucketAggregationWithField(bucketAgg)) { + return { + ...bucketAgg, + field: (queries[queryIndex].bucketAggs?.[aggIndex] as BucketAggregationWithField).field, + }; + } + + return bucketAgg; + }), + })); } testDatasource() {