1 | const _ = require('lodash');
|
2 | const Promise = require('bluebird');
|
3 | const fs = require('fs');
|
4 |
|
5 | const Utils = function () {
|
6 | const self = this;
|
7 |
|
8 | self.loadFile = (path) => fs.readFileSync(path, 'utf8');
|
9 |
|
10 | self.createIndices = (clients, src, dst) => {
|
11 | return clients.source.indices.create({index: src})
|
12 | .then(() => clients.dest.indices.create({index: dst}));
|
13 | };
|
14 |
|
15 | self.addData = (client) =>
|
16 | client.indices.create({index: 'myindex1'})
|
17 | .then(() => client.indices.create({index: 'myindex2'}))
|
18 | .then(() => client.indices.create({index: 'myindex3'}))
|
19 | .then(() => client.bulk({
|
20 | refresh: true,
|
21 | body: [
|
22 | {index: {_index: 'myindex1', _type: 'mytype1'}},
|
23 | {someField1: 'somedata1'},
|
24 | {index: {_index: 'myindex1', _type: 'mytype1'}},
|
25 | {someField2: 'somedata2'},
|
26 | {index: {_index: 'myindex2', _type: 'mytype1'}},
|
27 | {someField1: 'somedata1'},
|
28 | {index: {_index: 'myindex3', _type: 'mytype3'}},
|
29 | {someField2: 'somedata2'},
|
30 | {index: {_index: 'myindex3', _type: 'mytype3'}},
|
31 | {someField2: 'somedata3'},
|
32 | {index: {_index: 'myindex3', _type: 'mytype3'}},
|
33 | {someField2: 'somedata3'}
|
34 | ]
|
35 | }));
|
36 |
|
37 | self.deleteAllTemplates = (client) =>
|
38 | client.indices.getTemplate()
|
39 | .then((templateNames) => Promise.all(_.keys(templateNames).map((t) => client.indices.deleteTemplate({name: t}))));
|
40 |
|
41 | self.deleteAllIndices = (client) =>
|
42 | client.indices.get({index: '_all'})
|
43 | .then((indexNames) => Promise.all(_.keys(indexNames).map((i) => client.indices.delete({index: i}))));
|
44 | };
|
45 |
|
46 | module.exports = Utils;
|