Skip to content

Commit e36abeb

Browse files
add support for temp reindexing
1 parent a305b0f commit e36abeb

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

config/default.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ module.exports = {
4040
API_VERSION: process.env.ES_API_VERSION || '6.8',
4141
ES_INDEX: process.env.ES_INDEX || 'challenge',
4242
ES_TYPE: process.env.ES_TYPE || '_doc', // ES 6.x accepts only 1 Type per index and it's mandatory to define it
43-
ES_REFRESH: process.env.ES_REFRESH || 'true'
43+
ES_REFRESH: process.env.ES_REFRESH || 'true',
44+
TEMP_REINDEXING: process.env.TEMP_REINDEXING || true // if true, it won't delete the existing index when reindexing data
4445
},
4546

4647
// in bytes

src/scripts/update-es-mappings.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,22 @@ function createIndex (indexName) {
3333
}
3434

3535
async function updateMappings () {
36+
const tempReindexing = config.get('ES.TEMP_REINDEXING')
3637
let indexName = config.get('ES.ES_INDEX')
3738
let newIndexName = `${indexName}_tmp_dont_use_for_querying`
3839

40+
if (tempReindexing) {
41+
try {
42+
logger.info(`Attemp to remove temporary index ${newIndexName}`)
43+
await esClient.indices.delete({
44+
index: newIndexName
45+
})
46+
await sleep(500)
47+
} catch (e) {
48+
logger.info(`Index ${newIndexName} does not exist`)
49+
}
50+
}
51+
3952
await createIndex(newIndexName)
4053
await sleep(500)
4154
logger.info(`Reindexing from ${indexName} to ${newIndexName}`)
@@ -47,6 +60,10 @@ async function updateMappings () {
4760
waitForCompletion: true
4861
})
4962

63+
if (tempReindexing) {
64+
return
65+
}
66+
5067
logger.warn(`Deleting ${indexName}. If script crashes after this point data may be lost and a recreation of index will be required.`)
5168

5269
await esClient.indices.delete({

0 commit comments

Comments
 (0)