/
deleteByQuery.js
executable file
·92 lines (86 loc) · 2.82 KB
/
deleteByQuery.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
module.exports = {
friendlyName: 'Delete by query',
description: 'Delete by query',
extendedDescription: '',
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 are documents to delete',
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 delete',
defaultsTo: 'default',
example: 'mytype'
},
query: {
description: 'The delete query',
example: '{ query: { "match_all" : {} } }',
required: true
}
},
defaultExit: 'success',
exits: {
error: {
description: 'Unexpected error occurred.',
},
couldNotConnect: {
description: 'Could not connect to ElasticSearch at the provided hostname and port.',
},
noSuchIndex: {
description: 'The specified index does not exist.'
},
success: {
description: 'Done.'
},
},
fn: function (inputs, exits) {
var util = require('util');
var _ = require('lodash');
var elasticsearch = require('elasticsearch');
var client = elasticsearch.Client({
host: util.format('%s:%d', inputs.hostname, inputs.port||9200),
log: require('../helpers/noop-logger')
});
client.deleteByQuery({
index: inputs.index,
type: inputs.type||'default',
body: inputs.query
}, 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(/IndexMissingException/)) {
return exits.noSuchIndex();
}
return exits.error(err);
}
client.close();
return exits.success();
});
},
};