This repository has been archived by the owner on Jun 18, 2019. It is now read-only.
/
update.js
124 lines (118 loc) · 4.04 KB
/
update.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
module.exports = {
friendlyName: 'Update document',
description: 'Update (reindex) the document with the specified unique id.',
extendedDescription: 'If no document with the specified id exists, one will be created.',
moreInfoUrl: 'http://www.elasticsearch.org/guide/en/elasticsearch/client/javascript-api/current/api-reference.html#api-index',
inputs: {
hostname: {
description: 'The hostname of your ElasticSearch server',
example: 'localhost',
required: true,
whereToGet: {
description: 'Copy the hostname of your ElasticSearch server',
extendedDescription: 'i.e. if you are using a hosted ElasticSearch instance at "bae23592g23523.some-hosted-service.com", that is your hostname. If you are running ElasticSearch locally, this will be "localhost".'
}
},
port: {
description: 'The port your ElasticSearch server is running on',
defaultsTo: 9200,
example: 9200,
whereToGet: {
description: 'Copy the port of your ElasticSearch server',
extendedDescription: 'The conventional port number for ElasticSearch servers is 9200.'
}
},
index: {
description: 'The name of the index where the document is stored',
extendedDescription: 'An "index" in ElasticSearch is a lot like a "database" in MySQL or MongoDB.',
example: 'myindex',
required: true,
},
type: {
description: 'The "type" of the document to update',
defaultsTo: 'default',
example: 'user'
},
id: {
description: 'The unique id of the document to replace',
example: 'lzmkDgMjTbGoacxLMsB_zA',
required: true
},
document: {
description: 'The new document (a dictionary) to replace the old one',
typeclass: 'dictionary',
required: true
}
},
defaultExit: 'success',
exits: {
error: {
description: 'Unexpected error occurred.'
},
couldNotConnect: {
description: 'Could not connect to ElasticSearch at the provided hostname and port, or all connections in the ES client pool are "dead".',
extendedDescription: 'See http://www.elasticsearch.org/guide/en/elasticsearch/client/javascript-api/current/errors.html'
},
noSuchIndex: {
description: 'The specified index does not exist',
},
documentNotFound: {
description: 'No document with the specified id and type exists in this index',
},
success: {
description: 'Done.'
},
created: {
description: 'Document with specified id and type did not exist, so it was created.',
example: 'lzmkDgMjTbGoacxLMsB_zA'
}
},
fn: function(inputs, exits) {
var util = require('util');
var _ = require('lodash');
var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
host: util.format('%s:%d', inputs.hostname, inputs.port||9200),
log: require('../helpers/noop-logger')
});
client.index({
index: inputs.index,
type: inputs.type||'default',
id: inputs.id,
body: {
doc: inputs.document
},
}, function (err, body) {
if (err) {
client.close();
if (typeof err !== 'object' || typeof err.message !== 'string'){
return exits.error(err);
}
if (err.constructor && err.constructor.name === 'NoConnections' || err.message.match(/No Living connections/)){
return exits.couldNotConnect();
}
if (err.message.match(/Not Found/)) {
return exits.documentNotFound();
}
if (err.message.match(/IndexMissingException/)){
return exits.noSuchIndex();
}
return exits.error(err);
}
try {
if (body.created) {
// Expected response from ElasticSearch to specify `created:false`
// since it didn't, we inadvertently created the document.
client.close();
return exits.created(inputs.id);
}
}
catch (e) {
client.close();
return exits.error(e);
}
client.close();
return exits.success();
});
},
};