ElasticSearch - Reindexing your data with zero downtime

Michalis picture Michalis · Apr 8, 2015 · Viewed 9.2k times · Source

https://www.elastic.co/blog/changing-mapping-with-zero-downtime/

I try to create a new index and reindexing my data with zero downtime with this guide.

Now I have an index called "photoshooter" and I follow the steps

1) Create new index "photoshooter_v1" with the new mapping... (Done)

2) Create alias...

curl -XPOST localhost:9200/_aliases -d '

{
    "actions": [
        { "add": {
            "alias": "photoshooter",
            "index": "photoshooter_v1"
        }}
    ]
}

and I get this error...

{
    "error": "InvalidAliasNameException[[photoshooter_v1] Invalid alias name [photoshooter], an index exists with the same name as the alias]",
    "status": 400
}

I think I lose something with the logic..

Answer

Akash Yadav picture Akash Yadav · Apr 8, 2015

Lets say your current index is named as "photoshooter " if i am guessing it right ok.

Now Create a Alias for this index first - OK

     {
            "actions": [
                { "add": {
                    "alias": "photoshooter_docs",
                    "index": "photoshooter"
                }}
            ]
        }

test it - curl -XGET 'localhost:9200/photoshooter_docs/_search'

Note - now you will use 'photoshooter_docs' as index name to interact with your index which is actually 'photoshooter' Ok.

Now we create a new index with your new mapping let's say we name it 'photoshooter_v2' now copy your 'photoshooter' index data to new index(photoshooter_v2)

Once you have copied all your data now simply Remove the alias from previous index to new index -

      curl -XPOST localhost:9200/_aliases -d '
        {
            "actions": [
                { "remove": {
                    "alias": "photoshooter_docs",
                    "index": "photoshooter"
                }},
                { "add": {
                    "alias": "photoshooter_docs",
                    "index": "photoshooter_v2"
                }}
            ]
        }

test it again -> curl -XGET 'localhost:9200/photoshooter_docs/_search'

Congrats you have changed your mapping without zero downtime .

And to copy data you can use tools like this

    https://github.com/mallocator/Elasticsearch-Exporter 

Note - this tools also copies the mapping from old index to new index which you might don't want to do. So that you have read in its documentation or edit it according to your use .

Thanks

Hope this helps