From 4a69b5145ee5def5b2480bb4f592e6d5987fbba1 Mon Sep 17 00:00:00 2001 From: Lukas Olson Date: Tue, 1 Sep 2020 18:54:07 -0700 Subject: [PATCH 1/2] [Search] Re-add support for aborting when a connection is closed --- .../server/search/es_search/es_search_strategy.ts | 8 ++++---- .../server/search/es_search_strategy.ts | 12 ++++++++++-- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/plugins/data/server/search/es_search/es_search_strategy.ts b/src/plugins/data/server/search/es_search/es_search_strategy.ts index eabbf3e3e2600..78ae0b042c6ca 100644 --- a/src/plugins/data/server/search/es_search/es_search_strategy.ts +++ b/src/plugins/data/server/search/es_search/es_search_strategy.ts @@ -47,10 +47,10 @@ export const esSearchStrategyProvider = ( }; try { - const esResponse = (await context.core.elasticsearch.client.asCurrentUser.search( - params - )) as ApiResponse>; - const rawResponse = esResponse.body; + // Temporary workaround until https://github.com/elastic/elasticsearch-js/issues/1297 + const promise = context.core.elasticsearch.client.asCurrentUser.search(params); + if (options?.signal) options.signal.addEventListener('abort', () => promise.abort()); + const { body: rawResponse } = (await promise) as ApiResponse>; if (usage) usage.trackSuccess(rawResponse.took); diff --git a/x-pack/plugins/data_enhanced/server/search/es_search_strategy.ts b/x-pack/plugins/data_enhanced/server/search/es_search_strategy.ts index 46609af52d072..7a493a1d0ad92 100644 --- a/x-pack/plugins/data_enhanced/server/search/es_search_strategy.ts +++ b/x-pack/plugins/data_enhanced/server/search/es_search_strategy.ts @@ -112,14 +112,18 @@ async function asyncSearch( }); // TODO: replace with async endpoints once https://github.com/elastic/elasticsearch-js/issues/1280 is resolved - const esResponse = await client.transport.request({ + const promise = client.transport.request({ method, path, body, querystring, }); + // Temporary workaround until https://github.com/elastic/elasticsearch-js/issues/1297 + if (options?.signal) options.signal.addEventListener('abort', () => promise.abort()); + const esResponse = await promise; const { id, response, is_partial: isPartial, is_running: isRunning } = esResponse.body; + return { id, isPartial, @@ -139,14 +143,18 @@ async function rollupSearch( const path = encodeURI(`/${index}/_rollup_search`); const querystring = toSnakeCase(params); - const esResponse = await client.transport.request({ + const promise = client.transport.request({ method, path, body, querystring, }); + // Temporary workaround until https://github.com/elastic/elasticsearch-js/issues/1297 + if (options?.signal) options.signal.addEventListener('abort', () => promise.abort()); + const esResponse = await promise; const response = esResponse.body as SearchResponse; + return { rawResponse: shimHitsTotal(response), ...getTotalLoaded(response._shards), From 5e5fbc79ff2caac9dce472414a8d4a743854cfa6 Mon Sep 17 00:00:00 2001 From: Lukas Olson Date: Sun, 13 Sep 2020 10:19:20 -0700 Subject: [PATCH 2/2] Fix types --- .../plugins/data_enhanced/server/search/es_search_strategy.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/x-pack/plugins/data_enhanced/server/search/es_search_strategy.ts b/x-pack/plugins/data_enhanced/server/search/es_search_strategy.ts index 1e566162cc431..72ea1f096e8fb 100644 --- a/x-pack/plugins/data_enhanced/server/search/es_search_strategy.ts +++ b/x-pack/plugins/data_enhanced/server/search/es_search_strategy.ts @@ -7,6 +7,7 @@ import { first } from 'rxjs/operators'; import { SearchResponse } from 'elasticsearch'; import { Observable } from 'rxjs'; +import { TransportRequestPromise } from '@elastic/elasticsearch/lib/Transport'; import { SharedGlobalConfig, RequestHandlerContext, Logger } from '../../../../../src/core/server'; import { getTotalLoaded, @@ -72,7 +73,7 @@ export const enhancedEsSearchStrategyProvider = ( request: IEnhancedEsSearchRequest, options?: ISearchOptions ): Promise { - let promise; + let promise: TransportRequestPromise; const esClient = context.core.elasticsearch.client.asCurrentUser; const uiSettingsClient = await context.core.uiSettings.client;