-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
57 lines (43 loc) · 1.17 KB
/
index.js
File metadata and controls
57 lines (43 loc) · 1.17 KB
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
#!/usr/bin/env node
var elasticsearch = require('elasticsearch');
var program = require('commander');
var co = require('co');
program
.option('-u, --url', 'url, defaul is: `localhost:9200`')
.option('-i, --index [value]', 'index')
.option('-t, --type [value]', 'type')
.option('-q, --query [value]', 'query, e.g.: `{query: {match_all: {}}}`')
.parse(process.argv);
if (!program.index || !program.type || !program.query) {
program.outputHelp();
process.exit(0);
}
var url = program.url || 'localhost:9200';
var client = new elasticsearch.Client({
host: url,
log: 'error'
});
remove(program.index, program.type, program.query);
function remove(index, type, query) {
var q = {
index: index,
type: type,
body: query,
size: 1000,
fields: []
};
co(function *() {
var response = yield client.search(q);
var hits = response.hits.hits;
if (hits.length === 0) {
console.log('Finished');
return;
}
var bulk = {};
bulk.body = hits.map(h => ({ delete: { _index: index, _type: type, _id: h._id } }));
yield client.bulk(bulk);
remove(index, type, query);
}).catch(function(e) {
console.log(e);
});
}