-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[skip ci] Data access service #38737
Changes from all commits
1e5692f
404679e
91a67bb
53cadce
9d91c6e
441f379
0586fd9
fff3492
533d321
db40e92
f7cb7ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* 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 { kfetch } from 'ui/kfetch'; | ||
import { from } from 'rxjs'; | ||
import { SearchArguments, SearchOptions } from '../../common'; | ||
|
||
/** | ||
* The client-side API for making requests to Elasticsearch using raw Elasticsearch request DSL. | ||
* | ||
* @example | ||
* const index = 'twitter'; | ||
* const body = { query: { match_all: {} } }; | ||
* const results = await search({ | ||
* searchParams: { index, body } | ||
* }).toPromise(); | ||
* | ||
* @example | ||
* const controller = new AbortController(); | ||
* setTimeout(() => controller.abort(), 1000); | ||
* const index = 'twitter'; | ||
* const body = { query: { match_all: {} } }; | ||
* const results$ = search({ | ||
* searchParams: { index, body }, | ||
* signal | ||
* }); | ||
* results$.subscribe({ | ||
* next: response => { | ||
* console.log(response._shards.successful / response._shards.total); | ||
* }, | ||
* complete: response => { | ||
* console.log('got response: ', response); | ||
* }, | ||
* error: error => { | ||
* console.log('got error: ', error); | ||
* } | ||
* }); | ||
*/ | ||
export function search({ searchParams, signal, options = {} }: SearchArguments) { | ||
const query = getSearchOptions(options); | ||
const promise = kfetch({ | ||
method: 'POST', | ||
pathname: `../api/search`, | ||
query, | ||
body: JSON.stringify(searchParams), | ||
signal: typeof signal !== 'undefined' ? signal : null, | ||
}); | ||
return from(promise); | ||
} | ||
|
||
// Not really a "session" ID per se, but just something unique to this browser load so that | ||
// requests can hit the same shards if the `preference` setting is set to this | ||
const sessionId = `${Date.now()}`; | ||
|
||
export function getSearchOptions(options: SearchOptions) { | ||
return { | ||
sessionId, | ||
...options, | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* 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 { Server, Request } from 'hapi'; | ||
import { from } from 'rxjs'; | ||
import { SearchArguments } from '../../common'; | ||
import { getSearchParams } from './get_search_params'; | ||
|
||
export function defaultSearchStrategyProvider(server: Server) { | ||
const { callWithRequest } = server.plugins.elasticsearch.getCluster('data'); | ||
return function defaultSearchStrategy( | ||
request: Request, | ||
{ searchParams, signal, options = {} }: SearchArguments | ||
) { | ||
const searchParamsPromise = getSearchParams(request, searchParams, options); | ||
const responsePromise = searchParamsPromise.then(params => | ||
callWithRequest(request, 'search', params, { signal }) | ||
); | ||
return from(responsePromise); | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* 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 { first, map } from 'rxjs/operators'; | ||
import { Request } from 'hapi'; | ||
import KbnServer from 'src/legacy/server/kbn_server'; | ||
import { SearchParams } from 'elasticsearch'; | ||
import { SearchOptions } from '../../common'; | ||
|
||
export async function getSearchParams( | ||
request: Request, | ||
searchParams: SearchParams, | ||
options: SearchOptions | ||
) { | ||
return { | ||
...searchParams, | ||
timeout: searchParams.hasOwnProperty('timeout') | ||
? searchParams.timeout | ||
: await getShardTimeout(request), | ||
preference: searchParams.hasOwnProperty('preference') | ||
? searchParams.preference | ||
: await getPreference(request, options), | ||
maxConcurrentShardRequests: searchParams.hasOwnProperty('maxConcurrentShardRequests') | ||
? (searchParams as any).maxConcurrentShardRequests | ||
: await getMaxConcurrentShardRequests(request), | ||
ignoreThrottled: searchParams.hasOwnProperty('ignoreThrottled') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if this can get moved to the x-pack search strategy. Maybe
Then the x-pack implementation can use the default one plus incorporate some additional settings that are commercial only? |
||
? (searchParams as any).ignoreThrottled | ||
: await getIgnoreThrottled(request), | ||
}; | ||
} | ||
|
||
export async function getShardTimeout(request: Request) { | ||
const kbnServer = (request.server as unknown) as KbnServer; | ||
const shardTimeout$ = kbnServer.newPlatform.setup.core.elasticsearch.legacy.config$.pipe( | ||
first(), | ||
map(config => config.shardTimeout.asMilliseconds()) | ||
); | ||
const timeout = await shardTimeout$.toPromise(); | ||
return `${timeout}ms`; | ||
} | ||
|
||
export async function getPreference(request: Request, { sessionId }: SearchOptions) { | ||
const config = request.getUiSettingsService(); | ||
const setRequestPreferenceTo = await config.get('courier:setRequestPreference'); | ||
if (setRequestPreferenceTo === 'sessionId') { | ||
return sessionId; | ||
} else if (setRequestPreferenceTo === 'custom') { | ||
return config.get('courier:customRequestPreference'); | ||
} | ||
} | ||
|
||
export async function getMaxConcurrentShardRequests(request: Request) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is returning |
||
const config = request.getUiSettingsService(); | ||
const maxConcurrentShardRequests = await config.get('courier:maxConcurrentShardRequests'); | ||
if (maxConcurrentShardRequests !== 0) return maxConcurrentShardRequests; | ||
} | ||
|
||
// TODO: Move to a plugin | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah, I see this comment now! |
||
export function getIgnoreThrottled(request: Request) { | ||
const config = request.getUiSettingsService(); | ||
return !config.get('search:includeFrozen'); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
export * from './get_search_params'; | ||
export * from './default_search_strategy'; | ||
export * from './search'; | ||
export * from './search_strategy'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* 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 { Request } from 'hapi'; | ||
import { SearchArguments } from '../../common'; | ||
import { getSearchStrategy } from './search_strategy'; | ||
|
||
/** | ||
* The server-side API for making requests to Elasticsearch using raw Elasticsearch request DSL. | ||
* | ||
* @example | ||
* const index = 'twitter'; | ||
* const body = { query: { match_all: {} } }; | ||
* const results = await search(request, { | ||
* searchParams: { index, body } | ||
* }).toPromise(); | ||
* | ||
* @example | ||
* const controller = new AbortController(); | ||
* setTimeout(() => controller.abort(), 1000); | ||
* const index = 'twitter'; | ||
* const body = { query: { match_all: {} } }; | ||
* const results$ = search(request, { | ||
* searchParams: { index, body }, | ||
* signal | ||
* }); | ||
* results$.subscribe({ | ||
* next: response => { | ||
* console.log(response._shards.successful / response._shards.total); | ||
* }, | ||
* complete: response => { | ||
* console.log('got response: ', response); | ||
* }, | ||
* error: error => { | ||
* console.log('got error: ', error); | ||
* } | ||
* }); | ||
*/ | ||
export function search(request: Request, searchArguments: SearchArguments) { | ||
const searchStrategy = getSearchStrategy(); | ||
if (typeof searchStrategy !== 'function') { | ||
throw new Error(`No search strategy registered`); | ||
} | ||
return searchStrategy(request, searchArguments); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* 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 { Request } from 'hapi'; | ||
import { Observable } from 'rxjs'; | ||
import { SearchResponse } from 'elasticsearch'; | ||
import { SearchArguments } from '../../common'; | ||
|
||
export type SearchStrategy = ( | ||
request: Request, | ||
searchArguments: SearchArguments | ||
) => Observable<SearchResponse<any>>; | ||
|
||
let searchStrategy: SearchStrategy; | ||
|
||
export function setSearchStrategy(strategy: SearchStrategy) { | ||
searchStrategy = strategy; | ||
} | ||
|
||
export function getSearchStrategy() { | ||
return searchStrategy; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any idea why
maxConcurrentShardRequests
isn't in the SearchParams type? Is it an oversight in the typing or is this a kibana specific param?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actually I see it's only part of msearch. I guess for search endpoint it's irrelevant.