From aad1de1a25a3d3e51421623973732176e6ccb071 Mon Sep 17 00:00:00 2001 From: Anan Zhuang Date: Tue, 29 Oct 2024 03:28:22 -0700 Subject: [PATCH] [BUG][Discover] Ensure save query loaded properly from asset (#8707) * [BUG][Discover] Ensure save query loaded properly from asset Signed-off-by: Anan Zhuang * Changeset file for PR #8707 created/updated --------- Signed-off-by: Anan Zhuang Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com> --- changelogs/fragments/8707.yml | 2 ++ .../application/view_components/utils/use_search.ts | 13 +++++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) create mode 100644 changelogs/fragments/8707.yml diff --git a/changelogs/fragments/8707.yml b/changelogs/fragments/8707.yml new file mode 100644 index 000000000000..2966d00ae17a --- /dev/null +++ b/changelogs/fragments/8707.yml @@ -0,0 +1,2 @@ +fix: +- Ensure save query loaded properly from asset ([#8707](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/8707)) \ No newline at end of file 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 50eba8d858de..aff68c36726c 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 @@ -86,7 +86,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 skipInitialFetch = useRef(false); const { @@ -387,9 +389,12 @@ export const useSearch = (services: DiscoverViewServices) => { // sync initial app filters from savedObject to filterManager const filters = cloneDeep(savedSearchInstance.searchSource.getOwnField('filter')); - 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])); @@ -397,7 +402,7 @@ export const useSearch = (services: DiscoverViewServices) => { } filterManager.setAppFilters(actualFilters); - data.query.queryString.setQuery(query); + data.query.queryString.setQuery(savedQuery ? data.query.queryString.getQuery() : query); setSavedSearch(savedSearchInstance); if (savedSearchInstance?.id) {