import Boom from '@hapi/boom' ; import { IScopedClusterClient } from 'kibana/server' ; import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey' ; import type { TransportResult } from '@elastic/elasticsearch' ; import { JobSavedObjectService } from '../../saved_objects' ; import { ML_RESULTS_INDEX_PATTERN } from '../../../common/constants/index_patterns' ; import type { JobType } from '../../../common/types/saved_objects' ; export function searchProvider ( client , jobSavedObjectService ) { async function jobIdsCheck ( jobType , jobIds ) { if ( jobIds . length ) { const filteredJobIds = await jobSavedObjectService . filterJobIdsForSpace ( jobType , jobIds ) ; const missingIds = jobIds . filter ( ( j ) => filteredJobIds . indexOf ( j ) === - 1 ) ; if ( missingIds . length ) { throw Boom . notFound ( `${ missingIds . join ( ',' ) } missing` ) ; } } } async function anomalySearch < T > ( searchParams , jobIds ) { await jobIdsCheck ( 'anomaly-detector' , jobIds ) ; const { asInternalUser } = client ; const resp = await asInternalUser . search < T > ( { ... searchParams , index : ML_RESULTS_INDEX_PATTERN , } ) ; return resp ; } return { anomalySearch } ; }