diff --git a/x-pack/legacy/plugins/lens/public/indexpattern_plugin/dimension_panel/dimension_panel.test.tsx b/x-pack/legacy/plugins/lens/public/indexpattern_plugin/dimension_panel/dimension_panel.test.tsx index ee916e8f1a9c5..4f04a4d1bffbb 100644 --- a/x-pack/legacy/plugins/lens/public/indexpattern_plugin/dimension_panel/dimension_panel.test.tsx +++ b/x-pack/legacy/plugins/lens/public/indexpattern_plugin/dimension_panel/dimension_panel.test.tsx @@ -860,7 +860,10 @@ describe('IndexPatternDimensionPanel', () => { {...defaultProps} dragDropContext={{ ...dragDropContext, - dragging: { indexPatternId: 'foo', field: { type: 'number', name: 'bar' } }, + dragging: { + indexPatternId: 'foo', + field: { type: 'number', name: 'bar', aggregatable: true }, + }, }} state={dragDropState()} filterOperations={() => false} @@ -882,7 +885,10 @@ describe('IndexPatternDimensionPanel', () => { {...defaultProps} dragDropContext={{ ...dragDropContext, - dragging: { field: { type: 'number', name: 'bar' }, indexPatternId: 'foo' }, + dragging: { + field: { type: 'number', name: 'bar', aggregatable: true }, + indexPatternId: 'foo', + }, }} state={dragDropState()} filterOperations={op => op.dataType === 'number'} @@ -904,7 +910,10 @@ describe('IndexPatternDimensionPanel', () => { {...defaultProps} dragDropContext={{ ...dragDropContext, - dragging: { field: { type: 'number', name: 'bar' }, indexPatternId: 'foo2' }, + dragging: { + field: { type: 'number', name: 'bar', aggregatable: true }, + indexPatternId: 'foo2', + }, }} state={dragDropState()} filterOperations={op => op.dataType === 'number'} @@ -921,7 +930,10 @@ describe('IndexPatternDimensionPanel', () => { }); it('appends the dropped column when a field is dropped', () => { - const dragging = { field: { type: 'number', name: 'bar' }, indexPatternId: 'foo' }; + const dragging = { + field: { type: 'number', name: 'bar', aggregatable: true }, + indexPatternId: 'foo', + }; const testState = dragDropState(); wrapper = shallow( { }); it('updates a column when a field is dropped', () => { - const dragging = { field: { type: 'number', name: 'bar' }, indexPatternId: 'foo' }; + const dragging = { + field: { type: 'number', name: 'bar', aggregatable: true }, + indexPatternId: 'foo', + }; const testState = dragDropState(); wrapper = shallow( [], - getPossibleOperationsForField: ({ aggregationRestrictions, type }) => { - if (type === 'date' && (!aggregationRestrictions || aggregationRestrictions.date_histogram)) { + getPossibleOperationsForField: ({ aggregationRestrictions, aggregatable, type }) => { + if ( + type === 'date' && + aggregatable && + (!aggregationRestrictions || aggregationRestrictions.date_histogram) + ) { return [ { dataType: 'date', diff --git a/x-pack/legacy/plugins/lens/public/indexpattern_plugin/operation_definitions/metrics.tsx b/x-pack/legacy/plugins/lens/public/indexpattern_plugin/operation_definitions/metrics.tsx index 27d5c9606754e..9dd574d8d5e8f 100644 --- a/x-pack/legacy/plugins/lens/public/indexpattern_plugin/operation_definitions/metrics.tsx +++ b/x-pack/legacy/plugins/lens/public/indexpattern_plugin/operation_definitions/metrics.tsx @@ -23,8 +23,12 @@ function buildMetricOperation( type, displayName, getPossibleOperationsForDocument: () => [], - getPossibleOperationsForField: ({ aggregationRestrictions, type: fieldType }) => { - if (fieldType === 'number' && (!aggregationRestrictions || aggregationRestrictions[type])) { + getPossibleOperationsForField: ({ aggregationRestrictions, aggregatable, type: fieldType }) => { + if ( + fieldType === 'number' && + aggregatable && + (!aggregationRestrictions || aggregationRestrictions[type]) + ) { return [ { dataType: 'number', diff --git a/x-pack/legacy/plugins/lens/public/indexpattern_plugin/operation_definitions/terms.test.tsx b/x-pack/legacy/plugins/lens/public/indexpattern_plugin/operation_definitions/terms.test.tsx index f6e54009adaae..b1ee1dd4de2d2 100644 --- a/x-pack/legacy/plugins/lens/public/indexpattern_plugin/operation_definitions/terms.test.tsx +++ b/x-pack/legacy/plugins/lens/public/indexpattern_plugin/operation_definitions/terms.test.tsx @@ -70,7 +70,80 @@ describe('terms', () => { }); }); + describe('getPossibleOperationsForField', () => { + it('should return operation with the right type', () => { + expect( + termsOperation.getPossibleOperationsForField({ + aggregatable: true, + searchable: true, + name: 'test', + type: 'string', + aggregationRestrictions: { + terms: { + agg: 'terms', + }, + }, + }) + ).toEqual([ + { + dataType: 'string', + isBucketed: true, + }, + ]); + + expect( + termsOperation.getPossibleOperationsForField({ + aggregatable: true, + searchable: true, + name: 'test', + type: 'boolean', + }) + ).toEqual([ + { + dataType: 'boolean', + isBucketed: true, + }, + ]); + }); + + it('should not return an operation if restrictions prevent terms', () => { + expect( + termsOperation.getPossibleOperationsForField({ + aggregatable: false, + searchable: true, + name: 'test', + type: 'string', + }) + ).toEqual([]); + + expect( + termsOperation.getPossibleOperationsForField({ + aggregatable: true, + aggregationRestrictions: {}, + searchable: true, + name: 'test', + type: 'string', + }) + ).toEqual([]); + }); + }); + describe('buildColumn', () => { + it('should use type from the passed field', () => { + const termsColumn = termsOperation.buildColumn({ + layerId: 'first', + suggestedPriority: undefined, + field: { + aggregatable: true, + searchable: true, + type: 'boolean', + name: 'test', + }, + columns: {}, + }); + expect(termsColumn.dataType).toEqual('boolean'); + }); + it('should use existing metric column as order column', () => { const termsColumn = termsOperation.buildColumn({ layerId: 'first', @@ -85,6 +158,12 @@ describe('terms', () => { operationType: 'count', }, }, + field: { + aggregatable: true, + searchable: true, + type: 'boolean', + name: 'test', + }, }); expect(termsColumn.params).toEqual( expect.objectContaining({ diff --git a/x-pack/legacy/plugins/lens/public/indexpattern_plugin/operation_definitions/terms.tsx b/x-pack/legacy/plugins/lens/public/indexpattern_plugin/operation_definitions/terms.tsx index 2f9863547bf84..6964e9fb89f3b 100644 --- a/x-pack/legacy/plugins/lens/public/indexpattern_plugin/operation_definitions/terms.tsx +++ b/x-pack/legacy/plugins/lens/public/indexpattern_plugin/operation_definitions/terms.tsx @@ -10,6 +10,7 @@ import { EuiForm, EuiFormRow, EuiRange, EuiSelect } from '@elastic/eui'; import { TermsIndexPatternColumn, IndexPatternColumn } from '../indexpattern'; import { OperationDefinition } from '../operations'; import { updateColumnParam } from '../state_helpers'; +import { DataType } from '../../types'; type PropType = C extends React.ComponentType ? P : unknown; @@ -42,11 +43,15 @@ export const termsOperation: OperationDefinition = { defaultMessage: 'Top Values', }), getPossibleOperationsForDocument: () => [], - getPossibleOperationsForField: ({ aggregationRestrictions, type }) => { - if (type === 'string' && (!aggregationRestrictions || aggregationRestrictions.terms)) { + getPossibleOperationsForField: ({ aggregationRestrictions, aggregatable, type }) => { + if ( + (type === 'string' || type === 'boolean') && + aggregatable && + (!aggregationRestrictions || aggregationRestrictions.terms) + ) { return [ { - dataType: 'string', + dataType: type, isBucketed: true, }, ]; @@ -60,7 +65,7 @@ export const termsOperation: OperationDefinition = { return { label: ofName(field ? field.name : ''), - dataType: 'string', + dataType: field!.type as DataType, operationType: 'terms', suggestedPriority, sourceField: field ? field.name : '',