forked from voldern/elasticsearch-bulk-index-stream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
178 lines (145 loc) · 4.54 KB
/
index.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
'use strict';
var util = require('util'),
assert = require('assert'),
FlushWritable = require('flushwritable'),
_ = require('lodash');
util.inherits(ElasticsearchBulkIndexWritable, FlushWritable);
/**
* Transform records into a format required by Elasticsearch bulk API
*
* @private
* @param {array} records
* @return {array}
*/
function transformRecords(records) {
return records.reduce(function(bulkOperations, record) {
assert(record.index, 'index is required');
assert(record.type, 'type is required');
assert(record.body, 'body is required');
var index = {
_index: record.index,
_type: record.type,
_id: record.id
};
if (record.parent) {
index._parent = record.parent;
}
bulkOperations.push({
index: index
});
bulkOperations.push(record.body);
return bulkOperations;
}, []);
}
/**
* A simple wrapper around Elasticsearch for bulk writing items
*
* @param {Elasticsearch.Client} client Elasticsearch client
* @param {Object} options Options
* @param {number} [options.highWaterMark=16] Number of items to buffer before writing.
* Also the size of the underlying stream buffer.
* @param {number} [options.flushTimeout=null] Number of ms to flush records after, if highWaterMark hasn't been reached
* @param {Object} [options.logger] Instance of a logger like bunyan or winston
*/
function ElasticsearchBulkIndexWritable(client, options) {
assert(client, 'client is required');
options = options || {};
options.objectMode = true;
FlushWritable.call(this, options);
this.client = client;
this.logger = options.logger || null;
this.highWaterMark = options.highWaterMark || 16;
this.flushTimeout = options.flushTimeout || null;
this.writtenRecords = 0;
this.queue = [];
}
/**
* Bulk write records to Elasticsearch
*
* @private
* @param {array} records
* @param {Function} callback
*/
ElasticsearchBulkIndexWritable.prototype.bulkWrite = function bulkWrite(records, callback) {
this.client.bulk({ body: records }, function bulkCallback(err, data) {
if (err) {
err.records = records;
return callback(err);
}
if (data.errors === true) {
var errors = data.items.map(function(item) {
return _.map(item, 'error')[0];
});
errors = _.uniq(_.filter(errors, _.isString));
if (this.logger) {
errors.forEach(this.logger.error.bind(this.logger));
}
var error = new Error(errors);
error.records = records;
return callback(error);
}
callback();
}.bind(this));
};
/**
* Flush method needed by the underlying stream implementation
*
* @private
* @param {Function} callback
* @return {undefined}
*/
ElasticsearchBulkIndexWritable.prototype._flush = function _flush(callback) {
clearTimeout(this.flushTimeoutId);
if (this.queue.length === 0) {
return callback();
}
try {
var records = transformRecords(this.queue);
} catch (error) {
return callback(error);
}
var recordsCount = this.queue.length;
this.queue = [];
if (this.logger) {
this.logger.debug('Writing %d records to Elasticsearch', recordsCount);
}
this.bulkWrite(records, function(err) {
if (err) {
return callback(err);
}
if (this.logger) {
this.logger.info('Wrote %d records to Elasticsearch', recordsCount);
}
this.writtenRecords += recordsCount;
callback();
}.bind(this));
};
/**
* Write method needed by the underlying stream implementation
*
* @private
* @param {Object} record
* @param {string} enc
* @param {Function} callback
* @returns {undefined}
*/
ElasticsearchBulkIndexWritable.prototype._write = function _write(record, enc, callback) {
if (this.logger) {
this.logger.debug('Adding to Elasticsearch queue', { record: record });
}
this.queue.push(record);
if (this.queue.length >= this.highWaterMark) {
return this._flush(callback);
} else if (this.flushTimeout) {
clearTimeout(this.flushTimeoutId);
this.flushTimeoutId = setTimeout(function() {
this._flush(function(err) {
if (err) {
this.emit('error', err);
}
}.bind(this));
}.bind(this), this.flushTimeout);
}
callback();
};
module.exports = ElasticsearchBulkIndexWritable;