2023-01-27 03:22:44 -08:00
|
|
|
import type { OptionsWithUri } from 'request';
|
2021-07-17 02:43:45 -07:00
|
|
|
|
2023-03-09 09:13:15 -08:00
|
|
|
import type { IExecuteFunctions, IDataObject, JsonObject } from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { NodeApiError } from 'n8n-workflow';
|
2021-07-17 02:43:45 -07:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type { ElasticsearchApiCredentials } from './types';
|
2021-07-17 02:43:45 -07:00
|
|
|
|
|
|
|
export async function elasticsearchApiRequest(
|
|
|
|
this: IExecuteFunctions,
|
|
|
|
method: 'GET' | 'PUT' | 'POST' | 'DELETE',
|
|
|
|
endpoint: string,
|
|
|
|
body: IDataObject = {},
|
|
|
|
qs: IDataObject = {},
|
|
|
|
) {
|
2022-08-01 13:47:55 -07:00
|
|
|
const { baseUrl, ignoreSSLIssues } = (await this.getCredentials(
|
|
|
|
'elasticsearchApi',
|
|
|
|
)) as ElasticsearchApiCredentials;
|
2021-07-17 02:43:45 -07:00
|
|
|
|
|
|
|
const options: OptionsWithUri = {
|
|
|
|
method,
|
|
|
|
body,
|
|
|
|
qs,
|
|
|
|
uri: `${baseUrl}${endpoint}`,
|
|
|
|
json: true,
|
feat(Elasticsearch Node): Add credential tests, index pipelines and index refresh (#2420)
* 🐛 ES query string not passed to request
* 🔑 Add ES credential test
* ✨ Add ES index pipelines and index refresh
* :hammer: merge fix
* :zap: renamed additional filds as options
* :zap: added ignore ssl to credentials
* :zap: Improvements
* :zap: Improvements
* feat(Redis Node): Add push and pop operations (#3127)
* ✨ Add push and pop operations
* :zap: linter fixes
* :zap: linter fixes
* 🐛 Fix errors and remove overwrite
* 🐛 Remove errant hint
* :zap: Small change
Co-authored-by: Michael Kret <michael.k@radency.com>
Co-authored-by: ricardo <ricardoespinoza105@gmail.com>
* refactor: Telemetry updates (#3529)
* Init unit tests for telemetry
* Update telemetry tests
* Test Workflow execution errored event
* Add new tracking logic in pulse
* cleanup
* interfaces
* Add event_version for Workflow execution count event
* add version_cli in all events
* add user saved credentials event
* update manual wf exec finished, fixes
* improve typings, lint
* add node_graph_string in User clicked execute workflow button event
* add User set node operation or mode event
* Add instance started event in FE
* Add User clicked retry execution button event
* add expression editor event
* add input node type to add node event
* add User stopped workflow execution wvent
* add error message in saved credential event
* update stop execution event
* add execution preflight event
* Remove instance started even tfrom FE, add session started to FE,BE
* improve typing
* remove node_graph as property from all events
* move back from default export
* move psl npm package to cli package
* cr
* update webhook node domain logic
* fix is_valid for User saved credentials event
* fix Expression Editor variable selector event
* add caused_by_credential in preflight event
* undo webhook_domain
* change node_type to full type
* add webhook_domain property in manual execution event (#3680)
* add webhook_domain property in manual execution event
* lint fix
* feat(SpreadsheetFile Node): Allow skipping headers when writing spreadsheets (#3234)
* ⚡ Allow skipping headers when writing spreadsheets
* Fix type on sheet options
* fix(Telegram Node): Fix sending binaryData media (photo, document, video etc.) (#3408)
* fixed send media (photo, document, video etc.) issues on Telegram Node
* fixed send media (photo, document, video etc.) issues on Telegram Node
* file name is optional now
* :zap: lock file and linter fix
* :zap: improvements
* :zap: fixes
* :zap: Improvements
* :zap: Add placeholder to File Name
* :zap: Add error message
* :fire: Remove requestWithAuthentication
* :zap: Fix typo
* :shirt: Fix linting issues
Co-authored-by: Michael Kret <michael.k@radency.com>
Co-authored-by: ricardo <ricardoespinoza105@gmail.com>
* feat(Freshworks CRM Node): Add Search + Lookup functionality (#3131)
* Add fields and Ops for Lookup Search
* Adds Search (Search + Lookup) operations
* :hammer: credentials update
* :hammer: improvements
* :zap: clean up and linter fixes
* :zap: merged search and query, more hints
* :zap: Improvements
* :zap: Add generic type to authentication method
Co-authored-by: Michael Kret <michael.k@radency.com>
Co-authored-by: ricardo <ricardoespinoza105@gmail.com>
* feat(Jira Trigger Node): Add optional query auth for security (#3172)
* ✨ Add query auth for Jira Trigger security
* :zap: small fixes:
* :zap: Response with 403 when invalid query authentication
* :shirt: Fix linting issues
Co-authored-by: Michael Kret <michael.k@radency.com>
Co-authored-by: ricardo <ricardoespinoza105@gmail.com>
* :zap: Changed authentication to use the generic type
Co-authored-by: Michael Kret <michael.k@radency.com>
Co-authored-by: ricardo <ricardoespinoza105@gmail.com>
Co-authored-by: Ahsan Virani <ahsan.virani@gmail.com>
Co-authored-by: Nicholas Penree <nick@penree.com>
Co-authored-by: Taha Sönmez <35905778+tahasonmez@users.noreply.github.com>
Co-authored-by: Jan Thiel <JanThiel@users.noreply.github.com>
Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
2022-07-10 02:00:47 -07:00
|
|
|
rejectUnauthorized: !ignoreSSLIssues,
|
2021-07-17 02:43:45 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
if (!Object.keys(body).length) {
|
|
|
|
delete options.body;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Object.keys(qs).length) {
|
|
|
|
delete options.qs;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
feat(Elasticsearch Node): Add credential tests, index pipelines and index refresh (#2420)
* 🐛 ES query string not passed to request
* 🔑 Add ES credential test
* ✨ Add ES index pipelines and index refresh
* :hammer: merge fix
* :zap: renamed additional filds as options
* :zap: added ignore ssl to credentials
* :zap: Improvements
* :zap: Improvements
* feat(Redis Node): Add push and pop operations (#3127)
* ✨ Add push and pop operations
* :zap: linter fixes
* :zap: linter fixes
* 🐛 Fix errors and remove overwrite
* 🐛 Remove errant hint
* :zap: Small change
Co-authored-by: Michael Kret <michael.k@radency.com>
Co-authored-by: ricardo <ricardoespinoza105@gmail.com>
* refactor: Telemetry updates (#3529)
* Init unit tests for telemetry
* Update telemetry tests
* Test Workflow execution errored event
* Add new tracking logic in pulse
* cleanup
* interfaces
* Add event_version for Workflow execution count event
* add version_cli in all events
* add user saved credentials event
* update manual wf exec finished, fixes
* improve typings, lint
* add node_graph_string in User clicked execute workflow button event
* add User set node operation or mode event
* Add instance started event in FE
* Add User clicked retry execution button event
* add expression editor event
* add input node type to add node event
* add User stopped workflow execution wvent
* add error message in saved credential event
* update stop execution event
* add execution preflight event
* Remove instance started even tfrom FE, add session started to FE,BE
* improve typing
* remove node_graph as property from all events
* move back from default export
* move psl npm package to cli package
* cr
* update webhook node domain logic
* fix is_valid for User saved credentials event
* fix Expression Editor variable selector event
* add caused_by_credential in preflight event
* undo webhook_domain
* change node_type to full type
* add webhook_domain property in manual execution event (#3680)
* add webhook_domain property in manual execution event
* lint fix
* feat(SpreadsheetFile Node): Allow skipping headers when writing spreadsheets (#3234)
* ⚡ Allow skipping headers when writing spreadsheets
* Fix type on sheet options
* fix(Telegram Node): Fix sending binaryData media (photo, document, video etc.) (#3408)
* fixed send media (photo, document, video etc.) issues on Telegram Node
* fixed send media (photo, document, video etc.) issues on Telegram Node
* file name is optional now
* :zap: lock file and linter fix
* :zap: improvements
* :zap: fixes
* :zap: Improvements
* :zap: Add placeholder to File Name
* :zap: Add error message
* :fire: Remove requestWithAuthentication
* :zap: Fix typo
* :shirt: Fix linting issues
Co-authored-by: Michael Kret <michael.k@radency.com>
Co-authored-by: ricardo <ricardoespinoza105@gmail.com>
* feat(Freshworks CRM Node): Add Search + Lookup functionality (#3131)
* Add fields and Ops for Lookup Search
* Adds Search (Search + Lookup) operations
* :hammer: credentials update
* :hammer: improvements
* :zap: clean up and linter fixes
* :zap: merged search and query, more hints
* :zap: Improvements
* :zap: Add generic type to authentication method
Co-authored-by: Michael Kret <michael.k@radency.com>
Co-authored-by: ricardo <ricardoespinoza105@gmail.com>
* feat(Jira Trigger Node): Add optional query auth for security (#3172)
* ✨ Add query auth for Jira Trigger security
* :zap: small fixes:
* :zap: Response with 403 when invalid query authentication
* :shirt: Fix linting issues
Co-authored-by: Michael Kret <michael.k@radency.com>
Co-authored-by: ricardo <ricardoespinoza105@gmail.com>
* :zap: Changed authentication to use the generic type
Co-authored-by: Michael Kret <michael.k@radency.com>
Co-authored-by: ricardo <ricardoespinoza105@gmail.com>
Co-authored-by: Ahsan Virani <ahsan.virani@gmail.com>
Co-authored-by: Nicholas Penree <nick@penree.com>
Co-authored-by: Taha Sönmez <35905778+tahasonmez@users.noreply.github.com>
Co-authored-by: Jan Thiel <JanThiel@users.noreply.github.com>
Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
2022-07-10 02:00:47 -07:00
|
|
|
return await this.helpers.requestWithAuthentication.call(this, 'elasticsearchApi', options);
|
2022-10-18 12:09:36 -07:00
|
|
|
} catch (error) {
|
2023-02-27 19:39:43 -08:00
|
|
|
throw new NodeApiError(this.getNode(), error as JsonObject);
|
2022-10-18 12:09:36 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function elasticsearchApiRequestAllItems(
|
|
|
|
this: IExecuteFunctions,
|
|
|
|
indexId: string,
|
|
|
|
body: IDataObject = {},
|
|
|
|
qs: IDataObject = {},
|
2022-11-22 04:43:28 -08:00
|
|
|
): Promise<any> {
|
2022-10-18 12:09:36 -07:00
|
|
|
//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
|
|
|
|
};
|
|
|
|
|
2022-12-29 03:20:43 -08:00
|
|
|
responseData = await elasticsearchApiRequest.call(this, 'GET', '/_search', requestBody, qs);
|
2022-10-18 12:09:36 -07:00
|
|
|
if (responseData?.hits?.hits) {
|
2023-02-27 19:39:43 -08:00
|
|
|
returnData = returnData.concat(responseData.hits.hits as IDataObject[]);
|
2022-10-18 12:09:36 -07:00
|
|
|
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' };
|
|
|
|
|
2022-12-29 03:20:43 -08:00
|
|
|
responseData = await elasticsearchApiRequest.call(this, 'GET', '/_search', requestBody, qs);
|
2022-10-18 12:09:36 -07:00
|
|
|
|
|
|
|
if (responseData?.hits?.hits?.length) {
|
2023-02-27 19:39:43 -08:00
|
|
|
returnData = returnData.concat(responseData.hits.hits as IDataObject[]);
|
2022-10-18 12:09:36 -07:00
|
|
|
const lastHitIndex = responseData.hits.hits.length - 1;
|
|
|
|
searchAfter = responseData.hits.hits[lastHitIndex].sort;
|
|
|
|
pit = responseData.pit_id;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-29 03:20:43 -08:00
|
|
|
await elasticsearchApiRequest.call(this, 'DELETE', '/_pit', { id: pit });
|
2022-10-18 12:09:36 -07:00
|
|
|
|
|
|
|
return returnData;
|
2021-07-17 02:43:45 -07:00
|
|
|
} catch (error) {
|
2022-07-10 03:11:12 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error as JsonObject);
|
2021-07-17 02:43:45 -07:00
|
|
|
}
|
|
|
|
}
|