Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ElasticSearch - alias auto update for rolling index

I have the following rolling index defined:

POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "elmah_*",
        "alias": "elmah_all"
      }
    }
  ]
}

That works great today, it picked up all my existing monthly rolling indexes. The problem is that when the index rolls over to a new month it automatically generates the new index of elmah_2016_06, but my alias doesn't pick up this new index. Each month I need to update my alias by running this:

POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "elmah_2016-06",
        "alias": "elmah_all"
      }
    }
  ]
}

Is there any way to have ES pick this up automatically?

like image 788
jhilden Avatar asked Jun 07 '16 21:06

jhilden


2 Answers

Yep, you can use a template.

PUT /_template/my_elmah_template
{
  "template" : "elmah_*",
  "alias" : {
    "elmah_all" : { }
  }
}

The empty object is a necessary evil because JSON expects field : value.

Whenever a matching index (based on the template parameter) is created, then it will automatically apply the template to it. In this case, the only thing that the template is doing is to add an alias to it.

You can also use a template to set settings and mappings.

like image 158
pickypg Avatar answered Sep 23 '22 17:09

pickypg


The answer provided by pickypg is correct, but has a minor error. Should be "aliases" as in:

   PUT /_template/my_elmah_template
   {
       "template" : "elmah_*",
       "aliases" : {
             "elmah_all" : { }
       }
   }
like image 26
brianNotBob Avatar answered Sep 21 '22 17:09

brianNotBob