diff --git a/x-pack/solutions/observability/plugins/apm/common/utils/environment_query.test.ts b/x-pack/solutions/observability/plugins/apm/common/utils/environment_query.test.ts index b996368096702..f127e845ac2b4 100644 --- a/x-pack/solutions/observability/plugins/apm/common/utils/environment_query.test.ts +++ b/x-pack/solutions/observability/plugins/apm/common/utils/environment_query.test.ts @@ -14,7 +14,23 @@ describe('environmentQuery', () => { it('returns ENVIRONMENT_NOT_DEFINED', () => { expect(environmentQuery('')).toEqual([ { - term: { [SERVICE_ENVIRONMENT]: ENVIRONMENT_NOT_DEFINED.value }, + bool: { + should: [ + { + term: { [SERVICE_ENVIRONMENT]: ENVIRONMENT_NOT_DEFINED.value }, + }, + { + bool: { + must_not: [ + { + exists: { field: SERVICE_ENVIRONMENT }, + }, + ], + }, + }, + ], + minimum_should_match: 1, + }, }, ]); }); @@ -35,7 +51,23 @@ describe('environmentQuery', () => { it('creates a query for missing service environments', () => { expect(environmentQuery(ENVIRONMENT_NOT_DEFINED.value)).toEqual([ { - term: { [SERVICE_ENVIRONMENT]: ENVIRONMENT_NOT_DEFINED.value }, + bool: { + should: [ + { + term: { [SERVICE_ENVIRONMENT]: ENVIRONMENT_NOT_DEFINED.value }, + }, + { + bool: { + must_not: [ + { + exists: { field: SERVICE_ENVIRONMENT }, + }, + ], + }, + }, + ], + minimum_should_match: 1, + }, }, ]); }); diff --git a/x-pack/solutions/observability/plugins/apm/common/utils/environment_query.ts b/x-pack/solutions/observability/plugins/apm/common/utils/environment_query.ts index 7859c64a8b306..638999dc371cb 100644 --- a/x-pack/solutions/observability/plugins/apm/common/utils/environment_query.ts +++ b/x-pack/solutions/observability/plugins/apm/common/utils/environment_query.ts @@ -19,22 +19,27 @@ export function environmentQuery( } if (!environment || environment === ENVIRONMENT_NOT_DEFINED.value) { - return [{ term: { [field]: ENVIRONMENT_NOT_DEFINED.value } }]; - } - - return [{ term: { [field]: environment } }]; -} - -export function environmentQueryAnnotations( - environment: string | undefined, - field: string = SERVICE_ENVIRONMENT -): QueryDslQueryContainer[] { - if (!environment || environment === ENVIRONMENT_ALL.value) { - return []; - } - - if (environment === ENVIRONMENT_NOT_DEFINED.value) { - return [{ bool: { must_not: { exists: { field } } } }]; + return [ + { + bool: { + should: [ + { + term: { [field]: ENVIRONMENT_NOT_DEFINED.value }, + }, + { + bool: { + must_not: [ + { + exists: { field }, + }, + ], + }, + }, + ], + minimum_should_match: 1, + }, + }, + ]; } return [{ term: { [field]: environment } }]; diff --git a/x-pack/solutions/observability/plugins/apm/server/routes/services/annotations/get_derived_service_annotations.ts b/x-pack/solutions/observability/plugins/apm/server/routes/services/annotations/get_derived_service_annotations.ts index 5fba0d1a9705a..73f53fd650782 100644 --- a/x-pack/solutions/observability/plugins/apm/server/routes/services/annotations/get_derived_service_annotations.ts +++ b/x-pack/solutions/observability/plugins/apm/server/routes/services/annotations/get_derived_service_annotations.ts @@ -14,7 +14,7 @@ import { isFiniteNumber } from '../../../../common/utils/is_finite_number'; import type { Annotation } from '../../../../common/annotations'; import { AnnotationType } from '../../../../common/annotations'; import { AT_TIMESTAMP, SERVICE_NAME, SERVICE_VERSION } from '../../../../common/es_fields/apm'; -import { environmentQueryAnnotations } from '../../../../common/utils/environment_query'; +import { environmentQuery } from '../../../../common/utils/environment_query'; import { getBackwardCompatibleDocumentTypeFilter, getProcessorEventForTransactions, @@ -39,7 +39,7 @@ export async function getDerivedServiceAnnotations({ const filter: ESFilter[] = [ { term: { [SERVICE_NAME]: serviceName } }, ...getBackwardCompatibleDocumentTypeFilter(searchAggregatedTransactions), - ...environmentQueryAnnotations(environment), + ...environmentQuery(environment), ]; const versions = diff --git a/x-pack/solutions/observability/plugins/apm/server/routes/services/annotations/get_stored_annotations.ts b/x-pack/solutions/observability/plugins/apm/server/routes/services/annotations/get_stored_annotations.ts index fdd6b8b7887a1..8a74142b2b7ce 100644 --- a/x-pack/solutions/observability/plugins/apm/server/routes/services/annotations/get_stored_annotations.ts +++ b/x-pack/solutions/observability/plugins/apm/server/routes/services/annotations/get_stored_annotations.ts @@ -15,7 +15,7 @@ import { import type { ESSearchResponse } from '@kbn/es-types'; import type { Annotation as ESAnnotation } from '@kbn/observability-plugin/common/annotations'; import type { ScopedAnnotationsClient } from '@kbn/observability-plugin/server'; -import { environmentQueryAnnotations } from '../../../../common/utils/environment_query'; +import { environmentQuery } from '../../../../common/utils/environment_query'; import type { Annotation } from '../../../../common/annotations'; import { AnnotationType } from '../../../../common/annotations'; import { SERVICE_NAME } from '../../../../common/es_fields/apm'; @@ -48,7 +48,7 @@ export function getStoredAnnotations({ { term: { tags: 'apm' } }, { term: { [SERVICE_NAME]: serviceName } }, ...rangeQuery(start, end), - ...environmentQueryAnnotations(environment), + ...environmentQuery(environment), ], }, },