import { Plugin , CoreSetup , CoreStart , PluginInitializerContext } from 'src/core/public' ; import { BehaviorSubject } from 'rxjs' ; import { ISearchSetup , ISearchStart , SearchEnhancements } from './types' ; import { handleResponse } from './fetch' ; import { IOpenSearchSearchRequest , IOpenSearchSearchResponse , IOpenSearchDashboardsSearchRequest , IOpenSearchDashboardsSearchResponse , ISearchGeneric , ISearchOptions , SearchSourceService , SearchSourceDependencies , } from '../../common/search' ; import { getCallMsearch } from './legacy' ; import { AggsService , AggsStartDependencies } from './aggs' ; import { IndexPatternsContract } from '../index_patterns/index_patterns' ; import { ISearchInterceptor , SearchInterceptor } from './search_interceptor' ; import { SearchUsageCollector , createUsageCollector } from './collectors' ; import { UsageCollectionSetup } from '../../../usage_collection/public' ; import { opensearchdsl , opensearchRawResponse } from './expressions' ; import { ExpressionsSetup } from '../../../expressions/public' ; import { ConfigSchema } from '../../config' ; import { SHARD_DELAY_AGG_NAME , getShardDelayBucketAgg , } from '../../common/search/aggs/buckets/shard_delay' ; import { aggShardDelay } from '../../common/search/aggs/buckets/shard_delay_fn' ; internal export interface SearchServiceSetupDependencies { expressions : ExpressionsSetup ; usageCollection ? : UsageCollectionSetup ; } internal export interface SearchServiceStartDependencies { fieldFormats : AggsStartDependencies [ 'fieldFormats' ] ; indexPatterns : IndexPatternsContract ; } export class SearchService implements Plugin < ISearchSetup , ISearchStart > { private readonly aggsService = new AggsService ( ) ; private readonly searchSourceService = new SearchSourceService ( ) ; private searchInterceptor ! : ISearchInterceptor ; private usageCollector ? : SearchUsageCollector ; constructor ( private initializerContext ) { } public setup ( { http , getStartServices , notifications , uiSettings } , { expressions , usageCollection } ) { this . usageCollector = createUsageCollector ( getStartServices , usageCollection ) ; /**
         * A global object that intercepts all searches and provides convenience methods for cancelling
         * all pending search requests, as well as getting the number of pending search requests.
         */ this . searchInterceptor = new SearchInterceptor ( { toasts : notifications . toasts , http , uiSettings , startServices : getStartServices ( ) , usageCollector : this . usageCollector ! , } ) ; expressions . registerFunction ( opensearchdsl ) ; expressions . registerType ( opensearchRawResponse ) ; const aggs = this . aggsService . setup ( { registerFunction : expressions . registerFunction , uiSettings , } ) ; if ( this . initializerContext . config . get ( ) . search . aggs . shardDelay . enabled ) { aggs . types . registerBucket ( SHARD_DELAY_AGG_NAME , getShardDelayBucketAgg ) ; expressions . registerFunction ( aggShardDelay ) ; } return { aggs , usageCollector : this . usageCollector ! , __enhance : ( enhancements ) => { this . searchInterceptor = enhancements . searchInterceptor ; } , } ; } public start ( { application , http , notifications , uiSettings } , { fieldFormats , indexPatterns } ) { const search = ( ( request , options ) => { return this . searchInterceptor . search ( request , options ) ; } ) as ISearchGeneric ; const loadingCount$ = new BehaviorSubject ( 0 ) ; http . addLoadingCountSource ( loadingCount$ ) ; const searchSourceDependencies = { getConfig : uiSettings . get . bind ( uiSettings ) , search : < SearchStrategyRequest extends IOpenSearchDashboardsSearchRequest = IOpenSearchSearchRequest , SearchStrategyResponse extends IOpenSearchDashboardsSearchResponse = IOpenSearchSearchResponse > ( request , options ) => { return search ( request , options ) . toPromise ( ) as Promise < SearchStrategyResponse > ; } , onResponse : handleResponse , legacy : { callMsearch : getCallMsearch ( { http } ) , loadingCount$ , } , } ; return { aggs : this . aggsService . start ( { fieldFormats , uiSettings } ) , search , showError : ( e ) => { this . searchInterceptor . showError ( e ) ; } , searchSource : this . searchSourceService . start ( indexPatterns , searchSourceDependencies ) , } ; } public stop ( ) { this . aggsService . stop ( ) ; this . searchSourceService . stop ( ) ; } }