2023-01-27 03:22:44 -08:00
|
|
|
import type { IExecuteFunctions } from 'n8n-core';
|
2021-07-17 02:43:45 -07:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2022-10-21 11:52:43 -07:00
|
|
|
IDataObject,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
|
|
|
} from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { jsonParse } from 'n8n-workflow';
|
2022-10-18 12:09:36 -07:00
|
|
|
|
|
|
|
import { elasticsearchApiRequest, elasticsearchApiRequestAllItems } from './GenericFunctions';
|
2021-07-17 02:43:45 -07:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
import { documentFields, documentOperations, indexFields, indexOperations } from './descriptions';
|
2021-07-17 02:43:45 -07:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type { DocumentGetAllOptions, FieldsUiValues } from './types';
|
2021-07-17 02:43:45 -07:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
import { omit } from 'lodash';
|
2021-07-17 02:43:45 -07:00
|
|
|
|
|
|
|
export class Elasticsearch implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Elasticsearch',
|
|
|
|
name: 'elasticsearch',
|
|
|
|
icon: 'file:elasticsearch.svg',
|
|
|
|
group: ['transform'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
|
|
|
description: 'Consume the Elasticsearch API',
|
|
|
|
defaults: {
|
|
|
|
name: 'Elasticsearch',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'elasticsearchApi',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2021-07-17 02:43:45 -07:00
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Document',
|
|
|
|
value: 'document',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Index',
|
|
|
|
value: 'index',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'document',
|
|
|
|
},
|
|
|
|
...documentOperations,
|
|
|
|
...documentFields,
|
|
|
|
...indexOperations,
|
|
|
|
...indexFields,
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
2022-08-30 08:55:33 -07:00
|
|
|
const returnData: INodeExecutionData[] = [];
|
2021-07-17 02:43:45 -07:00
|
|
|
|
|
|
|
const resource = this.getNodeParameter('resource', 0) as 'document' | 'index';
|
2022-12-02 03:53:59 -08:00
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
2021-07-17 02:43:45 -07:00
|
|
|
|
|
|
|
let responseData;
|
|
|
|
|
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
|
|
if (resource === 'document') {
|
|
|
|
// **********************************************************************
|
|
|
|
// document
|
|
|
|
// **********************************************************************
|
|
|
|
|
|
|
|
if (operation === 'delete') {
|
|
|
|
// ----------------------------------------
|
|
|
|
// document: delete
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
// https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete.html
|
|
|
|
|
|
|
|
const indexId = this.getNodeParameter('indexId', i);
|
|
|
|
const documentId = this.getNodeParameter('documentId', i);
|
|
|
|
|
|
|
|
const endpoint = `/${indexId}/_doc/${documentId}`;
|
|
|
|
responseData = await elasticsearchApiRequest.call(this, 'DELETE', endpoint);
|
|
|
|
} else if (operation === 'get') {
|
|
|
|
// ----------------------------------------
|
|
|
|
// document: get
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
// https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-get.html
|
|
|
|
|
|
|
|
const indexId = this.getNodeParameter('indexId', i);
|
|
|
|
const documentId = this.getNodeParameter('documentId', i);
|
|
|
|
|
|
|
|
const qs = {} as IDataObject;
|
2022-11-18 07:29:44 -08:00
|
|
|
const options = this.getNodeParameter('options', i);
|
2021-07-17 02:43:45 -07:00
|
|
|
|
|
|
|
if (Object.keys(options).length) {
|
|
|
|
Object.assign(qs, options);
|
|
|
|
qs._source = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const endpoint = `/${indexId}/_doc/${documentId}`;
|
|
|
|
responseData = await elasticsearchApiRequest.call(this, 'GET', endpoint, {}, qs);
|
|
|
|
|
|
|
|
const simple = this.getNodeParameter('simple', i) as IDataObject;
|
|
|
|
|
|
|
|
if (simple) {
|
2021-07-17 15:32:08 -07:00
|
|
|
responseData = {
|
|
|
|
_id: responseData._id,
|
|
|
|
...responseData._source,
|
|
|
|
};
|
2021-07-17 02:43:45 -07:00
|
|
|
}
|
|
|
|
} else if (operation === 'getAll') {
|
|
|
|
// ----------------------------------------
|
|
|
|
// document: getAll
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
// https://www.elastic.co/guide/en/elasticsearch/reference/current/search-search.html
|
|
|
|
|
|
|
|
const indexId = this.getNodeParameter('indexId', i);
|
|
|
|
|
|
|
|
const body = {} as IDataObject;
|
|
|
|
const qs = {} as IDataObject;
|
|
|
|
const options = this.getNodeParameter('options', i) as DocumentGetAllOptions;
|
2022-10-18 12:09:36 -07:00
|
|
|
// const paginate = this.getNodeParameter('paginate', i) as boolean;
|
2021-07-17 02:43:45 -07:00
|
|
|
|
|
|
|
if (Object.keys(options).length) {
|
|
|
|
const { query, ...rest } = options;
|
2022-10-21 11:52:43 -07:00
|
|
|
if (query) {
|
|
|
|
Object.assign(
|
|
|
|
body,
|
|
|
|
jsonParse(query, { errorMessage: "Invalid JSON in 'Query' option" }),
|
|
|
|
);
|
|
|
|
}
|
2021-07-17 02:43:45 -07:00
|
|
|
Object.assign(qs, rest);
|
|
|
|
qs._source = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const returnAll = this.getNodeParameter('returnAll', 0);
|
|
|
|
|
2022-10-18 12:09:36 -07:00
|
|
|
if (returnAll) {
|
|
|
|
//Defines the number of hits to return. Defaults to 10. By default, you cannot page through more than 10,000 hits
|
|
|
|
qs.size = 10000;
|
|
|
|
if (qs.sort) {
|
|
|
|
responseData = await elasticsearchApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
indexId as string,
|
|
|
|
body,
|
|
|
|
qs,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
responseData = await elasticsearchApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/${indexId}/_search`,
|
|
|
|
body,
|
|
|
|
qs,
|
|
|
|
);
|
|
|
|
responseData = responseData.hits.hits;
|
|
|
|
}
|
|
|
|
} else {
|
2021-07-17 02:43:45 -07:00
|
|
|
qs.size = this.getNodeParameter('limit', 0);
|
|
|
|
|
2022-10-18 12:09:36 -07:00
|
|
|
responseData = await elasticsearchApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/${indexId}/_search`,
|
|
|
|
body,
|
|
|
|
qs,
|
|
|
|
);
|
|
|
|
responseData = responseData.hits.hits;
|
|
|
|
}
|
2021-07-17 02:43:45 -07:00
|
|
|
|
2021-07-18 05:21:30 -07:00
|
|
|
const simple = this.getNodeParameter('simple', 0) as IDataObject;
|
|
|
|
|
|
|
|
if (simple) {
|
|
|
|
responseData = responseData.map((item: IDataObject) => {
|
|
|
|
return {
|
|
|
|
_id: item._id,
|
2022-12-02 12:54:28 -08:00
|
|
|
...(item._source as IDataObject),
|
2021-07-18 05:21:30 -07:00
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
2021-07-17 02:43:45 -07:00
|
|
|
} else if (operation === 'create') {
|
|
|
|
// ----------------------------------------
|
|
|
|
// document: create
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
// https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html
|
|
|
|
|
|
|
|
const body: IDataObject = {};
|
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
const dataToSend = this.getNodeParameter('dataToSend', 0) as
|
|
|
|
| 'defineBelow'
|
|
|
|
| 'autoMapInputData';
|
2021-07-17 02:43:45 -07:00
|
|
|
|
|
|
|
if (dataToSend === 'defineBelow') {
|
|
|
|
const fields = this.getNodeParameter('fieldsUi.fieldValues', i, []) as FieldsUiValues;
|
2022-08-01 13:47:55 -07:00
|
|
|
fields.forEach(({ fieldId, fieldValue }) => (body[fieldId] = fieldValue));
|
2021-07-17 02:43:45 -07:00
|
|
|
} else {
|
|
|
|
const inputData = items[i].json;
|
|
|
|
const rawInputsToIgnore = this.getNodeParameter('inputsToIgnore', i) as string;
|
2022-08-01 13:47:55 -07:00
|
|
|
const inputsToIgnore = rawInputsToIgnore.split(',').map((c) => c.trim());
|
2021-07-17 02:43:45 -07:00
|
|
|
|
|
|
|
for (const key of Object.keys(inputData)) {
|
|
|
|
if (inputsToIgnore.includes(key)) continue;
|
|
|
|
body[key] = inputData[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const qs = {} as IDataObject;
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2021-07-17 02:43:45 -07:00
|
|
|
|
|
|
|
if (Object.keys(additionalFields).length) {
|
|
|
|
Object.assign(qs, omit(additionalFields, ['documentId']));
|
|
|
|
}
|
|
|
|
|
|
|
|
const indexId = this.getNodeParameter('indexId', i);
|
|
|
|
const { documentId } = additionalFields;
|
|
|
|
|
|
|
|
if (documentId) {
|
|
|
|
const endpoint = `/${indexId}/_doc/${documentId}`;
|
2022-10-18 12:09:36 -07:00
|
|
|
responseData = await elasticsearchApiRequest.call(this, 'PUT', endpoint, body);
|
2021-07-17 02:43:45 -07:00
|
|
|
} else {
|
|
|
|
const endpoint = `/${indexId}/_doc`;
|
2022-10-18 12:09:36 -07:00
|
|
|
responseData = await elasticsearchApiRequest.call(this, 'POST', endpoint, body);
|
2021-07-17 02:43:45 -07:00
|
|
|
}
|
|
|
|
} else if (operation === 'update') {
|
|
|
|
// ----------------------------------------
|
|
|
|
// document: update
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
// https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html
|
|
|
|
|
|
|
|
const body = { doc: {} } as { doc: { [key: string]: string } };
|
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
const dataToSend = this.getNodeParameter('dataToSend', 0) as
|
|
|
|
| 'defineBelow'
|
|
|
|
| 'autoMapInputData';
|
2021-07-17 02:43:45 -07:00
|
|
|
|
|
|
|
if (dataToSend === 'defineBelow') {
|
|
|
|
const fields = this.getNodeParameter('fieldsUi.fieldValues', i, []) as FieldsUiValues;
|
2022-08-01 13:47:55 -07:00
|
|
|
fields.forEach(({ fieldId, fieldValue }) => (body.doc[fieldId] = fieldValue));
|
2021-07-17 02:43:45 -07:00
|
|
|
} else {
|
|
|
|
const inputData = items[i].json;
|
|
|
|
const rawInputsToIgnore = this.getNodeParameter('inputsToIgnore', i) as string;
|
2022-08-01 13:47:55 -07:00
|
|
|
const inputsToIgnore = rawInputsToIgnore.split(',').map((c) => c.trim());
|
2021-07-17 02:43:45 -07:00
|
|
|
|
|
|
|
for (const key of Object.keys(inputData)) {
|
|
|
|
if (inputsToIgnore.includes(key)) continue;
|
|
|
|
body.doc[key] = inputData[key] as string;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const indexId = this.getNodeParameter('indexId', i);
|
|
|
|
const documentId = this.getNodeParameter('documentId', i);
|
|
|
|
|
|
|
|
const endpoint = `/${indexId}/_update/${documentId}`;
|
2022-10-18 12:09:36 -07:00
|
|
|
responseData = await elasticsearchApiRequest.call(this, 'POST', endpoint, body);
|
2021-07-17 02:43:45 -07:00
|
|
|
}
|
|
|
|
} else if (resource === 'index') {
|
|
|
|
// **********************************************************************
|
|
|
|
// index
|
|
|
|
// **********************************************************************
|
|
|
|
|
|
|
|
// https://www.elastic.co/guide/en/elasticsearch/reference/current/indices.html
|
|
|
|
|
|
|
|
if (operation === 'create') {
|
|
|
|
// ----------------------------------------
|
|
|
|
// index: create
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
// https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html
|
|
|
|
|
|
|
|
const indexId = this.getNodeParameter('indexId', i);
|
|
|
|
|
|
|
|
const body = {} as IDataObject;
|
|
|
|
const qs = {} as IDataObject;
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2021-07-17 02:43:45 -07:00
|
|
|
|
|
|
|
if (Object.keys(additionalFields).length) {
|
|
|
|
const { aliases, mappings, settings, ...rest } = additionalFields;
|
|
|
|
Object.assign(body, aliases, mappings, settings);
|
|
|
|
Object.assign(qs, rest);
|
|
|
|
}
|
|
|
|
|
|
|
|
responseData = await elasticsearchApiRequest.call(this, 'PUT', `/${indexId}`);
|
|
|
|
responseData = { id: indexId, ...responseData };
|
|
|
|
delete responseData.index;
|
|
|
|
} else if (operation === 'delete') {
|
|
|
|
// ----------------------------------------
|
|
|
|
// index: delete
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
// https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-delete-index.html
|
|
|
|
|
|
|
|
const indexId = this.getNodeParameter('indexId', i);
|
|
|
|
|
|
|
|
responseData = await elasticsearchApiRequest.call(this, 'DELETE', `/${indexId}`);
|
|
|
|
responseData = { success: true };
|
|
|
|
} else if (operation === 'get') {
|
|
|
|
// ----------------------------------------
|
|
|
|
// index: get
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
// https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-index.html
|
|
|
|
|
|
|
|
const indexId = this.getNodeParameter('indexId', i) as string;
|
|
|
|
|
|
|
|
const qs = {} as IDataObject;
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2021-07-17 02:43:45 -07:00
|
|
|
|
|
|
|
if (Object.keys(additionalFields).length) {
|
|
|
|
Object.assign(qs, additionalFields);
|
|
|
|
}
|
|
|
|
|
|
|
|
responseData = await elasticsearchApiRequest.call(this, 'GET', `/${indexId}`, {}, qs);
|
|
|
|
responseData = { id: indexId, ...responseData[indexId] };
|
|
|
|
} else if (operation === 'getAll') {
|
|
|
|
// ----------------------------------------
|
|
|
|
// index: getAll
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
// https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-aliases.html
|
|
|
|
|
|
|
|
responseData = await elasticsearchApiRequest.call(this, 'GET', '/_aliases');
|
2022-12-02 12:54:28 -08:00
|
|
|
responseData = Object.keys(responseData).map((index) => ({ indexId: index }));
|
2021-07-17 02:43:45 -07:00
|
|
|
|
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
|
|
|
|
|
|
|
if (!returnAll) {
|
2022-11-18 06:26:22 -08:00
|
|
|
const limit = this.getNodeParameter('limit', i);
|
2021-07-17 02:43:45 -07:00
|
|
|
responseData = responseData.slice(0, limit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-30 08:55:33 -07:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
|
|
|
this.helpers.returnJsonArray(responseData),
|
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
returnData.push(...executionData);
|
2021-07-17 02:43:45 -07:00
|
|
|
}
|
|
|
|
|
2022-08-30 08:55:33 -07:00
|
|
|
return this.prepareOutputData(returnData);
|
2021-07-17 02:43:45 -07:00
|
|
|
}
|
|
|
|
}
|