diff --git a/public/app/plugins/datasource/elasticsearch/components/QueryEditor/BucketAggregationsEditor/SettingsEditor/TermsSettingsEditor.test.tsx b/public/app/plugins/datasource/elasticsearch/components/QueryEditor/BucketAggregationsEditor/SettingsEditor/TermsSettingsEditor.test.tsx
new file mode 100644
index 00000000000..3243378b00a
--- /dev/null
+++ b/public/app/plugins/datasource/elasticsearch/components/QueryEditor/BucketAggregationsEditor/SettingsEditor/TermsSettingsEditor.test.tsx
@@ -0,0 +1,41 @@
+import { screen } from '@testing-library/react';
+import { ElasticsearchQuery } from '../../../../types';
+import React from 'react';
+import { renderWithESProvider } from '../../../../test-helpers/render';
+import { Average, Derivative, TopMetrics } from '../../MetricAggregationsEditor/aggregations';
+import { Terms } from '../aggregations';
+import { TermsSettingsEditor } from './TermsSettingsEditor';
+import selectEvent from 'react-select-event';
+import { describeMetric } from 'app/plugins/datasource/elasticsearch/utils';
+
+describe('Terms Settings Editor', () => {
+ it('Pipeline aggregations should not be in "order by" options', () => {
+ const termsAgg: Terms = {
+ id: '1',
+ type: 'terms',
+ };
+ const avg: Average = { id: '2', type: 'avg', field: '@value' };
+ const derivative: Derivative = { id: '3', field: avg.id, type: 'derivative' };
+ const topMetrics: TopMetrics = { id: '4', type: 'top_metrics' };
+ const query: ElasticsearchQuery = {
+ refId: 'A',
+ query: '',
+ bucketAggs: [termsAgg],
+ metrics: [avg, derivative, topMetrics],
+ };
+
+ renderWithESProvider(, { providerProps: { query } });
+
+ const selectEl = screen.getByLabelText('Order By');
+ expect(selectEl).toBeInTheDocument();
+
+ selectEvent.openMenu(selectEl);
+
+ // Derivative is a pipeline aggregation, it shouldn't be present in the order by options
+ expect(screen.queryByText(describeMetric(derivative))).not.toBeInTheDocument();
+ // TopMetrics cannot be used as order by option
+ expect(screen.queryByText(describeMetric(topMetrics))).not.toBeInTheDocument();
+ // All other metric aggregations can be used in order by
+ expect(screen.getByText(describeMetric(avg))).toBeInTheDocument();
+ });
+});
diff --git a/public/app/plugins/datasource/elasticsearch/components/QueryEditor/BucketAggregationsEditor/SettingsEditor/TermsSettingsEditor.tsx b/public/app/plugins/datasource/elasticsearch/components/QueryEditor/BucketAggregationsEditor/SettingsEditor/TermsSettingsEditor.tsx
index dfc9b5fd1db..b13e020d20f 100644
--- a/public/app/plugins/datasource/elasticsearch/components/QueryEditor/BucketAggregationsEditor/SettingsEditor/TermsSettingsEditor.tsx
+++ b/public/app/plugins/datasource/elasticsearch/components/QueryEditor/BucketAggregationsEditor/SettingsEditor/TermsSettingsEditor.tsx
@@ -3,10 +3,20 @@ import { InlineField, Select, Input } from '@grafana/ui';
import { Terms } from '../aggregations';
import { useDispatch } from '../../../../hooks/useStatelessReducer';
import { inlineFieldProps } from '.';
-import { bucketAggregationConfig, createOrderByOptionsFromMetrics, orderOptions, sizeOptions } from '../utils';
+import { bucketAggregationConfig, orderByOptions, orderOptions, sizeOptions } from '../utils';
import { useCreatableSelectPersistedBehaviour } from '../../../hooks/useCreatableSelectPersistedBehaviour';
import { changeBucketAggregationSetting } from '../state/actions';
import { useQuery } from '../../ElasticsearchQueryContext';
+import { SelectableValue } from '@grafana/data';
+import { describeMetric } from '../../../../utils';
+import {
+ ExtendedStatMetaType,
+ ExtendedStats,
+ isPipelineAggregation,
+ MetricAggregation,
+ Percentiles,
+} from '../../MetricAggregationsEditor/aggregations';
+import { uniqueId } from 'lodash';
interface Props {
bucketAgg: Terms;
@@ -14,7 +24,7 @@ interface Props {
export const TermsSettingsEditor = ({ bucketAgg }: Props) => {
const { metrics } = useQuery();
- const orderBy = createOrderByOptionsFromMetrics(metrics);
+ const orderBy = createOrderByOptions(metrics);
const dispatch = useDispatch();
@@ -60,6 +70,7 @@ export const TermsSettingsEditor = ({ bucketAgg }: Props) => {