5

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

Michalis
  • 6,686
  • 13
  • 52
  • 78
  • Did you get to make this happen? ES docs never mentioned that what you are trying to do is not possible, but their example explicitly add alias to multiple indices as a part of single operation. – tishma May 18 '16 at 16:40
  • Hey, I did not read the error message properly :) I was understanding that it won't let you apply the same alias to more than 1 index. – tishma May 18 '16 at 16:48

2 Answers2

8

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

Zoltán
  • 21,321
  • 14
  • 93
  • 134
Akash Yadav
  • 861
  • 7
  • 11
  • How can i copy the data from the old one to the new index. Should I change the index name from my app? – Michalis Apr 08 '15 at 11:19
  • To copy data from one index to another you can use tools like this https://github.com/mallocator/Elasticsearch-Exporter There are many other toots available, you can use any one . Just read documentation for the tool and run it according to you project need . – Akash Yadav Apr 08 '15 at 14:51
  • To copy data elasticsearch has an option with basic _reindex. Have not tried. https://www.elastic.co/guide/en/elasticsearch/reference/7.3/docs-reindex.html – user1769790 Aug 07 '19 at 17:00
1

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.

eliasah
  • 39,588
  • 11
  • 124
  • 154
  • if i remove the old one, i will lose all the data!! i want zero downtime... If new data append or change to my index while copy my data to the the new index.. it will be problem – Michalis Apr 08 '15 at 11:27
  • you have to re-index the data in the new one and then remove the old index. There is no other way around. – eliasah Apr 08 '15 at 11:35
  • This is the way... http://www.elastic.co/guide/en/elasticsearch/guide/master/index-aliases.html but I'm missing something... "To start off, create the index my_index_v1, and set up the alias my_index to point to it:" but when I try this... i get this error... { "error": "InvalidAliasNameException[[photoshooter_v1] Invalid alias name [photoshooter], an index exists with the same name as the alias]", "status": 400 } – Michalis Apr 08 '15 at 11:41
  • because you are creating an alias that has the same name that an existing index! As I already answered. can you try to update your question with the indices table you have along with the aliases using the cat API – eliasah Apr 08 '15 at 11:47