Set up a data stream

edit

To set up a data stream, follow these steps:

Optional: Configure an ILM lifecycle policy

edit

While optional, we recommend you configure an index lifecycle management (ILM) policy to automate the management of your data stream’s backing indices.

In Kibana, open the menu and go to Stack Management > Index Lifecycle Policies. Click Index Lifecycle Policies.

Index Lifecycle Policies page
API example

Use the create lifecycle policy API to configure a policy:

PUT /_ilm/policy/my-data-stream-policy
{
  "policy": {
    "phases": {
      "hot": {
        "actions": {
          "rollover": {
            "max_size": "25GB"
          }
        }
      },
      "delete": {
        "min_age": "30d",
        "actions": {
          "delete": {}
        }
      }
    }
  }
}

Create an index template

edit
  1. In Kibana, open the menu and go to Stack Management > Index Management.
  2. In the Index Templates tab, click Create template.
  3. In the Create template wizard, use the Data stream toggle to indicate the template is used for data streams.
  4. Use the wizard to finish defining your template. Specify:

    • One or more index patterns that match the data stream’s name.
    • Mappings and settings for the stream’s backing indices.
    • A priority for the index template

      Elasticsearch has built-in index templates for the metrics-*-* and logs-*-* index patterns, each with a priority of 100. Elastic Agent uses these templates to create data streams.

      If you use Elastic Agent, assign your index templates a priority lower than 100 to avoid overriding the built-in templates. Otherwise, use a non-overlapping index pattern or assign templates with an overlapping pattern a priority higher than 100.

      For example, if you don’t use Elastic Agent and want to create a template for the logs-* index pattern, assign your template a priority of 200. This ensures your template is applied instead of the built-in template for logs-*-*.

If the index template doesn’t specify a mapping for the @timestamp field, Elasticsearch maps @timestamp as a date field with default options.

If using ILM, specify your lifecycle policy in the index.lifecycle.name setting.

Carefully consider your template’s mappings and settings. Later changes may require reindexing. See Change mappings and settings for a data stream.

Create template page
API example

Use the put index template API to create an index template. The template must include an empty data_stream object, indicating it’s used for data streams.

PUT /_index_template/my-data-stream-template
{
  "index_patterns": [ "my-data-stream*" ],
  "data_stream": { },
  "priority": 200,
  "template": {
    "settings": {
      "index.lifecycle.name": "my-data-stream-policy"
    }
  }
}

Create the data stream

edit

To automatically create the data stream, submit an indexing request to the stream. The stream’s name must match one of your template’s index patterns.

POST /my-data-stream/_doc/
{
  "@timestamp": "2020-12-06T11:04:05.000Z",
  "user": {
    "id": "vlb44hny"
  },
  "message": "Login attempt failed"
}

You can also use the create data stream API to manually create the data stream. The stream’s name must match one of your template’s index patterns.

PUT /_data_stream/my-data-stream-alt

Secure the data stream

edit

To control access to the data stream and its data, use Elasticsearch’s security features.

Get information about a data stream

edit

In Kibana, open the menu and go to Stack Management > Index Management. In the Data Streams tab, click the data stream’s name.

Data Streams tab
API example

Use the get data stream API to retrieve information about one or more data streams:

GET /_data_stream/my-data-stream

Delete a data stream

edit

To delete a data stream and its backing indices, open the Kibana menu and go to Stack Management > Index Management. In the Data Streams tab, click the trash can icon.

Data Streams tab
API example

Use the delete data stream API to delete a data stream and its backing indices:

DELETE /_data_stream/my-data-stream