-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
search_source.ts
576 lines (513 loc) · 18.4 KB
/
search_source.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/**
* @name SearchSource
*
* @description A promise-based stream of search results that can inherit from other search sources.
*
* Because filters/queries in Kibana have different levels of persistence and come from different
* places, it is important to keep track of where filters come from for when they are saved back to
* the savedObject store in the Kibana index. To do this, we create trees of searchSource objects
* that can have associated query parameters (index, query, filter, etc) which can also inherit from
* other searchSource objects.
*
* At query time, all of the searchSource objects that have subscribers are "flattened", at which
* point the query params from the searchSource are collected while traversing up the inheritance
* chain. At each link in the chain a decision about how to merge the query params is made until a
* single set of query parameters is created for each active searchSource (a searchSource with
* subscribers).
*
* That set of query parameters is then sent to elasticsearch. This is how the filter hierarchy
* works in Kibana.
*
* Visualize, starting from a new search:
*
* - the `savedVis.searchSource` is set as the `appSearchSource`.
* - The `savedVis.searchSource` would normally inherit from the `appSearchSource`, but now it is
* upgraded to inherit from the `rootSearchSource`.
* - Any interaction with the visualization will still apply filters to the `appSearchSource`, so
* they will be stored directly on the `savedVis.searchSource`.
* - Any interaction with the time filter will be written to the `rootSearchSource`, so those
* filters will not be saved by the `savedVis`.
* - When the `savedVis` is saved to elasticsearch, it takes with it all the filters that are
* defined on it directly, but none of the ones that it inherits from other places.
*
* Visualize, starting from an existing search:
*
* - The `savedVis` loads the `savedSearch` on which it is built.
* - The `savedVis.searchSource` is set to inherit from the `saveSearch.searchSource` and set as
* the `appSearchSource`.
* - The `savedSearch.searchSource`, is set to inherit from the `rootSearchSource`.
* - Then the `savedVis` is written to elasticsearch it will be flattened and only include the
* filters created in the visualize application and will reconnect the filters from the
* `savedSearch` at runtime to prevent losing the relationship
*
* Dashboard search sources:
*
* - Each panel in a dashboard has a search source.
* - The `savedDashboard` also has a searchsource, and it is set as the `appSearchSource`.
* - Each panel's search source inherits from the `appSearchSource`, meaning that they inherit from
* the dashboard search source.
* - When a filter is added to the search box, or via a visualization, it is written to the
* `appSearchSource`.
*/
import { setWith } from '@elastic/safer-lodash-set';
import { uniqueId, uniq, extend, pick, difference, omit, isObject, keys, isFunction } from 'lodash';
import { map } from 'rxjs/operators';
import { HttpStart } from 'src/core/public';
import { BehaviorSubject } from 'rxjs';
import { normalizeSortRequest } from './normalize_sort_request';
import { filterDocvalueFields } from './filter_docvalue_fields';
import { fieldWildcardFilter } from '../../../../kibana_utils/common';
import { IIndexPattern, ISearchGeneric } from '../..';
import { SearchSourceOptions, SearchSourceFields } from './types';
import {
RequestFailure,
handleResponse,
getSearchParamsFromRequest,
SearchRequest,
} from '../fetch';
import {
getEsQueryConfig,
buildEsQuery,
Filter,
UI_SETTINGS,
ISearchOptions,
} from '../../../common';
import { getHighlightRequest } from '../../../common/field_formats';
import { GetConfigFn } from '../../../common/types';
import { fetchSoon } from '../legacy';
import { extractReferences } from './extract_references';
/** @internal */
export const searchSourceRequiredUiSettings = [
'dateFormat:tz',
UI_SETTINGS.COURIER_BATCH_SEARCHES,
UI_SETTINGS.COURIER_CUSTOM_REQUEST_PREFERENCE,
UI_SETTINGS.COURIER_IGNORE_FILTER_IF_FIELD_NOT_IN_INDEX,
UI_SETTINGS.COURIER_MAX_CONCURRENT_SHARD_REQUESTS,
UI_SETTINGS.COURIER_SET_REQUEST_PREFERENCE,
UI_SETTINGS.DOC_HIGHLIGHT,
UI_SETTINGS.META_FIELDS,
UI_SETTINGS.QUERY_ALLOW_LEADING_WILDCARDS,
UI_SETTINGS.QUERY_STRING_OPTIONS,
UI_SETTINGS.SEARCH_INCLUDE_FROZEN,
UI_SETTINGS.SORT_OPTIONS,
];
export interface SearchSourceDependencies {
getConfig: GetConfigFn;
search: ISearchGeneric;
http: HttpStart;
loadingCount$: BehaviorSubject<number>;
}
/** @public **/
export class SearchSource {
private id: string = uniqueId('data_source');
private searchStrategyId?: string;
private parent?: SearchSource;
private requestStartHandlers: Array<
(searchSource: SearchSource, options?: ISearchOptions) => Promise<unknown>
> = [];
private inheritOptions: SearchSourceOptions = {};
public history: SearchRequest[] = [];
private fields: SearchSourceFields;
private readonly dependencies: SearchSourceDependencies;
constructor(fields: SearchSourceFields = {}, dependencies: SearchSourceDependencies) {
this.fields = fields;
this.dependencies = dependencies;
}
/** ***
* PUBLIC API
*****/
/**
* internal, dont use
* @param searchStrategyId
*/
setPreferredSearchStrategyId(searchStrategyId: string) {
this.searchStrategyId = searchStrategyId;
}
/**
* sets value to a single search source feild
* @param field: field name
* @param value: value for the field
*/
setField<K extends keyof SearchSourceFields>(field: K, value: SearchSourceFields[K]) {
if (value == null) {
delete this.fields[field];
} else {
this.fields[field] = value;
}
return this;
}
/**
* Internal, do not use. Overrides all search source fields with the new field array.
*
* @private
* @param newFields New field array.
*/
setFields(newFields: SearchSourceFields) {
this.fields = newFields;
return this;
}
/**
* returns search source id
*/
getId() {
return this.id;
}
/**
* returns all search source fields
*/
getFields() {
return { ...this.fields };
}
/**
* Gets a single field from the fields
*/
getField<K extends keyof SearchSourceFields>(field: K, recurse = true): SearchSourceFields[K] {
if (!recurse || this.fields[field] !== void 0) {
return this.fields[field];
}
const parent = this.getParent();
return parent && parent.getField(field);
}
/**
* Get the field from our own fields, don't traverse up the chain
*/
getOwnField<K extends keyof SearchSourceFields>(field: K): SearchSourceFields[K] {
return this.getField(field, false);
}
/**
* @deprecated Don't use.
*/
create() {
return new SearchSource({}, this.dependencies);
}
/**
* creates a copy of this search source (without its children)
*/
createCopy() {
const newSearchSource = new SearchSource({}, this.dependencies);
newSearchSource.setFields({ ...this.fields });
// when serializing the internal fields we lose the internal classes used in the index
// pattern, so we have to set it again to workaround this behavior
newSearchSource.setField('index', this.getField('index'));
newSearchSource.setParent(this.getParent());
return newSearchSource;
}
/**
* creates a new child search source
* @param options
*/
createChild(options = {}) {
const childSearchSource = new SearchSource({}, this.dependencies);
childSearchSource.setParent(this, options);
return childSearchSource;
}
/**
* Set a searchSource that this source should inherit from
* @param {SearchSource} parent - the parent searchSource
* @param {SearchSourceOptions} options - the inherit options
* @return {this} - chainable
*/
setParent(parent?: ISearchSource, options: SearchSourceOptions = {}) {
this.parent = parent as SearchSource;
this.inheritOptions = options;
return this;
}
/**
* Get the parent of this SearchSource
* @return {undefined|searchSource}
*/
getParent() {
return this.parent;
}
/**
* Fetch this source and reject the returned Promise on error
*
* @async
*/
async fetch(options: ISearchOptions = {}) {
const { getConfig } = this.dependencies;
await this.requestIsStarting(options);
const searchRequest = await this.flatten();
this.history = [searchRequest];
let response;
if (getConfig(UI_SETTINGS.COURIER_BATCH_SEARCHES)) {
response = await this.legacyFetch(searchRequest, options);
} else {
response = await this.fetch$(searchRequest, options).toPromise();
}
// TODO: Remove casting when https://github.com/elastic/elasticsearch-js/issues/1287 is resolved
if ((response as any).error) {
throw new RequestFailure(null, response);
}
return response;
}
/**
* Add a handler that will be notified whenever requests start
* @param {Function} handler
* @return {undefined}
*/
onRequestStart(
handler: (searchSource: SearchSource, options?: ISearchOptions) => Promise<unknown>
) {
this.requestStartHandlers.push(handler);
}
/**
* Returns body contents of the search request, often referred as query DSL.
*/
async getSearchRequestBody() {
const searchRequest = await this.flatten();
return searchRequest.body;
}
/**
* Completely destroy the SearchSource.
* @return {undefined}
*/
destroy() {
this.requestStartHandlers.length = 0;
}
/** ****
* PRIVATE APIS
******/
/**
* Run a search using the search service
* @return {Observable<SearchResponse<unknown>>}
*/
private fetch$(searchRequest: SearchRequest, options: ISearchOptions) {
const { search, getConfig } = this.dependencies;
const params = getSearchParamsFromRequest(searchRequest, {
getConfig,
});
return search({ params, indexType: searchRequest.indexType }, options).pipe(
map(({ rawResponse }) => handleResponse(searchRequest, rawResponse))
);
}
/**
* Run a search using the search service
* @return {Promise<SearchResponse<unknown>>}
*/
private async legacyFetch(searchRequest: SearchRequest, options: ISearchOptions) {
const { http, getConfig, loadingCount$ } = this.dependencies;
return await fetchSoon(
searchRequest,
{
...(this.searchStrategyId && { searchStrategyId: this.searchStrategyId }),
...options,
},
{
http,
config: { get: getConfig },
loadingCount$,
}
);
}
/**
* Called by requests of this search source when they are started
* @param options
* @return {Promise<undefined>}
*/
private requestIsStarting(options: ISearchOptions = {}) {
const handlers = [...this.requestStartHandlers];
// If callParentStartHandlers has been set to true, we also call all
// handlers of parent search sources.
if (this.inheritOptions.callParentStartHandlers) {
let searchSource = this.getParent();
while (searchSource) {
handlers.push(...searchSource.requestStartHandlers);
searchSource = searchSource.getParent();
}
}
return Promise.all(handlers.map((fn) => fn(this, options)));
}
/**
* Used to merge properties into the data within ._flatten().
* The data is passed in and modified by the function
*
* @param {object} data - the current merged data
* @param {*} val - the value at `key`
* @param {*} key - The key of `val`
* @return {undefined}
*/
private mergeProp<K extends keyof SearchSourceFields>(
data: SearchRequest,
val: SearchSourceFields[K],
key: K
) {
val = typeof val === 'function' ? val(this) : val;
if (val == null || !key) return;
const addToRoot = (rootKey: string, value: any) => {
data[rootKey] = value;
};
/**
* Add the key and val to the body of the request
*/
const addToBody = (bodyKey: string, value: any) => {
// ignore if we already have a value
if (data.body[bodyKey] == null) {
data.body[bodyKey] = value;
}
};
const { getConfig } = this.dependencies;
switch (key) {
case 'filter':
return addToRoot('filters', (data.filters || []).concat(val));
case 'query':
return addToRoot(key, (data[key] || []).concat(val));
case 'fields':
const fields = uniq((data[key] || []).concat(val));
return addToRoot(key, fields);
case 'index':
case 'type':
case 'highlightAll':
return key && data[key] == null && addToRoot(key, val);
case 'searchAfter':
return addToBody('search_after', val);
case 'source':
return addToBody('_source', val);
case 'sort':
const sort = normalizeSortRequest(
val,
this.getField('index'),
getConfig(UI_SETTINGS.SORT_OPTIONS)
);
return addToBody(key, sort);
default:
return addToBody(key, val);
}
}
/**
* Walk the inheritance chain of a source and return its
* flat representation (taking into account merging rules)
* @returns {Promise}
* @resolved {Object|null} - the flat data of the SearchSource
*/
private mergeProps(root = this, searchRequest: SearchRequest = { body: {} }) {
Object.entries(this.fields).forEach(([key, value]) => {
this.mergeProp(searchRequest, value, key as keyof SearchSourceFields);
});
if (this.parent) {
this.parent.mergeProps(root, searchRequest);
}
return searchRequest;
}
private getIndexType(index?: IIndexPattern) {
if (this.searchStrategyId) {
return this.searchStrategyId === 'default' ? undefined : this.searchStrategyId;
} else {
return index?.type;
}
}
private flatten() {
const searchRequest = this.mergeProps();
searchRequest.body = searchRequest.body || {};
const { body, index, fields, query, filters, highlightAll } = searchRequest;
searchRequest.indexType = this.getIndexType(index);
const computedFields = index ? index.getComputedFields() : {};
body.stored_fields = computedFields.storedFields;
body.script_fields = body.script_fields || {};
extend(body.script_fields, computedFields.scriptFields);
const defaultDocValueFields = computedFields.docvalueFields
? computedFields.docvalueFields
: [];
body.docvalue_fields = body.docvalue_fields || defaultDocValueFields;
if (!body.hasOwnProperty('_source') && index) {
body._source = index.getSourceFiltering();
}
const { getConfig } = this.dependencies;
if (body._source) {
// exclude source fields for this index pattern specified by the user
const filter = fieldWildcardFilter(body._source.excludes, getConfig(UI_SETTINGS.META_FIELDS));
body.docvalue_fields = body.docvalue_fields.filter((docvalueField: any) =>
filter(docvalueField.field)
);
}
// if we only want to search for certain fields
if (fields) {
// filter out the docvalue_fields, and script_fields to only include those that we are concerned with
body.docvalue_fields = filterDocvalueFields(body.docvalue_fields, fields);
body.script_fields = pick(body.script_fields, fields);
// request the remaining fields from both stored_fields and _source
const remainingFields = difference(fields, keys(body.script_fields));
body.stored_fields = remainingFields;
setWith(body, '_source.includes', remainingFields, (nsValue) =>
isObject(nsValue) ? {} : nsValue
);
}
const esQueryConfigs = getEsQueryConfig({ get: getConfig });
body.query = buildEsQuery(index, query, filters, esQueryConfigs);
if (highlightAll && body.query) {
body.highlight = getHighlightRequest(body.query, getConfig(UI_SETTINGS.DOC_HIGHLIGHT));
delete searchRequest.highlightAll;
}
return searchRequest;
}
/**
* serializes search source fields (which can later be passed to {@link ISearchStartSearchSource})
*/
public getSerializedFields() {
const { filter: originalFilters, ...searchSourceFields } = omit(this.getFields(), [
'sort',
'size',
]);
let serializedSearchSourceFields: SearchSourceFields = {
...searchSourceFields,
index: (searchSourceFields.index ? searchSourceFields.index.id : undefined) as any,
};
if (originalFilters) {
const filters = this.getFilters(originalFilters);
serializedSearchSourceFields = {
...serializedSearchSourceFields,
filter: filters,
};
}
return serializedSearchSourceFields;
}
/**
* Serializes the instance to a JSON string and a set of referenced objects.
* Use this method to get a representation of the search source which can be stored in a saved object.
*
* The references returned by this function can be mixed with other references in the same object,
* however make sure there are no name-collisions. The references will be named `kibanaSavedObjectMeta.searchSourceJSON.index`
* and `kibanaSavedObjectMeta.searchSourceJSON.filter[<number>].meta.index`.
*
* Using `createSearchSource`, the instance can be re-created.
* @public */
public serialize() {
const [searchSourceFields, references] = extractReferences(this.getSerializedFields());
return { searchSourceJSON: JSON.stringify(searchSourceFields), references };
}
private getFilters(filterField: SearchSourceFields['filter']): Filter[] {
if (!filterField) {
return [];
}
if (Array.isArray(filterField)) {
return filterField;
}
if (isFunction(filterField)) {
return this.getFilters(filterField());
}
return [filterField];
}
}
/**
* search source interface
* @public
*/
export type ISearchSource = Pick<SearchSource, keyof SearchSource>;