mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 10:02:05 +00:00
fix(Elasticsearch Node): fix pagination issue
This commit is contained in:
@@ -36,6 +36,69 @@ export async function elasticsearchApiRequest(
|
||||
|
||||
try {
|
||||
return await this.helpers.requestWithAuthentication.call(this, 'elasticsearchApi', options);
|
||||
} catch (error) {
|
||||
throw new NodeApiError(this.getNode(), error);
|
||||
}
|
||||
}
|
||||
|
||||
export async function elasticsearchApiRequestAllItems(
|
||||
this: IExecuteFunctions,
|
||||
indexId: string,
|
||||
body: IDataObject = {},
|
||||
qs: IDataObject = {},
|
||||
): Promise<any> { //tslint:disable-line:no-any
|
||||
//https://www.elastic.co/guide/en/elasticsearch/reference/7.16/paginate-search-results.html#search-after
|
||||
try {
|
||||
//create a point in time (PIT) to preserve the current index state over your searches
|
||||
let pit = (
|
||||
await elasticsearchApiRequest.call(this, 'POST', `/${indexId}/_pit`, {}, { keep_alive: '1m' })
|
||||
)?.id as string;
|
||||
|
||||
let returnData: IDataObject[] = [];
|
||||
let responseData;
|
||||
let searchAfter: string[] = [];
|
||||
|
||||
const requestBody: IDataObject = {
|
||||
...body,
|
||||
size: 10000,
|
||||
pit: {
|
||||
id: pit,
|
||||
keep_alive: '1m',
|
||||
},
|
||||
track_total_hits: false, //Disable the tracking of total hits to speed up pagination
|
||||
};
|
||||
|
||||
responseData = await elasticsearchApiRequest.call(this, 'GET', `/_search`, requestBody, qs);
|
||||
if (responseData?.hits?.hits) {
|
||||
returnData = returnData.concat(responseData.hits.hits);
|
||||
const lastHitIndex = responseData.hits.hits.length - 1;
|
||||
//Sort values for the last returned hit with the tiebreaker value
|
||||
searchAfter = responseData.hits.hits[lastHitIndex].sort;
|
||||
//Update id for the point in time
|
||||
pit = responseData.pit_id;
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
|
||||
while (true) {
|
||||
requestBody.search_after = searchAfter;
|
||||
requestBody.pit = { id: pit, keep_alive: '1m' };
|
||||
|
||||
responseData = await elasticsearchApiRequest.call(this, 'GET', `/_search`, requestBody, qs);
|
||||
|
||||
if (responseData?.hits?.hits?.length) {
|
||||
returnData = returnData.concat(responseData.hits.hits);
|
||||
const lastHitIndex = responseData.hits.hits.length - 1;
|
||||
searchAfter = responseData.hits.hits[lastHitIndex].sort;
|
||||
pit = responseData.pit_id;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
await elasticsearchApiRequest.call(this, 'DELETE', `/_pit`, { id: pit });
|
||||
|
||||
return returnData;
|
||||
} catch (error) {
|
||||
throw new NodeApiError(this.getNode(), error as JsonObject);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user