import { get } from 'lodash' ; import { ml } from '../../../../services/ml_api_service' ; import { RuntimeMappings } from '../../../../../../common/types/fields' ; import { IndicesOptions } from '../../../../../../common/types/anomaly_detection_jobs' ; interface CategoryResults { success : boolean ; results : string [ ] ; } export function getCategoryFields ( indexPatternName , fieldName , size , query , runtimeMappings ? , indicesOptions ? ) { return new Promise ( ( resolve , reject ) => { ml . esSearch ( { index : indexPatternName , size : 0 , body : { query , aggs : { catFields : { terms : { field : fieldName , size , } , } , } , ... ( runtimeMappings !== undefined ? { runtime_mappings : runtimeMappings } : { } ) , } , ... ( indicesOptions ?? { } ) , } ) . then ( ( resp ) => { const catFields = get ( resp , [ 'aggregations' , 'catFields' , 'buckets' ] , [ ] ) ; resolve ( { success : true , results : catFields . map ( ( f ) => f . key ) , } ) ; } ) . catch ( ( resp ) => { reject ( resp ) ; } ) ; } ) ; }