diff --git a/x-pack/legacy/plugins/lens/public/indexpattern_plugin/indexpattern.tsx b/x-pack/legacy/plugins/lens/public/indexpattern_plugin/indexpattern.tsx index 15f19bb9d97e6b..b58a2d8ca52c75 100644 --- a/x-pack/legacy/plugins/lens/public/indexpattern_plugin/indexpattern.tsx +++ b/x-pack/legacy/plugins/lens/public/indexpattern_plugin/indexpattern.tsx @@ -115,8 +115,12 @@ export function getIndexPatternDatasource({ const indexPatternDatasource: Datasource = { id: 'indexpattern', - initialize(state?: IndexPatternPersistedState) { - return loadInitialState({ state, savedObjectsClient }); + async initialize(state?: IndexPatternPersistedState) { + return loadInitialState({ + state, + savedObjectsClient, + defaultIndexPatternId: core.uiSettings.get('defaultIndex'), + }); }, getPersistableState({ currentIndexPatternId, layers }: IndexPatternPrivateState) { diff --git a/x-pack/legacy/plugins/lens/public/indexpattern_plugin/loader.test.ts b/x-pack/legacy/plugins/lens/public/indexpattern_plugin/loader.test.ts index 2fb678aed5a54b..e180ab690d4185 100644 --- a/x-pack/legacy/plugins/lens/public/indexpattern_plugin/loader.test.ts +++ b/x-pack/legacy/plugins/lens/public/indexpattern_plugin/loader.test.ts @@ -114,8 +114,9 @@ const sampleIndexPatterns = { { name: 'source', type: 'string', - aggregatable: true, - searchable: true, + aggregatable: false, + searchable: false, + scripted: true, aggregationRestrictions: { terms: { agg: 'terms', @@ -196,7 +197,7 @@ describe('loader', () => { expect(cache).toMatchObject(sampleIndexPatterns); }); - it('should not allow full text fields', async () => { + it('should allow scripted, but not full text fields', async () => { const cache = await loadIndexPatterns({ cache: {}, patterns: ['a', 'b'], @@ -286,6 +287,26 @@ describe('loader', () => { }); }); + it('should use the default index pattern id, if provided', async () => { + const state = await loadInitialState({ + defaultIndexPatternId: 'b', + savedObjectsClient: mockClient(), + }); + + expect(state).toMatchObject({ + currentIndexPatternId: 'b', + indexPatternRefs: [ + { id: 'a', title: sampleIndexPatterns.a.title }, + { id: 'b', title: sampleIndexPatterns.b.title }, + ], + indexPatterns: { + b: sampleIndexPatterns.b, + }, + layers: {}, + showEmptyFields: false, + }); + }); + it('should initialize from saved state', async () => { const savedState: IndexPatternPersistedState = { currentIndexPatternId: 'b', diff --git a/x-pack/legacy/plugins/lens/public/indexpattern_plugin/loader.ts b/x-pack/legacy/plugins/lens/public/indexpattern_plugin/loader.ts index 661c627f3454f6..7f46f50786cf48 100644 --- a/x-pack/legacy/plugins/lens/public/indexpattern_plugin/loader.ts +++ b/x-pack/legacy/plugins/lens/public/indexpattern_plugin/loader.ts @@ -84,9 +84,11 @@ export async function loadIndexPatterns({ export async function loadInitialState({ state, savedObjectsClient, + defaultIndexPatternId, }: { state?: IndexPatternPersistedState; savedObjectsClient: SavedObjectsClient; + defaultIndexPatternId?: string; }): Promise { const indexPatternRefs = await loadIndexPatternRefs(savedObjectsClient); const requiredPatterns = _.unique( @@ -94,7 +96,7 @@ export async function loadInitialState({ ? Object.values(state.layers) .map(l => l.indexPatternId) .concat(state.currentIndexPatternId) - : [indexPatternRefs[0].id] + : [defaultIndexPatternId || indexPatternRefs[0].id] ); const currentIndexPatternId = requiredPatterns[0]; @@ -280,7 +282,7 @@ function fromSavedObject( type, title: attributes.title, fields: (JSON.parse(attributes.fields) as IndexPatternField[]) - .filter(({ aggregatable }) => !!aggregatable) + .filter(({ aggregatable, scripted }) => !!aggregatable || !!scripted) .concat(documentField), typeMeta: attributes.typeMeta ? (JSON.parse(attributes.typeMeta) as SavedRestrictionsInfo) diff --git a/x-pack/legacy/plugins/lens/public/indexpattern_plugin/types.ts b/x-pack/legacy/plugins/lens/public/indexpattern_plugin/types.ts index 9ed50836333148..50478515d19ce2 100644 --- a/x-pack/legacy/plugins/lens/public/indexpattern_plugin/types.ts +++ b/x-pack/legacy/plugins/lens/public/indexpattern_plugin/types.ts @@ -39,6 +39,7 @@ export interface IndexPatternField { type: string; esTypes?: string[]; aggregatable: boolean; + scripted?: boolean; searchable: boolean; aggregationRestrictions?: AggregationRestrictions; }