import * as Either from 'fp-ts/lib/Either' ; import * as TaskEither from 'fp-ts/lib/TaskEither' ; import * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey' ; import { ElasticsearchClient } from '../../../elasticsearch' ; import type { SavedObjectsRawDoc , SavedObjectsRawDocSource } from '../../serialization' ; import { catchRetryableEsClientErrors , RetryableEsClientError , } from './catch_retryable_es_client_errors' ; internal export interface SearchResponse { outdatedDocuments : SavedObjectsRawDoc [ ] ; } export interface SearchForOutdatedDocumentsOptions { batchSize : number ; targetIndex : string ; outdatedDocumentsQuery ? : estypes . QueryDslQueryContainer ; } /**
 * Search for outdated saved object documents with the provided query. Will
 * return one batch of documents. Searching should be repeated until no more
 * outdated documents can be found.
 *
 * Used for testing only
 */ export const searchForOutdatedDocuments = ( client , options ) : TaskEither . TaskEither < RetryableEsClientError , SearchResponse > => ( ) => { return client . search < SavedObjectsRawDocSource > ( { index : options . targetIndex , seq_no_primary_term : true , size : options . batchSize , body : { query : options . outdatedDocumentsQuery , sort : [ '_doc' ] , } , allow_no_indices : false , allow_partial_search_results : false , track_total_hits : false , filter_path : [ 'hits.hits._id' , 'hits.hits._source' , 'hits.hits._seq_no' , 'hits.hits._primary_term' , ] , } ) . then ( ( res ) => Either . right ( { outdatedDocuments : ( res . body . hits ?. hits as SavedObjectsRawDoc [ ] ) ?? [ ] } ) ) . catch ( catchRetryableEsClientErrors ) ; } ;