import { SearchHit } from '../../../../../../../src/core/types/elasticsearch' ; import { SERVICE_NAME , SERVICE_ENVIRONMENT , } from '../../../../common/elasticsearch_fieldnames' ; import { Setup } from '../../../lib/helpers/setup_request' ; import { AgentConfiguration } from '../../../../common/agent_configuration/configuration_types' ; import { convertConfigSettingsToString } from './convert_settings_to_string' ; export async function searchConfigurations ( { service , setup , } ) { const { internalClient , indices } = setup ; const serviceNameFilter = service . name ? [ { constant_score : { filter : { term : { [ SERVICE_NAME ] : service . name } } , boost : 2 , } , } , ] : [ ] ; const environmentFilter = service . environment ? [ { constant_score : { filter : { term : { [ SERVICE_ENVIRONMENT ] : service . environment } } , boost : 1 , } , } , ] : [ ] ; const params = { index : indices . apmAgentConfigurationIndex , body : { query : { bool : { minimum_should_match : 2 , should : [ ... serviceNameFilter , ... environmentFilter , { bool : { must_not : [ { exists : { field : SERVICE_NAME } } ] } } , { bool : { must_not : [ { exists : { field : SERVICE_ENVIRONMENT } } ] , } , } , ] , } , } , } , } ; const resp = await internalClient . search < AgentConfiguration , typeof params > ( 'search_agent_configurations' , params ) ; const hit = resp . hits . hits [ 0 ] as SearchHit < AgentConfiguration > | undefined ; if ( ! hit ) { return ; } return convertConfigSettingsToString ( hit ) ; }