import { first } from 'rxjs/operators' ; import { SharedGlobalConfig , Logger } from 'opensearch-dashboards/server' ; import { SearchResponse } from 'elasticsearch' ; import { Observable } from 'rxjs' ; import { ApiResponse } from '@elastic/elasticsearch' ; import { SearchUsage } from '../collectors/usage' ; import { toSnakeCase } from './to_snake_case' ; import { ISearchStrategy , getDefaultSearchParams , getTotalLoaded , getShardTimeout , shimAbortSignal , } from '..' ; export const opensearchSearchStrategyProvider = ( config$ , logger , usage ? ) : ISearchStrategy => { return { search : async ( context , request , options ) => { logger . debug ( `search ${ request . params ?. index }` ) ; const config = await config$ . pipe ( first ( ) ) . toPromise ( ) ; const uiSettingsClient = await context . core . uiSettings . client ; if ( ! ! request . indexType ) { throw new Error ( `Unsupported index pattern type ${ request . indexType }` ) ; } const { ignoreThrottled , ... defaultParams } = await getDefaultSearchParams ( uiSettingsClient ) ; const params = toSnakeCase ( { ... defaultParams , ... getShardTimeout ( config ) , ... request . params , } ) ; try { const promise = shimAbortSignal ( context . core . opensearch . client . asCurrentUser . search ( params ) , options ?. abortSignal ) ; const { body : rawResponse } = ( await promise ) as ApiResponse < SearchResponse < any > > ; if ( usage ) usage . trackSuccess ( rawResponse . took ) ; return { isPartial : false , isRunning : false , rawResponse , ... getTotalLoaded ( rawResponse . _shards ) , } ; } catch ( e ) { if ( usage ) usage . trackError ( ) ; throw e ; } } , } ; } ;