Applying a policy to our indexedit

There are a few ways to associate a policy to an index. Since we wish specific settings to be applied to the new index created from Rollover, we will set the policy via index templates.

PUT _template/datastream_template
{
  "index_patterns": ["datastream-*"],                 
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 1,
    "index.lifecycle.name": "datastream_policy",      
    "index.lifecycle.rollover_alias": "datastream"    
  }
}

match all indices starting with "datastream-". These will include all newly created indices from actions like rollover

the name of the lifecycle policy managing the index

alias to use for the rollover action, required since a rollover action is defined in the policy.

The above index template introduces a few new settings specific to ILM. The first being index.lifecycle.name. This setting will configure the "datastream_policy" to the index applying this template. This means that all newly created indices prefixed "datastream-" will be managed by our policy. The other setting used here is index.lifecycle.rollover_alias. This setting is required when using a policy containing the rollover action and specifies which alias to rollover on behalf of this index. The intention here is that the rollover alias is also defined on the index.

To begin, we will want to bootstrap our first index to write to.

PUT datastream-000001
{
  "aliases": {
    "datastream": {
      "is_write_index": true
    }
  }
}

When creating our index, we have to consider a few important configurations that tie our index and our policy together correctly. We need to make sure that our index name matches our index template pattern of "datastream-*", which it does. We are using the Rollover Action in our policy, which requires that our index name ends with a number. In our case, we used 000001. This is important so that Rollover can increment this number when naming the new index created from rolling over.

Our index creation request leverages its template to apply our settings, but we must also configure our rollover alias: "datastream". To do this, we take advantage of write indices. This is a way to define an alias to be used for both reading and writing, with only one index being the index that is being written to at a time. Rollover swaps the write index to be the new index created from rollover, and sets the alias to be read-only for the source index.