From 4a69b5145ee5def5b2480bb4f592e6d5987fbba1 Mon Sep 17 00:00:00 2001 From: Lukas Olson Date: Tue, 1 Sep 2020 18:54:07 -0700 Subject: [PATCH] [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),