import { Observable } from 'rxjs' ; import { IEsSearchRequest } from 'src/plugins/data/server' ; import { schema } from '@kbn/config-schema' ; import { IEsSearchResponse } from 'src/plugins/data/common' ; import type { DataRequestHandlerContext } from 'src/plugins/data/server' ; import type { IRouter } from 'src/core/server' ; import { SERVER_SEARCH_ROUTE_PATH } from '../../common' ; export function registerServerSearchRoute ( router ) { router . get ( { path : SERVER_SEARCH_ROUTE_PATH , validate : { query : schema . object ( { index : schema . maybe ( schema . string ( ) ) , field : schema . maybe ( schema . string ( ) ) , } ) , } , } , async ( context , request , response ) => { const { index , field } = request . query ; const abortSignal = getRequestAbortedSignal ( request . events . aborted$ ) ; try { const res = await context . search ! . search ( { params : { index , body : { aggs : { '1' : { avg : { field , } , } , } , } , } , } as IEsSearchRequest , { abortSignal } ) . toPromise ( ) ; return response . ok ( { body : { aggs : ( res as IEsSearchResponse ) . rawResponse . aggregations , } , } ) ; } catch ( e ) { return response . customError ( { statusCode : e . statusCode ?? 500 , body : { message : e . message , } , } ) ; } } ) ; } function getRequestAbortedSignal ( aborted$ ) { const controller = new AbortController ( ) ; aborted$ . subscribe ( ( ) => controller . abort ( ) ) ; return controller . signal ; }