Skip to content

Commit

Permalink
[Search] Re-add support for aborting when a connection is closed
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasolson committed Sep 2, 2020
1 parent b5faf41 commit 4a69b51
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ export const esSearchStrategyProvider = (
};

try {
const esResponse = (await context.core.elasticsearch.client.asCurrentUser.search(
params
)) as ApiResponse<SearchResponse<any>>;
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<SearchResponse<any>>;

if (usage) usage.trackSuccess(rawResponse.took);

Expand Down
12 changes: 10 additions & 2 deletions x-pack/plugins/data_enhanced/server/search/es_search_strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<any>;

return {
rawResponse: shimHitsTotal(response),
...getTotalLoaded(response._shards),
Expand Down

0 comments on commit 4a69b51

Please sign in to comment.