Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ElasticSearch - Reindexing your data with zero downtime

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..

like image 568
Michalis Avatar asked Apr 08 '15 07:04

Michalis


People also ask

How long does it take to reindex Elasticsearch?

Reindexing takes around 2 hours to complete.

What does it mean to reindex Elasticsearch?

Reindex is the concept of copying existing data from a source index to a destination index which can be inside the same or a different cluster. Elasticsearch has a dedicated endpoint _reindex for this purpose. A reindexing is mostly required for updating mapping or settings.


2 Answers

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

like image 184
Akash Yadav Avatar answered Nov 15 '22 07:11

Akash Yadav


It's it very simple, you cannot create an alias with a name of an index that already exists.

You'll need to consider a new name for the new index, re-index the data in the new one and then remove the old one to be able to give it the same name.

If you want to do that on daily basis, you might consider adding per say the date to your index's name and switch upon it every day.

like image 36
eliasah Avatar answered Nov 15 '22 05:11

eliasah