@@ -83,6 +83,8 @@ import {
8383 MODIFY_COLUMNS_ON_SWITCH ,
8484} from '../../../common' ;
8585import { METRIC_TYPE } from '@kbn/analytics' ;
86+ import { SEARCH_SESSION_ID_QUERY_PARAM } from '../../url_generator' ;
87+ import { removeQueryParam , getQueryParams } from '../../../../kibana_utils/public' ;
8688
8789const fetchStatuses = {
8890 UNINITIALIZED : 'uninitialized' ,
@@ -91,6 +93,9 @@ const fetchStatuses = {
9193 ERROR : 'error' ,
9294} ;
9395
96+ const getSearchSessionIdFromURL = ( history ) =>
97+ getQueryParams ( history . location ) [ SEARCH_SESSION_ID_QUERY_PARAM ] ;
98+
9499const app = getAngularModule ( ) ;
95100
96101app . config ( ( $routeProvider ) => {
@@ -208,6 +213,8 @@ function discoverController($element, $route, $scope, $timeout, $window, Promise
208213 } ;
209214
210215 const history = getHistory ( ) ;
216+ // used for restoring background session
217+ let isInitialSearch = true ;
211218
212219 const {
213220 appStateContainer,
@@ -798,17 +805,30 @@ function discoverController($element, $route, $scope, $timeout, $window, Promise
798805 if ( abortController ) abortController . abort ( ) ;
799806 abortController = new AbortController ( ) ;
800807
801- const sessionId = data . search . session . start ( ) ;
808+ const searchSessionId = ( ( ) => {
809+ const searchSessionIdFromURL = getSearchSessionIdFromURL ( history ) ;
810+ if ( searchSessionIdFromURL ) {
811+ if ( isInitialSearch ) {
812+ data . search . session . restore ( searchSessionIdFromURL ) ;
813+ isInitialSearch = false ;
814+ return searchSessionIdFromURL ;
815+ } else {
816+ // navigating away from background search
817+ removeQueryParam ( history , SEARCH_SESSION_ID_QUERY_PARAM ) ;
818+ }
819+ }
820+ return data . search . session . start ( ) ;
821+ } ) ( ) ;
802822
803823 $scope
804824 . updateDataSource ( )
805825 . then ( setupVisualization )
806826 . then ( function ( ) {
807827 $scope . fetchStatus = fetchStatuses . LOADING ;
808- logInspectorRequest ( ) ;
828+ logInspectorRequest ( { searchSessionId } ) ;
809829 return $scope . searchSource . fetch ( {
810830 abortSignal : abortController . signal ,
811- sessionId,
831+ sessionId : searchSessionId ,
812832 } ) ;
813833 } )
814834 . then ( onResults )
@@ -900,15 +920,15 @@ function discoverController($element, $route, $scope, $timeout, $window, Promise
900920 $scope . fetchStatus = fetchStatuses . COMPLETE ;
901921 }
902922
903- function logInspectorRequest ( ) {
923+ function logInspectorRequest ( { searchSessionId = null } = { searchSessionId : null } ) {
904924 inspectorAdapters . requests . reset ( ) ;
905925 const title = i18n . translate ( 'discover.inspectorRequestDataTitle' , {
906926 defaultMessage : 'data' ,
907927 } ) ;
908928 const description = i18n . translate ( 'discover.inspectorRequestDescription' , {
909929 defaultMessage : 'This request queries Elasticsearch to fetch the data for the search.' ,
910930 } ) ;
911- inspectorRequest = inspectorAdapters . requests . start ( title , { description } ) ;
931+ inspectorRequest = inspectorAdapters . requests . start ( title , { description, searchSessionId } ) ;
912932 inspectorRequest . stats ( getRequestInspectorStats ( $scope . searchSource ) ) ;
913933 $scope . searchSource . getSearchRequestBody ( ) . then ( ( body ) => {
914934 inspectorRequest . json ( body ) ;
0 commit comments