From 20cc1a5f1acfcd9f60591a2cce7832363ca38216 Mon Sep 17 00:00:00 2001 From: Kawika Avilla Date: Wed, 23 Oct 2024 09:18:42 +0000 Subject: [PATCH] [bug] set saved search instance for Discover based on dataset Preventing any errors being thrown because of the index pattern not existing in the case of saved search was created with a dataset. Also fix issue that prevented saved search instance not being set even though loaded properly. The original fix that was removed fixed a previous issue but subsequent updates and refactors required an update that was uncaught. Issue: n/a Signed-off-by: Kawika Avilla --- .../search_source/create_search_source.ts | 10 ++++-- src/plugins/data/public/index.ts | 2 -- .../utils/state_management/discover_slice.tsx | 9 ++++-- .../view_components/utils/use_search.ts | 32 ++++++++++++++----- 4 files changed, 39 insertions(+), 14 deletions(-) diff --git a/src/plugins/data/common/search/search_source/create_search_source.ts b/src/plugins/data/common/search/search_source/create_search_source.ts index 74b164369018..ed7a66d67a99 100644 --- a/src/plugins/data/common/search/search_source/create_search_source.ts +++ b/src/plugins/data/common/search/search_source/create_search_source.ts @@ -32,6 +32,7 @@ import { migrateLegacyQuery } from './migrate_legacy_query'; import { SearchSource, SearchSourceDependencies } from './search_source'; import { IndexPatternsContract } from '../../index_patterns/index_patterns'; import { SearchSourceFields } from './types'; +import { DEFAULT_DATA } from '../../constants'; /** * Deserializes a json string and a set of referenced objects to a `SearchSource` instance. @@ -57,8 +58,13 @@ export const createSearchSource = ( const fields = { ...searchSourceFields }; // hydrating index pattern - if (fields.index && typeof fields.index === 'string') { - fields.index = await indexPatterns.get(searchSourceFields.index as any); + if ( + fields.index && + typeof fields.index === 'string' && + (!fields.query?.dataset?.type || + fields.query.dataset.type === DEFAULT_DATA.SET_TYPES.INDEX_PATTERN) + ) { + fields.index = await indexPatterns.get(fields.index as string); } const searchSource = new SearchSource(fields, searchSourceDependencies); diff --git a/src/plugins/data/public/index.ts b/src/plugins/data/public/index.ts index 773f24118907..e39722118721 100644 --- a/src/plugins/data/public/index.ts +++ b/src/plugins/data/public/index.ts @@ -68,8 +68,6 @@ export { DQLBody, SingleLineInput, DatasetSelector, - AdvancedSelector, - NoIndexPatternsPanel, DatasetSelectorAppearance, } from './ui'; diff --git a/src/plugins/discover/public/application/utils/state_management/discover_slice.tsx b/src/plugins/discover/public/application/utils/state_management/discover_slice.tsx index 026599d7fc65..bd1538384da1 100644 --- a/src/plugins/discover/public/application/utils/state_management/discover_slice.tsx +++ b/src/plugins/discover/public/application/utils/state_management/discover_slice.tsx @@ -11,7 +11,11 @@ import { RootState, DefaultViewState } from '../../../../../data_explorer/public import { buildColumns } from '../columns'; import * as utils from './common'; import { SortOrder } from '../../../saved_searches/types'; -import { DEFAULT_COLUMNS_SETTING, PLUGIN_ID } from '../../../../common'; +import { + DEFAULT_COLUMNS_SETTING, + PLUGIN_ID, + QUERY_ENHANCEMENT_ENABLED_SETTING, +} from '../../../../common'; export interface DiscoverState { /** @@ -90,7 +94,8 @@ export const getPreloadedState = async ({ const indexPatternId = savedSearchInstance.searchSource.getField('index')?.id; preloadedState.root = { metadata: { - indexPattern: indexPatternId, + ...(indexPatternId && + !config.get(QUERY_ENHANCEMENT_ENABLED_SETTING) && { indexPattern: indexPatternId }), view: PLUGIN_ID, }, }; diff --git a/src/plugins/discover/public/application/view_components/utils/use_search.ts b/src/plugins/discover/public/application/view_components/utils/use_search.ts index 3796faea1142..dbfb7d1824ba 100644 --- a/src/plugins/discover/public/application/view_components/utils/use_search.ts +++ b/src/plugins/discover/public/application/view_components/utils/use_search.ts @@ -354,16 +354,34 @@ export const useSearch = (services: DiscoverViewServices) => { const savedSearchInstance = await getSavedSearchById(savedSearchId); setSavedSearch(savedSearchInstance); - // if saved search does not exist, do not atempt to sync filters and query from savedObject - if (!savedSearch) { - return; + const query = + savedSearchInstance.searchSource.getField('query') || + data.query.queryString.getDefaultQuery(); + + const isEnhancementsEnabled = await uiSettings.get('query:enhancements:enabled'); + if (isEnhancementsEnabled && query.dataset) { + let pattern = await data.indexPatterns.get( + query.dataset.id, + query.dataset.type !== 'INDEX_PATTERN' + ); + if (!pattern) { + await data.query.queryString.getDatasetService().cacheDataset(query.dataset, { + uiSettings: services.uiSettings, + savedObjects: services.savedObjects, + notifications: services.notifications, + http: services.http, + data: services.data, + }); + pattern = await data.indexPatterns.get( + query.dataset.id, + query.dataset.type !== 'INDEX_PATTERN' + ); + savedSearchInstance.searchSource.setField('index', pattern); + } } // sync initial app filters from savedObject to filterManager const filters = cloneDeep(savedSearchInstance.searchSource.getOwnField('filter')); - const query = - savedSearchInstance.searchSource.getField('query') || - data.query.queryString.getDefaultQuery(); const actualFilters = []; if (filters !== undefined) { @@ -387,8 +405,6 @@ export const useSearch = (services: DiscoverViewServices) => { ); } })(); - - return () => {}; // This effect will only run when getSavedSearchById is called, which is // only called when the component is first mounted. // eslint-disable-next-line react-hooks/exhaustive-deps