From 9381f92c0bb6ccc27c707ff813cfaf992713186d Mon Sep 17 00:00:00 2001 From: Dmitry Kolomiets Date: Thu, 16 Mar 2017 10:37:40 +0300 Subject: [PATCH 1/3] 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. --- handlers/shipElasticsearch.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/handlers/shipElasticsearch.js b/handlers/shipElasticsearch.js index 0365907..f45a61b 100644 --- a/handlers/shipElasticsearch.js +++ b/handlers/shipElasticsearch.js @@ -43,9 +43,22 @@ exports.process = function(config) { }; _.forEach(config.data, function(datum) { + + var indexName = config.elasticsearch.index; + + // if date is available, build dynamic index: [index]-YYYY.MM.DD + if (config.dateField) { + var timestamp = new Date(datum[config.dateField]); + indexName = [ + indexName + '-' + timestamp.getUTCFullYear(), // year + ('0' + (timestamp.getUTCMonth() + 1)).slice(-2), // month + ('0' + timestamp.getUTCDate()).slice(-2) // day + ].join('.'); + } + docs.push({ index: { - _index: config.elasticsearch.index, + _index: indexName, _type: config.elasticsearch.type } }); From 289301aba44a070794bc5ec7453c39d8d3543fe1 Mon Sep 17 00:00:00 2001 From: Dmitry Kolomiets Date: Thu, 16 Mar 2017 13:56:43 +0300 Subject: [PATCH 2/3] Fix style error --- handlers/shipElasticsearch.js | 1 - 1 file changed, 1 deletion(-) diff --git a/handlers/shipElasticsearch.js b/handlers/shipElasticsearch.js index f45a61b..5b43e68 100644 --- a/handlers/shipElasticsearch.js +++ b/handlers/shipElasticsearch.js @@ -43,7 +43,6 @@ exports.process = function(config) { }; _.forEach(config.data, function(datum) { - var indexName = config.elasticsearch.index; // if date is available, build dynamic index: [index]-YYYY.MM.DD From da2490ddafd19fa580769ee58d3ead4d68d98d5c Mon Sep 17 00:00:00 2001 From: Dmitry Kolomiets Date: Thu, 16 Mar 2017 14:23:57 +0300 Subject: [PATCH 3/3] Add unit test to verify dynamic index name functionality --- handlers/shipElasticsearch.js | 2 +- test/shipElasticsearch.spec.js | 50 ++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/handlers/shipElasticsearch.js b/handlers/shipElasticsearch.js index 5b43e68..b124400 100644 --- a/handlers/shipElasticsearch.js +++ b/handlers/shipElasticsearch.js @@ -46,7 +46,7 @@ exports.process = function(config) { var indexName = config.elasticsearch.index; // if date is available, build dynamic index: [index]-YYYY.MM.DD - if (config.dateField) { + if (config.dateField && datum[config.dateField]) { var timestamp = new Date(datum[config.dateField]); indexName = [ indexName + '-' + timestamp.getUTCFullYear(), // year diff --git a/test/shipElasticsearch.spec.js b/test/shipElasticsearch.spec.js index e7c47fe..b410fa9 100644 --- a/test/shipElasticsearch.spec.js +++ b/test/shipElasticsearch.spec.js @@ -172,5 +172,55 @@ describe('handler/shipElasticsearch.js', function() { done(); }); }); + + it('should generate dynamic index name if date field is present', + function(done) { + var config = { + elasticsearch: { + index: 'index', + type: 'type', + useAWS: true, + region: 'us-east-1' + }, + data: [ + { + value: '1', + date: '2017-03-06T21:28:58.872Z' + }, + { + value: '2' + } + ], + dateField: 'date' + }; + + var es = require('elasticsearch'); + es.Client = function(config) { + assert.strictEqual(config.amazonES.credentials.envPrefix, 'AWS', 'environment credentials provided'); + assert.strictEqual(config.amazonES.region, 'us-east-1', 'region param provided'); + + return { + // fake 'bulk' method to verify messages to Elasticsearch + bulk: function(docs, callback) { + assert.strictEqual(docs.body.length, 4, 'exactly two messages to Elasticsearch'); + + var messages = docs.body; + assert.strictEqual(messages[0].index._index, 'index-2017.03.06', 'dynamic index contains date'); + assert.strictEqual(messages[0].index._type, 'type', 'type of the message passed along'); + assert.strictEqual(messages[1].value, '1', 'value of the message passed along'); + assert.strictEqual(messages[1].date, '2017-03-06T21:28:58.872Z', 'value of the message passed along'); + + assert.strictEqual(messages[2].index._index, 'index', 'index does not contain date'); + assert.strictEqual(messages[2].index._type, 'type', 'type of the message passed along'); + assert.strictEqual(messages[3].value, '2', 'value of the message passed along'); + + callback(null, 'success'); + done(); + } + }; + }; + + handler.process(config); + }); }); });