-
Notifications
You must be signed in to change notification settings - Fork 849
/
search_service.ts
207 lines (186 loc) · 7.27 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
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Any modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/
/*
* 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.
*/
import { Plugin, CoreSetup, CoreStart, PluginInitializerContext } from 'src/core/public';
import { BehaviorSubject } from 'rxjs';
import { ISearchSetup, ISearchStart, SearchEnhancements } from './types';
import { handleResponse } from './fetch';
import {
IOpenSearchSearchRequest,
IOpenSearchSearchResponse,
IOpenSearchDashboardsSearchRequest,
IOpenSearchDashboardsSearchResponse,
ISearchGeneric,
ISearchOptions,
SearchSourceService,
SearchSourceDependencies,
} from '../../common/search';
import { getCallMsearch } from './legacy';
import { AggsService, AggsStartDependencies } from './aggs';
import { IndexPatternsContract } from '../index_patterns/index_patterns';
import { ISearchInterceptor, SearchInterceptor } from './search_interceptor';
import { SearchUsageCollector, createUsageCollector } from './collectors';
import { UsageCollectionSetup } from '../../../usage_collection/public';
import { opensearchdsl, opensearchRawResponse } from './expressions';
import { ExpressionsSetup } from '../../../expressions/public';
import { ConfigSchema } from '../../config';
import {
SHARD_DELAY_AGG_NAME,
getShardDelayBucketAgg,
} from '../../common/search/aggs/buckets/shard_delay';
import { aggShardDelay } from '../../common/search/aggs/buckets/shard_delay_fn';
import {
DataFrameService,
IDataFrame,
IDataFrameResponse,
createDataFrameCache,
} from '../../common/data_frames';
import { getQueryService } from '../services';
import { UI_SETTINGS } from '../../common';
/** @internal */
export interface SearchServiceSetupDependencies {
expressions: ExpressionsSetup;
usageCollection?: UsageCollectionSetup;
}
/** @internal */
export interface SearchServiceStartDependencies {
fieldFormats: AggsStartDependencies['fieldFormats'];
indexPatterns: IndexPatternsContract;
}
export class SearchService implements Plugin<ISearchSetup, ISearchStart> {
private readonly aggsService = new AggsService();
private readonly searchSourceService = new SearchSourceService();
private readonly dfCache = createDataFrameCache();
private searchInterceptor!: ISearchInterceptor;
private defaultSearchInterceptor!: ISearchInterceptor;
private usageCollector?: SearchUsageCollector;
constructor(private initializerContext: PluginInitializerContext<ConfigSchema>) {}
public setup(
{ http, getStartServices, notifications, uiSettings }: CoreSetup,
{ expressions, usageCollection }: SearchServiceSetupDependencies
): ISearchSetup {
this.usageCollector = createUsageCollector(getStartServices, usageCollection);
/**
* 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,
http,
uiSettings,
startServices: getStartServices(),
usageCollector: this.usageCollector!,
});
this.defaultSearchInterceptor = this.searchInterceptor;
expressions.registerFunction(opensearchdsl);
expressions.registerType(opensearchRawResponse);
const aggs = this.aggsService.setup({
registerFunction: expressions.registerFunction,
uiSettings,
});
if (this.initializerContext.config.get().search.aggs.shardDelay.enabled) {
aggs.types.registerBucket(SHARD_DELAY_AGG_NAME, getShardDelayBucketAgg);
expressions.registerFunction(aggShardDelay);
}
return {
aggs,
usageCollector: this.usageCollector!,
__enhance: (enhancements: SearchEnhancements) => {
this.searchInterceptor = enhancements.searchInterceptor;
},
getDefaultSearchInterceptor: () => this.defaultSearchInterceptor,
};
}
public start(
{ application, http, notifications, uiSettings }: CoreStart,
{ fieldFormats, indexPatterns }: SearchServiceStartDependencies
): ISearchStart {
const search = ((request, options) => {
const isEnhancedEnabled = uiSettings.get(UI_SETTINGS.QUERY_ENHANCEMENTS_ENABLED);
if (isEnhancedEnabled) {
const queryStringManager = getQueryService().queryString;
const language = queryStringManager.getQuery().language;
const languageConfig = queryStringManager.getLanguageService().getLanguage(language);
queryStringManager.getLanguageService().setUiOverridesByUserQueryLanguage(language);
if (languageConfig) {
return languageConfig.search.search(request, options);
}
}
return this.defaultSearchInterceptor.search(request, options);
}) as ISearchGeneric;
const loadingCount$ = new BehaviorSubject(0);
http.addLoadingCountSource(loadingCount$);
const dfService: DataFrameService = {
get: () => this.dfCache.get(),
set: async (dataFrame: IDataFrame) => {
this.dfCache.set(dataFrame);
},
clear: () => {
if (this.dfCache.get() === undefined) return;
this.dfCache.clear();
},
};
const searchSourceDependencies: SearchSourceDependencies = {
getConfig: uiSettings.get.bind(uiSettings),
search: <
SearchStrategyRequest extends IOpenSearchDashboardsSearchRequest = IOpenSearchSearchRequest,
SearchStrategyResponse extends
| IOpenSearchDashboardsSearchResponse
| IDataFrameResponse = IOpenSearchSearchResponse
>(
request: SearchStrategyRequest,
options: ISearchOptions
) => {
return search(request, options).toPromise() as Promise<SearchStrategyResponse>;
},
onResponse: handleResponse,
legacy: {
callMsearch: getCallMsearch({ http }),
loadingCount$,
},
df: dfService,
};
return {
aggs: this.aggsService.start({ fieldFormats, uiSettings }),
search,
showError: (e: Error) => {
this.searchInterceptor.showError(e);
},
searchSource: this.searchSourceService.start(indexPatterns, searchSourceDependencies),
__enhance: (enhancements: SearchEnhancements) => {
this.searchInterceptor = enhancements.searchInterceptor;
},
getDefaultSearchInterceptor: () => this.defaultSearchInterceptor,
df: dfService,
};
}
public stop() {
this.aggsService.stop();
this.searchSourceService.stop();
}
}