Skip to content

Commit

Permalink
[Discover] Fix overwriting data view id when loading a saved search w…
Browse files Browse the repository at this point in the history
…ith ES|QL (#175769)

This commit prevents the propagation of the AppState index (which is the id of the data view in the URL), to the state of a loaded saved search containing a ES|QL query. Since the id of the data view used in this case is retrieved from the text base language statement, there's no reason for mutating the saved search state
  • Loading branch information
kertal authored Feb 1, 2024
1 parent 38872c6 commit 415b925
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
33 changes: 33 additions & 0 deletions src/plugins/discover/public/__mocks__/data_view_esql.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import { buildDataViewMock } from '@kbn/discover-utils/src/__mocks__';
import { DataView } from '@kbn/data-views-plugin/common';

const fields = [
{
name: '@timestamp',
displayName: 'timestamp',
type: 'date',
scripted: false,
filterable: true,
aggregatable: true,
sortable: true,
},
{
name: 'message',
displayName: 'message',
type: 'string',
scripted: false,
filterable: false,
},
] as DataView['fields'];

export const dataViewEsql = buildDataViewMock({
name: 'index-pattern-esql',
fields,
});
6 changes: 4 additions & 2 deletions src/plugins/discover/public/__mocks__/saved_search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { createSearchSourceMock } from '@kbn/data-plugin/public/mocks';
import { dataViewMock } from '@kbn/discover-utils/src/__mocks__';
import { dataViewWithTimefieldMock } from './data_view_with_timefield';
import { dataViewAdHoc } from './data_view_complex';
import { dataViewEsql } from './data_view_esql';

export const savedSearchMock = {
id: 'the-saved-search-id',
Expand All @@ -30,9 +31,10 @@ export const savedSearchMockWithTimeFieldNew = {
export const savedSearchMockWithESQL = {
id: 'the-saved-search-id-esql',
searchSource: createSearchSourceMock({
index: dataViewWithTimefieldMock,
query: { esql: 'FROM "the-saved-search-id-esql"' },
index: dataViewEsql,
query: { esql: 'FROM "index-pattern-esql"' },
}),
isTextBasedQuery: true,
} as unknown as SavedSearch;

export const savedSearchAdHoc = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
savedSearchMock,
savedSearchMockWithTimeField,
savedSearchMockWithTimeFieldNew,
savedSearchMockWithESQL,
} from '../../../__mocks__/saved_search';
import { discoverServiceMock } from '../../../__mocks__/services';
import { dataViewMock } from '@kbn/discover-utils/src/__mocks__';
Expand Down Expand Up @@ -650,6 +651,20 @@ describe('Test discover state actions', () => {
expect(state.internalState.getState().adHocDataViews[0].id).toBe(adHocDataViewId);
});

test('loadSavedSearch with ES|QL, data view index is not overwritten by URL ', async () => {
const savedSearchMockWithESQLCopy = copySavedSearch(savedSearchMockWithESQL);
const persistedDataViewId = savedSearchMockWithESQLCopy?.searchSource.getField('index')!.id;
const url = "/#?_a=(index:'the-data-view-id')&_g=()";
const { state } = await getState(url, {
savedSearch: savedSearchMockWithESQLCopy,
isEmptyUrl: false,
});
const nextSavedSearch = await state.actions.loadSavedSearch({
savedSearchId: savedSearchMockWithESQL.id,
});
expect(persistedDataViewId).toBe(nextSavedSearch?.searchSource.getField('index')!.id);
});

test('onChangeDataView', async () => {
const { state, getCurrentUrl } = await getState('/', { savedSearch: savedSearchMock });
const { actions, savedSearchState, dataState, appState } = state;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ export const loadSavedSearch = async (
savedSearch: nextSavedSearch,
});
const dataViewDifferentToAppState = stateDataView.id !== savedSearchDataViewId;
if (stateDataView && (dataViewDifferentToAppState || !savedSearchDataViewId)) {
if (
!nextSavedSearch.isTextBasedQuery &&
stateDataView &&
(dataViewDifferentToAppState || !savedSearchDataViewId)
) {
nextSavedSearch.searchSource.setField('index', stateDataView);
}
}
Expand Down

0 comments on commit 415b925

Please sign in to comment.