-
Notifications
You must be signed in to change notification settings - Fork 2
/
elastic.js
185 lines (152 loc) · 5.71 KB
/
elastic.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
const elasticsearch = require('elasticsearch');
const fs = require('fs');
const config = require('./config');
const options = require('./options');
class Elastic{
constructor(){
this.target = {
doc_count: {
file: 0,
elastic: 0
}
}
this.batches = 0;
this._scroll_id = null;
}
async init(){
//configure source client
this.sourceClient = new elasticsearch.Client({
host: config.source.host,
log: options.elasticsearch.log_level
})
await this._checkConnection(this.sourceClient, 'source');
//configure target elastic client if required
if(options.consume.byElastic){
this.targetClient = new elasticsearch.Client({
host: config.target.host,
log: options.elasticsearch.log_level
})
await this._checkConnection(this.targetClient, 'target');
if(config.target.importMappingFromSource)
await this._transferMapping(this.sourceClient, this.targetClient);
}
//configure target file if required
if(options.consume.byFile)
await this._mkdir(config.target.dirPath);
}
async getDocsFromSourceElastic(){
let results;
//the first time; when scroll_id isn't generated yet
if(!this._scroll_id){
results = await this.sourceClient.search({
scroll: options.elasticsearch.scroll_time,
index: config.source.index,
type: config.source.type,
body: {
size: options.produce.batch_size,
query: options.elasticsearch.query
}
})
}
//rest of the times
else{
results = await this.sourceClient.scroll({
scrollId: this._scroll_id,
scroll: options.elasticsearch.scroll_time
})
}
this.batches++;
this._scroll_id = results._scroll_id;
console.log('Batches produced: ', this.batches);
return results
}
addDocsToTargetElastic(rawHitsList){
let body = rawHitsList.reduce((accumulator, hit) => {
let docCount = this._getUpdatedDocCount('elastic');
let index = {
_index: config.target.index,
_type: config.target.type
}
if(config.target.useDocCountAsId)
index._id = docCount;
return accumulator.concat([{index}, hit._source])
}, []);
console.log('Docs transferred (elastic): ', this.target.doc_count.elastic);
return this.targetClient.bulk({body});
}
async writeDocsToFile(rawHitsList){
let body = rawHitsList.map(hits => {
let path = config.target.dirPath + config.target.filePath;
let docCount = this._getUpdatedDocCount('file')
let index = {
_index: config.target.index,
_type: config.target.type
}
if(config.target.useDocCountAsId)
index._id = docCount;
return this._writeLineToFile(path, `${JSON.stringify({index})}\n${JSON.stringify(hits._source)}`);
})
console.log('Docs transferred (file): ', this.target.doc_count.file);
return Promise.all(body);
}
async _checkConnection(client, clientName = ''){
await client.ping({requestTimeout: 3000});
console.log(`${clientName} client connected successfully`);
}
_getUpdatedDocCount(target_type){
return ++this.target.doc_count[target_type];
}
_writeLineToFile(path, line){
line = line + '\n';
return new Promise((resolve, reject) => {
fs.appendFile(path, line, err => err? reject(err): resolve());
})
}
_mkdir(path){
return new Promise((resolve, reject) => {
fs.mkdir(path, err => {
//if any error other than 'directory already exists'
if (err && err.code != 'EEXIST')
reject(err);
resolve();
})
})
}
async _transferMapping(source, target){
let sourceMapping = await source.indices.getMapping({
index: config.source.index,
type: config.source.type
})
let mapping = this._buildMappingFrom(sourceMapping);
if(!await target.indices.exists({index: config.target.index}))
await target.indices.create({index: config.target.index});
return await target.indices.putMapping({
index: config.target.index,
type: config.target.type,
body: mapping
});
}
//sourceMapping:
//{<index>: {"mappings": {<type>: {<the mapping>}}}}
//To return:
//{<type>: <mapping>}
_buildMappingFrom(sourceMapping){
let base = {};
let type = Object.assign({}, sourceMapping[config.source.index].mappings[config.source.type]);
//if target 'type' given in config is different from the type in source elastic, change it
if(this._targetConfigCheckIf('type', 'given') && !this._targetConfigCheckIf('type', 'same'))
base[config.target.type] = type;
else
base[config.source.type] = type;
//base = {<type>: <mapping>}
return base;
}
_targetConfigCheckIf(property, flag){
switch(flag){
case 'given': return !!config.target[property];
case 'same': return config.target[property] === config.source[property];
default: throw new Error(`Error with mapping: ${property}`);
}
}
}
module.exports = {Elastic}