diff --git a/x-pack/plugins/lens/public/indexpattern_plugin/indexpattern.test.tsx b/x-pack/plugins/lens/public/indexpattern_plugin/indexpattern.test.tsx
index 9e19f7bd2c4d3..418cb9eab2443 100644
--- a/x-pack/plugins/lens/public/indexpattern_plugin/indexpattern.test.tsx
+++ b/x-pack/plugins/lens/public/indexpattern_plugin/indexpattern.test.tsx
@@ -91,6 +91,7 @@ describe('IndexPattern Data Source', () => {
// Private
operationType: 'value',
+ sourceField: 'op',
},
},
};
@@ -158,6 +159,46 @@ describe('IndexPattern Data Source', () => {
});
});
+ describe('#toExpression', () => {
+ it('should generate an empty expression when no columns are selected', async () => {
+ const state = await indexPatternDatasource.initialize();
+ expect(indexPatternDatasource.toExpression(state)).toEqual('');
+ });
+
+ it('should generate an expression for a values query', async () => {
+ const queryPersistedState: IndexPatternPersistedState = {
+ currentIndexPatternId: '1',
+ columnOrder: ['col1', 'col2'],
+ columns: {
+ col1: {
+ operationId: 'op1',
+ label: 'My Op',
+ dataType: 'string',
+ isBucketed: false,
+
+ // Private
+ operationType: 'value',
+ sourceField: 'op',
+ },
+ col2: {
+ operationId: 'op2',
+ label: 'My Op 2',
+ dataType: 'number',
+ isBucketed: false,
+
+ // Private
+ operationType: 'value',
+ sourceField: 'op2',
+ },
+ },
+ };
+ const state = await indexPatternDatasource.initialize(queryPersistedState);
+ expect(indexPatternDatasource.toExpression(state)).toMatchInlineSnapshot(
+ `"esdocs index=\\"1\\" fields=\\"op, op2\\" sort=\\"op, DESC\\""`
+ );
+ });
+ });
+
describe('#getPublicAPI', () => {
let publicAPI: DatasourcePublicAPI;
@@ -262,6 +303,7 @@ describe('IndexPattern Data Source', () => {
dataType: 'date',
isBucketed: false,
operationType: 'value',
+ sourceField: 'timestamp',
},
},
columnOrder: ['col1', 'col2'],
diff --git a/x-pack/plugins/lens/public/indexpattern_plugin/indexpattern.tsx b/x-pack/plugins/lens/public/indexpattern_plugin/indexpattern.tsx
index 81549315fe41b..ec558609e3900 100644
--- a/x-pack/plugins/lens/public/indexpattern_plugin/indexpattern.tsx
+++ b/x-pack/plugins/lens/public/indexpattern_plugin/indexpattern.tsx
@@ -24,6 +24,7 @@ interface IndexPatternColumn {
// Private
operationType: OperationType;
+ sourceField: string;
}
export interface IndexPattern {
@@ -107,6 +108,7 @@ export function IndexPatternDimensionPanel(props: IndexPatternDimensionPanelProp
isBucketed: false,
operationType: 'value' as OperationType,
+ sourceField: field.name,
}));
const filteredColumns = columns.filter(col => {
@@ -196,7 +198,16 @@ export function getIndexPatternDatasource(chrome: Chrome, toastNotifications: To
},
toExpression(state: IndexPatternPrivateState) {
- return `${JSON.stringify(state.columns)}`;
+ if (state.columnOrder.length === 0) {
+ return '';
+ }
+
+ const fieldNames = state.columnOrder.map(col => state.columns[col].sourceField);
+ const expression = `esdocs index="${state.currentIndexPatternId}" fields="${fieldNames.join(
+ ', '
+ )}" sort="${fieldNames[0]}, DESC"`;
+
+ return expression;
},
renderDataPanel(
diff --git a/x-pack/plugins/lens/public/xy_visualization_plugin/xy_visualization.tsx b/x-pack/plugins/lens/public/xy_visualization_plugin/xy_visualization.tsx
index 1da16b073b148..b5a6111c9570f 100644
--- a/x-pack/plugins/lens/public/xy_visualization_plugin/xy_visualization.tsx
+++ b/x-pack/plugins/lens/public/xy_visualization_plugin/xy_visualization.tsx
@@ -38,6 +38,14 @@ export const xyVisualization: Visualization
+ true,
+ suggestedOrder: 2,
+ }}
+ render={props.datasource.renderDimensionPanel}
+ />
,
domElement
);