Skip to content

Commit 9381f92

Browse files
committed
Dynamic index support for elasticsearch
Dynamic indeces are much more flexible, so it log entry has a timestamp (typically it does) - we can build dynamic index name for elasticsearch.
1 parent 729f9ba commit 9381f92

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

handlers/shipElasticsearch.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,22 @@ exports.process = function(config) {
4343
};
4444

4545
_.forEach(config.data, function(datum) {
46+
47+
var indexName = config.elasticsearch.index;
48+
49+
// if date is available, build dynamic index: [index]-YYYY.MM.DD
50+
if (config.dateField) {
51+
var timestamp = new Date(datum[config.dateField]);
52+
indexName = [
53+
indexName + '-' + timestamp.getUTCFullYear(), // year
54+
('0' + (timestamp.getUTCMonth() + 1)).slice(-2), // month
55+
('0' + timestamp.getUTCDate()).slice(-2) // day
56+
].join('.');
57+
}
58+
4659
docs.push({
4760
index: {
48-
_index: config.elasticsearch.index,
61+
_index: indexName,
4962
_type: config.elasticsearch.type
5063
}
5164
});

0 commit comments

Comments
 (0)