Set up a time series data stream

This page shows you how to manually set up a time series data stream (TSDS).

Note

If you're working with OpenTelemetry data, try the OpenTelemetry quickstarts.

  1. Create an index template

    The structure of a time series data stream is defined by an index template. You can create the template with the create or update index template API. For example:

    				PUT _index_template/my-weather-sensor-index-template
    					{
      "index_patterns": ["metrics-weather_sensors-*"],
      "data_stream": { },
      "template": {
        "settings": {
          "index.mode": "time_series"
        },
        "lifecycle": {
          "enabled": true,
          "data_retention": "60d"
        },
        "mappings": {
          "properties": {
            "sensor_id": {
              "type": "keyword",
              "time_series_dimension": true
            },
            "location": {
              "type": "keyword",
              "time_series_dimension": true
            },
            "temperature": {
              "type": "half_float",
              "time_series_metric": "gauge"
            },
            "humidity": {
              "type": "half_float",
              "time_series_metric": "gauge"
            },
            "@timestamp": {
              "type": "date"
            }
          }
        }
      },
      "priority": 500,
      "_meta": {
        "description": "Template for my weather sensor data"
      }
    }
    		
    1. Define one or more wildcard patterns matching the name of your TSDS. For best results, use the data stream naming scheme.

    2. The template must include "data_stream": {} to create data streams instead of indices.

    3. Set index.mode to time_series. Optionally, define other relevant index settings, such as index.number_of_replicas, for the data stream's backing indices.

    4. Define a lifecycle to manage retention and storage and to prevent indices from growing too large. In this example, it's a data stream lifecycle with a retention period of 60 days.

      If you omit a data stream lifecycle and an ILM policy, the minimum lifecycle rolls over and tail merges the stream. Configure an explicit data stream lifecycle for retention, downsampling, or frozen searchable snapshots.

      All data streams have a data stream lifecycle and you can update the configuration.

    5. Define mappings for at least one dimension field and typically one or more metric fields.

    6. To define a dimension, set time_series_dimension to true. For details, refer to Dimensions. To define dimensions dynamically, you can use a pass-through object. For details, refer to Defining sub-fields as time series dimensions.

    7. To define a metric, use the time_series_metric mapping parameter. For details, refer to Metrics.

    8. Optionally, define a date or date_nanos mapping for the @timestamp field. If you don't specify a mapping, Elasticsearch maps @timestamp as a date field with default options.

    9. Set the priority higher than 200 to avoid collisions with built-in templates.

    Important

    Without lifecycle management enabled, a time series data stream can grow into very large indices that never roll over. This can lead to performance issues. Always configure lifecycle management for production deployments.

  2. Turn on past index creation (optional)

    Elasticsearch can create missing backing indices when you add data that precedes existing time ranges. To enable this feature, update the cluster settings:

    				PUT _cluster/settings
    					{
      "persistent": {
        "data_stream.past_tsdb_index_creation_enabled": true,
        "data_streams.past_tsdb_index_interval": "2d"
      }
    }
    		
    1. These settings are persistent and apply to all data streams in the cluster.
    2. By default, each past backing index covers one day of data. Refer to data_streams.past_tsdb_index_interval.
  3. Create the time series data stream and add data

    You can create a time series data stream explicitly with the create a data stream API. You must give it a name that matches the index_patterns in your index template. For example:

    				PUT _data_stream/metrics-weather-sensors
    		

    Alternatively, you can create it implicitly by indexing a document. The TSDS is created automatically as long as the index name in your API matches the index template pattern.

    Important

    To add data to your data stream with the following bulk API request or POST request, you must update the timestamps to within two hours of your current time. Data added to a TSDS must fit the accepted time range.

    If you turned on past index creation, Elasticsearch creates backing indices automatically as historical documents are added within the eligible write window. For more details, go to How time-bound indices work.

    				PUT metrics-weather-sensors/_bulk
    					{ "create":{ } }
    { "@timestamp": "2099-05-06T16:21:15.000Z", "sensor_id": "SENSOR-001", "location": "warehouse-A", "temperature": 26.7,"humidity": 49.9 }
    { "create":{ } }
    { "@timestamp": "2099-05-06T16:25:42.000Z", "sensor_id": "SENSOR-002", "location": "warehouse-B", "temperature": 32.4, "humidity": 88.9 }
    				POST metrics-weather-sensors/_doc
    					{
      "@timestamp": "2099-05-06T16:21:15.000Z",
      "sensor_id": "SENSOR-00002",
      "location": "warehouse-B",
      "temperature": 32.4,
      "humidity": 88.9
    }
    		

    For more information about adding documents to data streams, go to Use a data stream > Add documents to a data stream.

  4. Verify setup

    To make sure your time series data stream is working, try some GET requests.

    View data stream details:

    				GET _data_stream/metrics-weather-sensors
    		

    Check the document count in a time series data stream:

    				GET metrics-weather-sensors/_count 
    		

    Query the time series data:

    				GET metrics-weather-sensors/_search 
    					{
      "size": 5,
      "sort": ["@timestamp"]
    }
    		

You can convert an existing data stream to a TSDS only if it uses the standard index mode. Converting a data stream that uses any other index mode, such as logsdb, is not supported.

To convert the data stream, follow these steps:

  1. Update your existing index template and component templates (if any) to include time series settings.
  2. Use the rollover API to manually roll over the existing data stream's write index, to apply the template changes. For example:
				POST metrics-weather-sensors/_rollover
		
Note

After the rollover, new backing indices will have time series functionality. Existing backing indices are not affected by the rollover because their index.mode cannot be changed.

To control access to a TSDS, use index privileges. Privileges set on a TSDS also apply to the backing indices. For an example, refer to Data stream privileges.

Users who write documents that trigger creation of past backing indices need the auto_configure index privilege on the data stream, in addition to privileges that allow indexing (such as create_doc or index). Users with only the index privilege receive a security_exception when a write would create a past backing index.

Now that you've set up a time series data stream, you can manage and use it like a regular data stream. For more information, refer to: