-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathload_into_elasticsearch.js
69 lines (61 loc) · 1.51 KB
/
load_into_elasticsearch.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
var opts = require('commander'),
elasticsearch = require('elasticsearch'),
fs = require('fs'),
path = require('path'),
moment = require('moment');
opts
.option('-s --source-dir [path]', 'Path to directory containing Enron emails JSON files', './dataset')
.parse(process.argv)
var sourceDir = opts.sourceDir;
// Helper functions
var emailArrayToList = function(emailArray) {
if (emailArray) {
return emailArray.map(function(email) {
return email.address;
});
} else {
return '';
}
}
var client = new elasticsearch.Client({
requestTimeout: 4 * 60 * 60 * 1000
});
// Populate index
fs.readdir(sourceDir, function(err, files) {
if (err) { console.error(err); process.exit(2); }
var filePath, data, json, numFilesProcessed = 0, totalNumFiles = files.length;
var bulkLines = [];
for (var i = 0; i < files.length; ++i) {
file = files[i];
filePath = path.join(sourceDir, file);
data = fs.readFileSync(filePath);
json = JSON.parse(data);
bulkLines.push({
index: {}
});
bulkLines.push({
sender: emailArrayToList(json.from),
recipients: emailArrayToList(json.to),
cc: emailArrayToList(json.cc),
bcc: emailArrayToList(json.bcc),
subject: json.subject,
body: json.text,
datetime: json.date
});
if ((i % 22000) === 0) {
client.bulk({
body: bulkLines,
index: 'enron',
type: 'email'
}, function(err, resp) {
});
bulkLines = [];
}
} // END for
client.bulk({
body: bulkLines,
index: 'enron',
type: 'email'
}, function(err, resp) {
});
}); // fs.readdir