diff --git a/public/app/plugins/datasource/elasticsearch/datasource.ts b/public/app/plugins/datasource/elasticsearch/datasource.ts index 6a763b8277e..430ffbec938 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'; @@ -388,7 +389,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() {