Last active
February 4, 2019 06:08
-
-
Save baybatu/732a6ed4a9e9e07393aa82d034e66deb to your computer and use it in GitHub Desktop.
ReIndex Elasticsearch index from one to another on Jenkinsfile. Example transfer happens from `products_v1` to `products_v2` index
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env groovy | |
node { | |
stage('checkout') { | |
checkout scm | |
} | |
stage("Reindex") { | |
String esHost = params.esHost //example: localhost:9200 | |
String oldIndexVersion = params.oldIndexVersion //example: v1 | |
String newIndexVersion = params.newIndexVersion //example: v2 | |
def productIndices = sh(script: "curl -f -XGET \"$esHost/products_*\"", returnStdout: true) | |
def allIndexNames = new groovy.json.JsonSlurper().parseText(productIndices).keySet() | |
def oldIndexNames = allIndexNames.findAll { idx -> idx.endsWith(oldIndexVersion) } | |
def newIndexNames = oldIndexNames.collect { idx -> idx.replaceAll(oldIndexVersion, newIndexVersion)} | |
if (!allIndexNames.containsAll(newIndexNames)) { | |
println("All indices:" + allIndexNames) | |
println("New indices:" + newIndexNames) | |
error "All new indices for '${newIndexVersion}' must be created before reindexing on ${esHost}!" | |
} | |
for (idx in oldIndexNames) { | |
String newIdx = idx.replaceAll(oldIndexVersion, newIndexVersion) | |
println("Reindexing from ${idx} to ${newIdx}") | |
sh """ | |
curl -f -XPOST "$esHost/_reindex" -H 'Content-Type: application/json' -d' | |
{ | |
"source": { | |
"index": "'${idx}'" | |
}, | |
"dest": { | |
"index": "'${newIdx}'" | |
} | |
}' | |
""" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment