-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
Copy pathsearch_configurations.ts
79 lines (71 loc) · 2.3 KB
/
search_configurations.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { ESSearchHit } from '../../../../typings/elasticsearch';
import {
SERVICE_NAME,
SERVICE_ENVIRONMENT,
} from '../../../../common/elasticsearch_fieldnames';
import { Setup } from '../../helpers/setup_request';
import { AgentConfiguration } from '../../../../common/agent_configuration/configuration_types';
import { convertConfigSettingsToString } from './convert_settings_to_string';
export async function searchConfigurations({
service,
setup,
}: {
service: AgentConfiguration['service'];
setup: Setup;
}) {
const { internalClient, indices } = setup;
// In the following `constant_score` is being used to disable IDF calculation (where frequency of a term influences scoring).
// Additionally a boost has been added to service.name to ensure it scores higher.
// If there is tie between a config with a matching service.name and a config with a matching environment, the config that matches service.name wins
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>(
params
);
const hit = resp.hits.hits[0] as ESSearchHit<AgentConfiguration> | undefined;
if (!hit) {
return;
}
return convertConfigSettingsToString(hit);
}