-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
search_service.ts
339 lines (314 loc) · 11.7 KB
/
search_service.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/
import { i18n } from '@kbn/i18n';
import { estypes } from '@elastic/elasticsearch';
import { handleWarnings } from '@kbn/search-response-warnings';
import {
CoreSetup,
CoreStart,
Plugin,
PluginInitializerContext,
StartServicesAccessor,
} from '@kbn/core/public';
import type { ISearchGeneric } from '@kbn/search-types';
import { RequestAdapter } from '@kbn/inspector-plugin/common/adapters/request';
import { DataViewsContract } from '@kbn/data-views-plugin/common';
import { ExpressionsSetup } from '@kbn/expressions-plugin/public';
import { FieldFormatsStart } from '@kbn/field-formats-plugin/public';
import { toMountPoint } from '@kbn/react-kibana-mount';
import { Storage } from '@kbn/kibana-utils-plugin/public';
import { ManagementSetup } from '@kbn/management-plugin/public';
import { ScreenshotModePluginStart } from '@kbn/screenshot-mode-plugin/public';
import { UsageCollectionSetup } from '@kbn/usage-collection-plugin/public';
import type { Start as InspectorStartContract } from '@kbn/inspector-plugin/public';
import React from 'react';
import { BehaviorSubject } from 'rxjs';
import {
cidrFunction,
dateRangeFunction,
eqlRawResponse,
esRawResponse,
existsFilterFunction,
extendedBoundsFunction,
fieldFunction,
geoBoundingBoxFunction,
geoPointFunction,
ipPrefixFunction,
ipRangeFunction,
kibana,
kibanaFilterFunction,
kibanaTimerangeFunction,
kqlFunction,
luceneFunction,
numericalRangeFunction,
phraseFilterFunction,
queryFilterFunction,
rangeFilterFunction,
rangeFunction,
removeFilterFunction,
SearchSourceDependencies,
SearchSourceService,
selectFilterFunction,
} from '../../common/search';
import {
getShardDelayBucketAgg,
SHARD_DELAY_AGG_NAME,
} from '../../common/search/aggs/buckets/shard_delay';
import { aggShardDelay } from '../../common/search/aggs/buckets/shard_delay_fn';
import type { ConfigSchema } from '../../server/config';
import { NowProviderInternalContract } from '../now_provider';
import { DataPublicPluginStart, DataStartDependencies } from '../types';
import { AggsService } from './aggs';
import { createUsageCollector, SearchUsageCollector } from './collectors';
import { getEql, getEsaggs, getEsdsl, getEssql, getEsql } from './expressions';
import { ISearchInterceptor, SearchInterceptor } from './search_interceptor';
import { ISessionsClient, ISessionService, SessionsClient, SessionService } from './session';
import { registerSearchSessionsMgmt } from './session/sessions_mgmt';
import { createConnectedSearchSessionIndicator } from './session/session_indicator';
import { ISearchSetup, ISearchStart } from './types';
/** @internal */
export interface SearchServiceSetupDependencies {
expressions: ExpressionsSetup;
usageCollection?: UsageCollectionSetup;
management: ManagementSetup;
nowProvider: NowProviderInternalContract;
}
/** @internal */
export interface SearchServiceStartDependencies {
fieldFormats: FieldFormatsStart;
indexPatterns: DataViewsContract;
inspector: InspectorStartContract;
screenshotMode: ScreenshotModePluginStart;
scriptedFieldsEnabled: boolean;
}
export class SearchService implements Plugin<ISearchSetup, ISearchStart> {
private readonly aggsService = new AggsService();
private readonly searchSourceService = new SearchSourceService();
private searchInterceptor!: ISearchInterceptor;
private usageCollector?: SearchUsageCollector;
private sessionService!: ISessionService;
private sessionsClient!: ISessionsClient;
constructor(private initializerContext: PluginInitializerContext<ConfigSchema>) {}
public setup(
core: CoreSetup,
{ expressions, usageCollection, nowProvider, management }: SearchServiceSetupDependencies
): ISearchSetup {
const { http, getStartServices, notifications, uiSettings, executionContext } = core;
this.usageCollector = createUsageCollector(getStartServices, usageCollection);
this.sessionsClient = new SessionsClient({ http });
this.sessionService = new SessionService(
this.initializerContext,
getStartServices,
this.sessionsClient,
nowProvider,
this.usageCollector
);
/**
* A global object that intercepts all searches and provides convenience methods for cancelling
* all pending search requests, as well as getting the number of pending search requests.
*/
this.searchInterceptor = new SearchInterceptor({
toasts: notifications.toasts,
executionContext,
http,
uiSettings,
startServices: getStartServices(),
usageCollector: this.usageCollector!,
session: this.sessionService,
searchConfig: this.initializerContext.config.get().search,
});
expressions.registerFunction(
getEsaggs({ getStartServices } as {
getStartServices: StartServicesAccessor<DataStartDependencies, DataPublicPluginStart>;
})
);
expressions.registerFunction(kibana);
expressions.registerFunction(cidrFunction);
expressions.registerFunction(dateRangeFunction);
expressions.registerFunction(extendedBoundsFunction);
expressions.registerFunction(ipPrefixFunction);
expressions.registerFunction(ipRangeFunction);
expressions.registerFunction(luceneFunction);
expressions.registerFunction(kqlFunction);
expressions.registerFunction(kibanaTimerangeFunction);
expressions.registerFunction(fieldFunction);
expressions.registerFunction(numericalRangeFunction);
expressions.registerFunction(geoBoundingBoxFunction);
expressions.registerFunction(geoPointFunction);
expressions.registerFunction(rangeFunction);
expressions.registerFunction(kibanaFilterFunction);
expressions.registerFunction(existsFilterFunction);
expressions.registerFunction(queryFilterFunction);
expressions.registerFunction(rangeFilterFunction);
expressions.registerFunction(removeFilterFunction);
expressions.registerFunction(selectFilterFunction);
expressions.registerFunction(phraseFilterFunction);
expressions.registerFunction(
getEsdsl({ getStartServices } as {
getStartServices: StartServicesAccessor<DataStartDependencies, DataPublicPluginStart>;
})
);
expressions.registerFunction(
getEssql({ getStartServices } as {
getStartServices: StartServicesAccessor<DataStartDependencies, DataPublicPluginStart>;
})
);
expressions.registerFunction(
getEsql({ getStartServices } as {
getStartServices: StartServicesAccessor<DataStartDependencies, DataPublicPluginStart>;
})
);
expressions.registerFunction(
getEql({ getStartServices } as {
getStartServices: StartServicesAccessor<DataStartDependencies, DataPublicPluginStart>;
})
);
expressions.registerType(esRawResponse);
expressions.registerType(eqlRawResponse);
const aggs = this.aggsService.setup({
uiSettings,
registerFunction: expressions.registerFunction,
nowProvider,
});
if (this.initializerContext.config.get().search.aggs.shardDelay.enabled) {
aggs.types.registerBucket(SHARD_DELAY_AGG_NAME, getShardDelayBucketAgg);
expressions.registerFunction(aggShardDelay);
}
const config = this.initializerContext.config.get<ConfigSchema>();
if (config.search.sessions.enabled) {
const sessionsConfig = config.search.sessions;
registerSearchSessionsMgmt(
core as CoreSetup<DataStartDependencies>,
{
searchUsageCollector: this.usageCollector!,
sessionsClient: this.sessionsClient,
management,
},
sessionsConfig,
this.initializerContext.env.packageInfo.version
);
}
return {
aggs,
usageCollector: this.usageCollector!,
session: this.sessionService,
sessionsClient: this.sessionsClient,
};
}
public start(
{ http, uiSettings, chrome, application, notifications, ...startServices }: CoreStart,
{
fieldFormats,
indexPatterns,
inspector,
screenshotMode,
scriptedFieldsEnabled,
}: SearchServiceStartDependencies
): ISearchStart {
const search = ((request, options = {}) => {
return this.searchInterceptor.search(request, options);
}) as ISearchGeneric;
const loadingCount$ = new BehaviorSubject(0);
http.addLoadingCountSource(loadingCount$);
const aggs = this.aggsService.start({ fieldFormats, indexPatterns });
const warningsServices = {
inspector,
notifications,
...startServices,
};
const searchSourceDependencies: SearchSourceDependencies = {
aggs,
getConfig: uiSettings.get.bind(uiSettings),
search,
dataViews: indexPatterns,
onResponse: (request, response, options) => {
if (!options.disableWarningToasts) {
const { rawResponse } = response;
const requestName = options.inspector?.title
? options.inspector.title
: i18n.translate('data.searchService.anonymousRequestTitle', {
defaultMessage: 'Request',
});
const requestAdapter = options.inspector?.adapter
? options.inspector?.adapter
: new RequestAdapter();
if (!options.inspector?.adapter) {
const requestResponder = requestAdapter.start(requestName, {
id: request.id,
});
requestResponder.json(request.body);
requestResponder.ok({ json: response });
}
handleWarnings({
request: request.body as estypes.SearchRequest,
requestAdapter,
requestId: request.id,
requestName,
response: rawResponse,
services: warningsServices,
});
}
return response;
},
scriptedFieldsEnabled,
};
const config = this.initializerContext.config.get();
if (config.search.sessions.enabled) {
chrome.setBreadcrumbsAppendExtension({
content: toMountPoint(
React.createElement(
createConnectedSearchSessionIndicator({
sessionService: this.sessionService,
application,
basePath: http.basePath,
storage: new Storage(window.localStorage),
usageCollector: this.usageCollector,
tourDisabled: screenshotMode.isScreenshotMode(),
})
),
startServices
),
});
}
return {
aggs,
search,
showError: (e) => {
this.searchInterceptor.showError(e);
},
showWarnings: (adapter, callback) => {
adapter?.getRequests().forEach((request) => {
const rawResponse = (
request.response?.json as { rawResponse: estypes.SearchResponse | undefined }
)?.rawResponse;
if (!rawResponse) {
return;
}
handleWarnings({
callback,
request: request.json as estypes.SearchRequest,
requestAdapter: adapter,
requestId: request.id,
requestName: request.name,
response: rawResponse,
services: warningsServices,
});
});
},
session: this.sessionService,
sessionsClient: this.sessionsClient,
searchSource: this.searchSourceService.start(indexPatterns, searchSourceDependencies),
};
}
public stop() {
this.aggsService.stop();
this.searchSourceService.stop();
this.searchInterceptor.stop();
}
}