jhilden
jhilden

Reputation: 12449

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?

Upvotes: 7

Views: 3787

Answers (3)

buddemat
buddemat

Reputation: 5301

Elasticsearch >= 7.8

From Elasticsearch 7.8 upwards, composable index templates have been introduced, and the previous template syntax is deprecated (at least for index templates):

#! Legacy index templates are deprecated in favor of composable templates.
#! Deprecated field [template] used, replaced by [index_patterns]

The syntax to create an index template (by the name of my-index-template) that automatically adds the alias my-alias to new indices that match the pattern my-index-* would be:

PUT _index_template/my-index-template
{
  "index_patterns": ["my-index-*"],
  "template": {
    "aliases": {
      "my-alias": { }
    }
  }
}

As with the previous syntax, the behavior is identical: whenever a new index matches the pattern (or patterns, since it can be multiple ones) specified in the index_patterns parameter, the template will automatically be applied to it.

Besides adding an alias, composable index templates can be used for other operations, among them defining settings or mappings, just as in the past.


Elasticsearch < 7.8

For completeness sake, as already described in existing answers, the syntax before there were composable index templates was:

PUT _template/my-index-template
{
    "template" : "my-index-*",
    "aliases" : {
        "my-alias" : { }
    }
}

Upvotes: 1

brianNotBob
brianNotBob

Reputation: 586

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" : { }
    }
}

Upvotes: 5

pickypg
pickypg

Reputation: 22332

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.

Upvotes: 6

Related Questions