Use data stream APIs

Updated at:

The Lindorm search engine supports the data stream feature. You can use API operations to create, read, write, and delete data streams. This feature is ideal for scenarios where both write and query operations include a timestamp field. For example, in logging scenarios, you can write time series log data to a database and efficiently query logs from a specific time range.

Prerequisites

The search engine version must be 3.9.33 or later. To upgrade the current version, see Minor version update.

Time partition read and write principles

  • Write: Data is written to the corresponding backing index based on the specified timestamp.

  • Query: Queries are routed to the corresponding backing indexes based on the timestamp range. If no timestamp condition is specified, a global index query is performed.

image

Create an index template

The Lindorm data stream feature uses a day-based interval parameter based on the UTC time zone. The minimum value is 1 day. When you create a partition, the system automatically calculates the number of backing indexes to create based on the start time and interval. Avoid setting a small interval value. A small value can significantly increase the time required to create the partition because it generates many indexes.

Sample code

PUT _index_template/tp-test
{
  "index_patterns": "tp-test",
  "data_stream": {
    "timestamp_field": {
      "name": "createTime"
    }
  },
  "template": {
    "settings": {
      "number_of_shards": 4,
      "index.mode": "time_partition",
      "index.partition.start": "10d",
      "index.partition.interval": "30d",
      "index.partition.ttl": "360d"
    }
  }
}

Parameters

Parameter Name

Description

index_patterns

Required parameter: The name must match the name of the time partition that you will create.

timestamp_field

Specify the time partition field, such as createTime. This field must be included in all subsequent write operations.

number_of_shards

Specifies the number of shards for each index.

index.mode

The default setting is `time_partition`, which is the time partition pattern.

index.partition.start

Required. Specifies the start time for creating partitions, relative to the current date. For example, 10d means partitions start from 10 days ago.

index.partition.interval

Required. Specifies the time span for each partition. For example, 30d means a new partition is created every 30 days.

index.partition.ttl

Optional. Specifies the time-to-live (TTL). By default, data never expires. For example, 360d means data older than 360 days is deleted to free up space.

Creating time partitions

Creating a time partition automatically applies the configuration of the index template that matches the partition's name.

Important

The time partition name must be the same as the index template's index_patterns.

Sample code

PUT _data_stream/tp-test

Writing to time partitions

The createTime value of the data must be within the time range from index.partition.start to index.partition.future. By default, data with a timestamp earlier than the start time is written to partition 0.

Sample code

POST tp-test/_bulk
{ "index" : { "_id": 10} }
{ "createTime" : "2024-08-01T15:38+0800", "storeId" : 100 }
{ "index" : { "_id": 11} }
{ "createTime" : "2024-08-02T15:39+0800", "storeId" : 200 }

Query time partitions

You can query data by specifying a time range.

Sample code

GET tp-test/_search
{
  "query": {                     
    "range": {
      "createTime": {
          "gte" : "2024-03-11T06:00:00",
          "lt" :  "2024-08-06T12:00:00"
      }
    }             
  }            
}

Deleting a time partition

Deletes a time partition specified by name.

Sample code

DELETE _data_stream/tp-test

View an index template

You can view a specific index template.

Sample code

GET _index_template/tp-test

View time partition configuration

You can view time partition configurations by name.

Sample code

GET _data_stream/tp-test

Modify time partition configuration

You can modify the configuration parameters for time partitions.

Note

The index.partition.start parameter cannot be modified.

Sample code

The following example shows how to modify the index.partition.ttl and index.partition.future parameters.

PUT _data_stream/tp-test/_settings
{
    "index.partition.ttl": "30d"
}

PUT _data_stream/tp-test/_settings
{
    "index.partition.future": "5d"
}