-
Notifications
You must be signed in to change notification settings - Fork 0
/
reindex.js
188 lines (177 loc) · 5.98 KB
/
reindex.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
let request = require('superagent');
let fs = require('fs');
let path = require('path');
let config = require('./config');
let ca = fs.readFileSync(path.resolve(__dirname, config.ca));
let indices = config.indices; // List or object holding names of indices to refresh
let settings = config.settings;
let mappings = config.mappings;
let suffix = config.suffix; // Suffix for new indices, eg v2
let indexNames;
if (Array.isArray(indices)) {
indexNames = indices;
} else if (typeof indices === 'object') {
indexNames = Object.keys(indices);
}
/**
* Step 1: Create new indices and copy the mappings from the old index.
* Use refresh_interval = -1 and number_of_replicas = 0 to prepare for the subsequent reindexing.
*/
function createNewIndices() {
let promises = [];
for (indexName of indexNames) {
let endpoint = `https://${config.elasticsearch.host}/${indexName}${suffix}`;
let requestPromise = request.put(endpoint)
.ca(ca)
.send({
// First set to these for efficient reindexing
settings: {
refresh_interval: -1,
number_of_replicas: 0
},
mappings: mappings.mappings
})
.then(function (response) {
console.log(`Index created: ${indexName}${suffix}.`);
return response.body
})
promises.push(requestPromise);
}
return Promise.all(promises)
.then(function (result) {
console.log(JSON.stringify(result, null, 2));
})
.catch(function (error) {
console.log(JSON.stringify(error));
})
}
/**
* Step 2: Reindex documents from the old indices to the new ones.
*/
function reindexDocuments() {
let promises = [];
let reindexResults = {};
let endpoint = `https://${config.elasticsearch.host}/_reindex`;
for (let indexName of indexNames) {
let requestPromise = request.post(endpoint)
.ca(ca)
.send({
source: {
index: indexName
},
dest: {
index: indexName + suffix
}
})
.then(function (response) {
console.log(`Reindexed from ${indexName} to ${indexName}${suffix}.`);
reindexResults[indexName] = {
to: indexName + suffix,
success: true
}
})
.catch(function (error) {
console.log(`Error reindexing from ${indexName} to ${indexName}${suffix}.`);
reindexResults[indexName] = {
to: indexName + suffix,
success: false
}
})
promises.push(requestPromise);
}
return Promise.all(promises)
.then(function (results) {
console.log(JSON.stringify(reindexResults, null, 2));
})
.catch(function (error) {
console.log(JSON.stringify(error));
})
}
/**
* Step 3: Take settings from original indices and apply them to the newly created ones
*/
function resetOldIndicesSettings() {
let promises = [];
let resetResults = {};
for (let indexName of indexNames) {
let endpoint = `https://${config.elasticsearch.host}/${indexName}${suffix}/_settings`;
let requestPromise = request.put(endpoint)
.ca(ca)
.send({
index: settings.settings.index
})
.then(function (response) {
console.log(`Reset settings for ${indexName}${suffix} success.`);
resetResults[indexName + suffix] = {
success: true
}
})
.catch(function (error) {
console.log(`Reset settings for ${indexName}${suffix} failed.`);
resetResults[indexName + suffix] = {
success: false,
error: error
}
})
promises.push(requestPromise);
}
return Promise.all(promises)
.then(function (results) {
console.log(JSON.stringify(resetResults, null, 2));
})
.catch(function (error) {
console.log(JSON.stringify(error));
})
}
/** Step 4: Wait till all the new indices report status green */
/**
* Step 5: In a single update aliases request:
* Delete the old index.
* Add an alias with the old index name to the new index.
* Add any aliases that existed on the old index to the new index.
*/
function updateAliases() {
let endpoint = `https://${config.elasticsearch.host}/_aliases`
let promises = [];
let updateResults = {};
for (let indexName of indexNames) {
let requestPromise = request.post(endpoint)
.ca(ca)
.send({
actions: [
{
add: {
index: indexName + suffix,
alias: indexName
}
},
{
remove_index: {
index: indexName
}
}
]
})
.then(function(response) {
console.log(`Update alias for ${indexName}${suffix} success.`);
updateResults[indexName + suffix] = {
success: true
}
})
.catch(function(error) {
console.log(`Update alias for ${indexName}${suffix} failed.`);
updateResults[indexName + suffix] = {
success: false,
error: error
}
})
promises.push(requestPromise);
}
return Promise.all(promises)
.then(function (results) {
console.log(JSON.stringify(updateResults, null, 2));
})
.catch(function (error) {
console.log(JSON.stringify(error));
})
}