-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelastic-import.js
executable file
·60 lines (52 loc) · 1.3 KB
/
elastic-import.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
#!/usr/bin/env node
const fs = require('fs');
const args = require('commander');
const elasticsearch = require('elasticsearch');
args
.option('-H, --host <host>', 'Elastic search hostname', '127.0.0.1:9200')
.option('-I, --index <index>', 'Index name to export')
.option('-D, --data <file>', 'Data file to import')
.option('-T, --type [type]', 'Index type name to import', '_doc')
.parse(process.argv);
if (!args.host) {
console.error('Host is required, -H <elastic hostname:port>');
process.exit();
}
if (!args.index) {
console.error('Index name is required, -I <index name>');
process.exit();
}
if (!args.data) {
console.error('Data file is required, -D <file name>');
process.exit();
}
const client = new elasticsearch.Client({
host: args.host
});
let data;
try {
let source = fs.readFileSync(args.data);
data = JSON.parse(source);
} catch (err) {
console.error(err);
}
let elasticActions = [];
for (let i = 0; i < data.length; i++) {
let { _id, ..._source } = data[i];
elasticActions.push({
index: {
_index: args.index,
_type: args.type,
_id
}
});
elasticActions.push(_source);
}
client
.bulk({ body: elasticActions })
.then(resp => {
console.log(`Done. Imported ${data.length} documents`);
})
.catch(err => {
console.error(err);
});