Skip to content

Instantly share code, notes, and snippets.

@baybatu
Last active February 4, 2019 06:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baybatu/732a6ed4a9e9e07393aa82d034e66deb to your computer and use it in GitHub Desktop.
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
#!/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