diff --git a/changelogs/fragments/8707.yml b/changelogs/fragments/8707.yml new file mode 100644 index 00000000000..2966d00ae17 --- /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 50eba8d858d..aff68c36726 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) {