lib/sync.js
function (next) {

            var reqOpts = {
                method: 'GET',
                url: helpers.makeAliasUri(options)
            }

            if(options.auth) {
                reqOpts.auth = {
                    user: options.auth.user,
                    pass: options.auth.password,
                    sendImmediately: false
                };
            }

            helpers.backOffRequest(reqOpts, function (err, res, body) {
                if (err) {
                    return next(err)
                }

                var existingIndices = Object.keys(body)

                // console.log('\nexistingIndices', body)

                var aliasName = helpers.makeIndexName(options)

                // get all aliases pertaining to this collection & are not the index we just created
                // we will remove all of them
                indicesToRemove = existingIndices.filter(function (indexName) {
                    return (indexName.indexOf(aliasName) === 0 && indexName !== versionedIndexName)
                })

                // console.log('\nindicesToRemove', indicesToRemove)

                // generate elasticsearch request body to atomically remove old indices and add the new one
                var requestBody = {
                    actions: [
                        {
                            add: {
                                alias: aliasName,
                                index: versionedIndexName
                            }
                        }
                    ]
                }

                indicesToRemove.forEach(function (indexToRemove) {
                    var removeObj = {
                        remove: {
                            alias: aliasName,
                            index: indexToRemove
                        }
                    }

                    requestBody.actions.unshift(removeObj)
                })

                var reqOpts = {
                    method: 'POST',
                    url: helpers.makeAliasUri(options),
                    body: JSON.stringify(requestBody)
                }

                if(options.auth) {
                    reqOpts.auth = {
                        user: options.auth.user,
                        pass: options.auth.password,
                        sendImmediately: false
                    };
                }

                helpers.backOffRequest(reqOpts, function (err, res, body) {
                    if (err) {
                        return next(err)
                    }

                    if (!helpers.elasticsearchBodyOk(body)) {
                        var error = new Error('Alias deletion error. Elasticsearch reply:'+util.inspect(body, true, 10, true))
                        error.elasticsearchReply = body
                        error.elasticsearchRequestBody = requestBody
                        return next(error)
                    }

                    return next()
                })
            })
        }
function (next) {

            var reqOpts = {
                method: 'GET',
                url: helpers.makeAliasUri(options)
            }

            if(options.auth) {
                reqOpts.auth = {
                    user: options.auth.user,
                    pass: options.auth.password,
                    sendImmediately: false
                };
            }

            helpers.backOffRequest(reqOpts, function (err, res, body) {
                if (err) {
                    return next(err)
                }

                var existingIndices = Object.keys(body)

                // console.log('\nexistingIndices', body)

                var aliasName = helpers.makeIndexName(options)

                // get all aliases pertaining to this collection & are not the index we just created
                // we will remove all of them
                indicesToRemove = existingIndices.filter(function (indexName) {
                    return (indexName.indexOf(aliasName) === 0 && indexName !== versionedIndexName)
                })

                // console.log('\nindicesToRemove', indicesToRemove)

                // generate elasticsearch request body to atomically remove old indices and add the new one
                var requestBody = {
                    actions: [
                        {
                            add: {
                                alias: aliasName,
                                index: versionedIndexName
                            }
                        }
                    ]
                }

                indicesToRemove.forEach(function (indexToRemove) {
                    var removeObj = {
                        remove: {
                            alias: aliasName,
                            index: indexToRemove
                        }
                    }

                    requestBody.actions.unshift(removeObj)
                })

                var reqOpts = {
                    method: 'POST',
                    url: helpers.makeAliasUri(options),
                    body: JSON.stringify(requestBody)
                }

                if(options.auth) {
                    reqOpts.auth = {
                        user: options.auth.user,
                        pass: options.auth.password,
                        sendImmediately: false
                    };
                }

                helpers.backOffRequest(reqOpts, function (err, res, body) {
                    if (err) {
                        return next(err)
                    }

                    if (!helpers.elasticsearchBodyOk(body)) {
                        var error = new Error('Alias deletion error. Elasticsearch reply:'+util.inspect(body, true, 10, true))
                        error.elasticsearchReply = body
                        error.elasticsearchRequestBody = requestBody
                        return next(error)
                    }

                    return next()
                })
            })
        }
function ( next ) { var reqOpts = { method : 'GET' , url : helpers . makeAliasUri ( options ) } if ( options . auth ) { reqOpts . auth = { user : options . auth . user , pass : options . auth . password , sendImmediately : false } ; } helpers . backOffRequest ( reqOpts , function ( err , res , body ) { if ( err ) { return next ( err ) } var existingIndices = Object . keys ( body ) // console.log('\nexistingIndices', body) var aliasName = helpers . makeIndexName ( options ) // get all aliases pertaining to this collection & are not the index we just created // we will remove all of them indicesToRemove = existingIndices . filter ( function ( indexName ) { return ( indexName . indexOf ( aliasName ) === 0 && indexName !== versionedIndexName ) } ) // console.log('\nindicesToRemove', indicesToRemove) // generate elasticsearch request body to atomically remove old indices and add the new one var requestBody = { actions : [ { add : { alias : aliasName , index : versionedIndexName } } ] } indicesToRemove . forEach ( function ( indexToRemove ) { var removeObj = { remove : { alias : aliasName , index : indexToRemove } } requestBody . actions . unshift ( removeObj ) } ) var reqOpts = { method : 'POST' , url : helpers . makeAliasUri ( options ) , body : JSON . stringify ( requestBody ) } if ( options . auth ) { reqOpts . auth = { user : options . auth . user , pass : options . auth . password , sendImmediately : false } ; } helpers . backOffRequest ( reqOpts , function ( err , res , body ) { if ( err ) { return next ( err ) } if ( ! helpers . elasticsearchBodyOk ( body ) ) { var error = new Error ( 'Alias deletion error. Elasticsearch reply:' + util . inspect ( body , true , 10 , true ) ) error . elasticsearchReply = body error . elasticsearchRequestBody = requestBody return next ( error ) } return next ( ) } ) } ) }
atomically replace existing aliases for this collection with the new one we just created
atomically replace existing aliases for this collection with the new one we just created