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 } from '../../serialization' ; import { catchRetryableEsClientErrors , RetryableEsClientError , } from './catch_retryable_es_client_errors' ; import { pitKeepAlive } from './open_pit' ; internal export interface ReadWithPit { outdatedDocuments : SavedObjectsRawDoc [ ] ; readonly lastHitSortValue : number [ ] | undefined ; readonly totalHits : number | undefined ; } internal export interface ReadWithPitParams { client : ElasticsearchClient ; pitId : string ; query : estypes . QueryDslQueryContainer ; batchSize : number ; searchAfter ? : number [ ] ; seqNoPrimaryTerm ? : boolean ; } export const readWithPit = ( { client , pitId , query , batchSize , searchAfter , seqNoPrimaryTerm , } ) : TaskEither . TaskEither < RetryableEsClientError , ReadWithPit > => ( ) => { return client . search < SavedObjectsRawDoc > ( { seq_no_primary_term : seqNoPrimaryTerm , body : { sort : { _shard_doc : { order : 'asc' } , } , pit : { id : pitId , keep_alive : pitKeepAlive } , size : batchSize , search_after : searchAfter , /**
             * We want to know how many documents we need to process so we can log the progress.
             * But we also want to increase the performance of these requests,
             * so we ask ES to report the total count only on the first request (when searchAfter does not exist)
             */ track_total_hits : typeof searchAfter === 'undefined' , query , } , } ) . then ( ( response ) => { const totalHits = typeof response . body . hits . total === 'number' ? response . body . hits . total : response . body . hits . total ?. value ; const hits = response . body . hits . hits ; if ( hits . length > 0 ) { return Either . right ( { outdatedDocuments : hits as SavedObjectsRawDoc [ ] , lastHitSortValue : hits [ hits . length - 1 ] . sort as number [ ] , totalHits , } ) ; } return Either . right ( { outdatedDocuments : [ ] , lastHitSortValue : undefined , totalHits , } ) ; } ) . catch ( catchRetryableEsClientErrors ) ; } ;