import { defaults , keyBy , sortBy } from 'lodash' ; import { LegacyAPICaller } from 'opensearch-dashboards/server' ; import { callFieldCapsApi } from '../opensearch_api' ; import { FieldCapsResponse , readFieldCapsResponse } from './field_caps_response' ; import { mergeOverrides } from './overrides' ; import { FieldDescriptor } from '../../index_patterns_fetcher' ; param { Function } callCluster param { Array } indices param { Array } metaFields param { Object } fieldCapsOptions return { Promise < Array < FieldDescriptor > > } export async function getFieldCapabilities ( callCluster , indices = [ ] , metaFields = [ ] , fieldCapsOptions ? ) { const opensearchFieldCaps = await callFieldCapsApi ( callCluster , indices , fieldCapsOptions ) ; const fieldsFromFieldCapsByName = keyBy ( readFieldCapsResponse ( opensearchFieldCaps ) , 'name' ) ; const allFieldsUnsorted = Object . keys ( fieldsFromFieldCapsByName ) . filter ( ( name ) => ! name . startsWith ( '_' ) ) . concat ( metaFields ) . reduce < { names : string [ ] ; hash : Record < string , string > ; } > ( ( agg , value ) => { if ( agg . hash [ value ] != null ) { return agg ; } else { agg . hash [ value ] = value ; agg . names . push ( value ) ; return agg ; } } , { names : [ ] , hash : { } } ) . names . map < FieldDescriptor > ( ( name ) => defaults ( { } , fieldsFromFieldCapsByName [ name ] , { name , type : 'string' , searchable : false , aggregatable : false , readFromDocValues : false , } ) ) . map ( mergeOverrides ) ; return sortBy ( allFieldsUnsorted , 'name' ) ; }