|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const _ = require('lodash'); |
| 4 | +const Promise = require('bluebird'); |
| 5 | +const isStream = require('is-stream'); |
| 6 | +const elasticsearch = require('elasticsearch'); |
| 7 | + |
| 8 | +/** |
| 9 | + * data is json array of objects or stream |
| 10 | + */ |
| 11 | +module.exports.import = function(data, options, schema) { |
| 12 | + |
| 13 | + options = options || {} |
| 14 | + options.concurrency = options.concurrency || 1 |
| 15 | + options.type = options.type || options.index |
| 16 | + |
| 17 | + if (!options.host) { |
| 18 | + return Promise.reject('Please define host name') |
| 19 | + } |
| 20 | + |
| 21 | + if (!options.index) { |
| 22 | + return Promise.reject('Please provide index name') |
| 23 | + } |
| 24 | + |
| 25 | + if (isStream(data)) { |
| 26 | + data.pause(); |
| 27 | + } |
| 28 | + |
| 29 | + var elastic = new elasticsearch.Client({ |
| 30 | + host: options.host, |
| 31 | + defer: function () { |
| 32 | + return Promise.defer(); |
| 33 | + } |
| 34 | + }); |
| 35 | + |
| 36 | + return Promise.resolve() |
| 37 | + .then(function(res) { |
| 38 | + |
| 39 | + if (schema) { |
| 40 | + |
| 41 | + return elastic.indices.create({ |
| 42 | + index: options.index, |
| 43 | + body: schema |
| 44 | + }) |
| 45 | + } |
| 46 | + }) |
| 47 | + .then(function(res) { |
| 48 | + |
| 49 | + if (isStream(data)) { |
| 50 | + return module.exports.addItemsStream(elastic, data, options) |
| 51 | + } else { |
| 52 | + |
| 53 | + return Promise.all(_.chunk(data, options.chunk_size)) |
| 54 | + .map(items => { |
| 55 | + return module.exports.addBulkItems(elastic, items, options) |
| 56 | + }, { |
| 57 | + concurrency: options.concurrency |
| 58 | + }) |
| 59 | + } |
| 60 | + }) |
| 61 | + .catch(function(res) { |
| 62 | + console.log(res); |
| 63 | + }) |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * import data by stream (file, json, psql, etc) |
| 68 | + */ |
| 69 | +module.exports.addItemsStream = function(elastic, stream, options) { |
| 70 | + |
| 71 | + |
| 72 | + return new Promise(function(resolve, reject) { |
| 73 | + var counter = 0; |
| 74 | + var global_counter = 0; |
| 75 | + var items = []; |
| 76 | + var counter_limit = options.chunk_size || options.limit || 100 |
| 77 | + var concurrency = 1 |
| 78 | + var added = 1 |
| 79 | + |
| 80 | + stream.on('data', function (item) { |
| 81 | + |
| 82 | + ++counter |
| 83 | + items.push(item) |
| 84 | + |
| 85 | + if (counter >= counter_limit) { |
| 86 | + stream.pause(); |
| 87 | + |
| 88 | + return module.exports.addBulkItems(elastic, items, options) |
| 89 | + .then(function(res) { |
| 90 | + counter = 0; |
| 91 | + items = [] |
| 92 | + console.log(added + ' series added!'); |
| 93 | + added++ |
| 94 | + stream.resume() |
| 95 | + }) |
| 96 | + } |
| 97 | + }) |
| 98 | + |
| 99 | + stream.on('end', function (data) { |
| 100 | + |
| 101 | + if (!items.length) { |
| 102 | + return resolve() |
| 103 | + } |
| 104 | + |
| 105 | + module.exports.addBulkItems(elastic, items, options) |
| 106 | + .then(function(res) { |
| 107 | + console.log('Last ' + added + ' series added!'); |
| 108 | + return resolve() |
| 109 | + }) |
| 110 | + }) |
| 111 | + |
| 112 | + stream.on('close', function (data) { |
| 113 | + //console.log('close'); |
| 114 | + return resolve() |
| 115 | + }) |
| 116 | + |
| 117 | + stream.on('error', function (err) { |
| 118 | + //console.log('error'); |
| 119 | + return reject(err) |
| 120 | + }) |
| 121 | + |
| 122 | + /** |
| 123 | + * it waits until creating schema is not finished |
| 124 | + */ |
| 125 | + stream.resume(); |
| 126 | + }) |
| 127 | +} |
| 128 | + |
| 129 | +module.exports.addBulkItems = function(elastic, items, options) { |
| 130 | + |
| 131 | + var body = []; |
| 132 | + for (var i = 0 ; i < items.length ; ++i) { |
| 133 | + var o = { index: { _id: items[i] ? items[i]._id : undefined } }; |
| 134 | + body.push(o); |
| 135 | + body.push(items[i]); |
| 136 | + } |
| 137 | + |
| 138 | + return elastic.bulk({ |
| 139 | + index: options.index, |
| 140 | + //type: options.type, |
| 141 | + body: body |
| 142 | + }) |
| 143 | + .then(v => { |
| 144 | + |
| 145 | + if (options.debug && v.errors) { |
| 146 | + console.log(JSON.stringify(v, null, 2)); |
| 147 | + } |
| 148 | + }) |
| 149 | +} |
0 commit comments