Skip to content

Commit e4281e4

Browse files
authored
[bug] set saved search instance for Discover based on dataset (#8689)
* [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 <[email protected]> * set saved search order Signed-off-by: Kawika Avilla <[email protected]> --------- Signed-off-by: Kawika Avilla <[email protected]>
1 parent c11a801 commit e4281e4

File tree

5 files changed

+41
-16
lines changed

5 files changed

+41
-16
lines changed

src/plugins/data/common/search/search_source/create_search_source.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import { migrateLegacyQuery } from './migrate_legacy_query';
3232
import { SearchSource, SearchSourceDependencies } from './search_source';
3333
import { IndexPatternsContract } from '../../index_patterns/index_patterns';
3434
import { SearchSourceFields } from './types';
35+
import { DEFAULT_DATA } from '../../constants';
3536

3637
/**
3738
* Deserializes a json string and a set of referenced objects to a `SearchSource` instance.
@@ -57,8 +58,13 @@ export const createSearchSource = (
5758
const fields = { ...searchSourceFields };
5859

5960
// hydrating index pattern
60-
if (fields.index && typeof fields.index === 'string') {
61-
fields.index = await indexPatterns.get(searchSourceFields.index as any);
61+
if (
62+
fields.index &&
63+
typeof fields.index === 'string' &&
64+
(!fields.query?.dataset?.type ||
65+
fields.query.dataset.type === DEFAULT_DATA.SET_TYPES.INDEX_PATTERN)
66+
) {
67+
fields.index = await indexPatterns.get(fields.index as string);
6268
}
6369

6470
const searchSource = new SearchSource(fields, searchSourceDependencies);

src/plugins/data/public/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,6 @@ export {
6868
DQLBody,
6969
SingleLineInput,
7070
DatasetSelector,
71-
AdvancedSelector,
72-
NoIndexPatternsPanel,
7371
DatasetSelectorAppearance,
7472
} from './ui';
7573

src/plugins/discover/public/application/utils/state_management/discover_slice.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ import { RootState, DefaultViewState } from '../../../../../data_explorer/public
1111
import { buildColumns } from '../columns';
1212
import * as utils from './common';
1313
import { SortOrder } from '../../../saved_searches/types';
14-
import { DEFAULT_COLUMNS_SETTING, PLUGIN_ID } from '../../../../common';
14+
import {
15+
DEFAULT_COLUMNS_SETTING,
16+
PLUGIN_ID,
17+
QUERY_ENHANCEMENT_ENABLED_SETTING,
18+
} from '../../../../common';
1519

1620
export interface DiscoverState {
1721
/**
@@ -90,7 +94,8 @@ export const getPreloadedState = async ({
9094
const indexPatternId = savedSearchInstance.searchSource.getField('index')?.id;
9195
preloadedState.root = {
9296
metadata: {
93-
indexPattern: indexPatternId,
97+
...(indexPatternId &&
98+
!config.get(QUERY_ENHANCEMENT_ENABLED_SETTING) && { indexPattern: indexPatternId }),
9499
view: PLUGIN_ID,
95100
},
96101
};

src/plugins/discover/public/application/view_components/utils/use_search.ts

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -358,18 +358,35 @@ export const useSearch = (services: DiscoverViewServices) => {
358358
useEffect(() => {
359359
(async () => {
360360
const savedSearchInstance = await getSavedSearchById(savedSearchId);
361-
setSavedSearch(savedSearchInstance);
362361

363-
// if saved search does not exist, do not atempt to sync filters and query from savedObject
364-
if (!savedSearch) {
365-
return;
362+
const query =
363+
savedSearchInstance.searchSource.getField('query') ||
364+
data.query.queryString.getDefaultQuery();
365+
366+
const isEnhancementsEnabled = await uiSettings.get('query:enhancements:enabled');
367+
if (isEnhancementsEnabled && query.dataset) {
368+
let pattern = await data.indexPatterns.get(
369+
query.dataset.id,
370+
query.dataset.type !== 'INDEX_PATTERN'
371+
);
372+
if (!pattern) {
373+
await data.query.queryString.getDatasetService().cacheDataset(query.dataset, {
374+
uiSettings: services.uiSettings,
375+
savedObjects: services.savedObjects,
376+
notifications: services.notifications,
377+
http: services.http,
378+
data: services.data,
379+
});
380+
pattern = await data.indexPatterns.get(
381+
query.dataset.id,
382+
query.dataset.type !== 'INDEX_PATTERN'
383+
);
384+
savedSearchInstance.searchSource.setField('index', pattern);
385+
}
366386
}
367387

368388
// sync initial app filters from savedObject to filterManager
369389
const filters = cloneDeep(savedSearchInstance.searchSource.getOwnField('filter'));
370-
const query =
371-
savedSearchInstance.searchSource.getField('query') ||
372-
data.query.queryString.getDefaultQuery();
373390
const actualFilters = [];
374391

375392
if (filters !== undefined) {
@@ -381,6 +398,7 @@ export const useSearch = (services: DiscoverViewServices) => {
381398

382399
filterManager.setAppFilters(actualFilters);
383400
data.query.queryString.setQuery(query);
401+
setSavedSearch(savedSearchInstance);
384402

385403
if (savedSearchInstance?.id) {
386404
chrome.recentlyAccessed.add(
@@ -393,8 +411,6 @@ export const useSearch = (services: DiscoverViewServices) => {
393411
);
394412
}
395413
})();
396-
397-
return () => {};
398414
// This effect will only run when getSavedSearchById is called, which is
399415
// only called when the component is first mounted.
400416
// eslint-disable-next-line react-hooks/exhaustive-deps

src/plugins/query_enhancements/public/plugin.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export class QueryEnhancementsPlugin
9696
title: 'SQL',
9797
search: sqlSearchInterceptor,
9898
getQueryString: (query: Query) => {
99-
return `SELECT * FROM ${queryString.getQuery().dataset?.title} LIMIT 10`;
99+
return `SELECT * FROM ${query.dataset?.title} LIMIT 10`;
100100
},
101101
fields: {
102102
filterable: false,

0 commit comments

Comments
 (0)