UNPKG

2.57 kBJavaScriptView Raw
1// Generated by CoffeeScript 1.11.1
2(function() {
3 module.exports = function(client) {
4 return function(bulk, callback) {
5 return bulk.reduce(function(p, c) {
6 return p.then(function() {
7 var number_of_replicas, number_of_shards, putsettings, ref, ref1, ref2, t;
8 t = c.settings;
9 ref2 = (ref = (ref1 = t._settings) != null ? ref1.index : void 0) != null ? ref : {}, number_of_replicas = ref2.number_of_replicas, number_of_shards = ref2.number_of_shards;
10 if (t._settings.index) {
11 delete t._settings.index.number_of_replicas;
12 delete t._settings.index.number_of_shards;
13 }
14 putsettings = function() {
15 var attempt;
16 attempt = function() {
17 return (t._settings.analysis ? client.indices.close({
18 index: t._index
19 }) : Promise.resolve()).then(function() {
20 return client.indices.putSettings({
21 index: t._index,
22 body: t._settings
23 });
24 }).then(function(res) {
25 return (t._settings.analysis ? client.indices.open({
26 index: t._index
27 }) : Promise.resolve()).then(function() {
28 return res;
29 });
30 })["catch"](function(err) {
31 var ref3, ref4;
32 if (((ref3 = err.body) != null ? (ref4 = ref3.error) != null ? ref4.type : void 0 : void 0) === 'index_primary_shard_not_allocated_exception') {
33 return attempt();
34 } else {
35 throw err;
36 }
37 });
38 };
39 return attempt();
40 };
41 return putsettings()["catch"](function(err) {
42 var opts;
43 if (err.status === 404) {
44 opts = {
45 index: t._index,
46 body: {}
47 };
48 if (number_of_replicas != null) {
49 opts.body.number_of_replicas = number_of_replicas;
50 }
51 if (number_of_shards != null) {
52 opts.body.number_of_shards = number_of_shards;
53 }
54 return client.indices.create(opts).then(putsettings);
55 } else {
56 throw err;
57 }
58 });
59 });
60 }, Promise.resolve()).then(function() {
61 return callback(null, {});
62 })["catch"](function(err) {
63 return callback(err);
64 });
65 };
66 };
67
68}).call(this);
69
70//# sourceMappingURL=write-settings.js.map