-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport.js
102 lines (74 loc) · 2 KB
/
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
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
// Import text files to Elasticsearch
(function () {
const fs = require('fs');
const dir = require('node-dir');
const elastic = require('elasticsearch');
// Configuration
let inputFolder = './text/';
let host = 'localhost:9200';
let index = 'my-index';
let type = 'doc';
let fileCount, client;
// Execute script if not used as a module
if (!module.parent) {
init(
process.argv[2],
process.argv[3],
process.argv[4],
process.argv[5]
);
}
function init(_inputFolder, _host, _index, _type) {
// Overwrite default configuration with arguments
// from module or command line interface
inputFolder = _inputFolder || inputFolder;
host = _host || host;
index = _index || index;
type = _type || type;
// Initialize Elasticsearch client
client = new elastic.Client({ host: host });
readFiles();
}
function readFiles() {
// Get a list of all files
dir.files(inputFolder, (error, fileList) => {
if (error) { throw error; }
// Include text files only
fileList = fileList.filter(file => file.indexOf('.txt') > -1);
fileCount = fileList.length;
processFiles(fileList);
});
}
function processFiles(fileList) {
const filePath = fileList.pop();
if (filePath) {
// Read file content
fs.readFile(filePath, 'utf8', (error, body) => {
if (error) { throw error; }
saveToElastic(filePath, body, () => {
// Recursion
processFiles(fileList);
});
});
} else {
console.log(`Finished processing ${fileCount} documents`);
}
}
// Saves file content and meta data to ElasticSearch
function saveToElastic(filePath, body, callback) {
client.create({
index,
type,
body: {
filePath,
body
}
},
error => {
if (error) throw error;
console.log(`Inserted document ${filePath} to ElasticSearch`);
callback();
});
}
module.exports = { init };
})();