From 4dcaef60caf4ae715174be29c22218af4fe505a8 Mon Sep 17 00:00:00 2001 From: abbyhu2000 Date: Wed, 11 Dec 2024 00:10:17 +0000 Subject: [PATCH 1/2] fix saved search loading issue Signed-off-by: abbyhu2000 --- .../view_components/utils/use_search.ts | 38 +++++++++++++------ 1 file changed, 26 insertions(+), 12 deletions(-) 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 91b896f143f1..0130cf81bf74 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 @@ -84,7 +84,9 @@ export const useSearch = (services: DiscoverViewServices) => { const { pathname } = useLocation(); const initalSearchComplete = useRef(false); const [savedSearch, setSavedSearch] = useState(undefined); - const { savedSearch: savedSearchId, sort, interval } = useSelector((state) => state.discover); + const { savedSearch: savedSearchId, sort, interval, savedQuery } = useSelector( + (state) => state.discover + ); const indexPattern = useIndexPattern(services); const { data, @@ -322,21 +324,34 @@ export const useSearch = (services: DiscoverViewServices) => { useEffect(() => { (async () => { 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.getQuery(); + + 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); + 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) { + let actualFilters: any[] = []; + + if (savedQuery) { + actualFilters = data.query.filterManager.getFilters(); + } else if (filters !== undefined) { const result = typeof filters === 'function' ? filters() : filters; if (result !== undefined) { actualFilters.push(...(Array.isArray(result) ? result : [result])); @@ -345,6 +360,7 @@ export const useSearch = (services: DiscoverViewServices) => { filterManager.setAppFilters(actualFilters); data.query.queryString.setQuery(query); + setSavedSearch(savedSearchInstance); if (savedSearchInstance?.id) { chrome.recentlyAccessed.add( @@ -357,8 +373,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 From 7f7a649f9c4461ec8723ca48e72afa2fab193108 Mon Sep 17 00:00:00 2001 From: "opensearch-changeset-bot[bot]" <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com> Date: Wed, 11 Dec 2024 00:12:26 +0000 Subject: [PATCH 2/2] Changeset file for PR #9034 created/updated --- changelogs/fragments/9034.yml | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 changelogs/fragments/9034.yml diff --git a/changelogs/fragments/9034.yml b/changelogs/fragments/9034.yml new file mode 100644 index 000000000000..f3fe3e54192e --- /dev/null +++ b/changelogs/fragments/9034.yml @@ -0,0 +1,2 @@ +fix: +- Fix saved search loading issue ([#9034](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/9034)) \ No newline at end of file