Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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
You can’t perform that action at this time.