注意: 文章中所有数据都在单节点下、如果是多节点、可以单独设置冷热数据;参考其他文章;
1、设置索引生命周期管理间隔(1s、默认是1小时,主要为了测试进行修改)
PUT _cluster/settings
{
"persistent": {
"indices.lifecycle.poll_interval": "1s"
}
}
2、设置生命周期
一共四个阶段:hot、warm、cold、delete
PUT _ilm/policy/my_custom_policy_filter
{
"policy": {
"phases": {
"hot": {
"actions": {
"set_priority": {
"priority": 100
},
"rollover": {
"max_age": "3d",
"max_docs": 5,
"max_size": "5gb"
}
}
},
"warm": {
"min_age": "15s",
"actions": {
"set_priority": {
"priority": 50
},
"allocate": {
"number_of_replicas": 1,
"include": {
"_name": "",
"_ip": ""
},
"exclude": {
"_name": "",
"_ip": ""
},
"require": {
"_name": "",
"_ip": ""
}
},
"shrink": {
"number_of_shards": 1
},
"forcemerge": {
"max_num_segments": 1
}
}
},
"cold": {
"min_age": "30s",
"actions": {
"set_priority": {
"priority": 0
},
"freeze": {},
"allocate": {
"number_of_replicas": 1,
"include": {
"_name": "",
"_ip": ""
},
"exclude": {
"_name": "",
"_ip": ""
},
"require": {
"_name": "",
"_ip": ""
}
}
}
},
"delete": {
"min_age": "45s",
"actions": {
"delete": {}
}
}
}
}
}
3、创建模版、关联配置的ilm_policy
PUT _index_template/timeseries_template
{
"index_patterns": ["timeseries-*"],
"template":{
"settings": {
"number_of_replicas": 0,
"number_of_shards": 1,
"index.lifecycle.name": "my_custom_policy_filter",
"index.lifecycle.rollover_alias": "timeseries"
}
}
}
4、创建起始索引
PUT timeseries-000001
{
"aliases": {
"timeseries": {
"is_write_index": true
}
}
}
5、插入数据
PUT timeseries/_bulk
{"index": {"_id": 1}}
{"title": "testting 01"}
{"index": {"_id": 2}}
{"title": "testting 02"}
{"index": {"_id": 3}}
{"title": "testting 03"}
{"index": {"_id": 4}}
{"title": "testting 04"}
5.1 生成新的索引
GET timeseries-000002/_search
6、临界值 (插入第5条数据)会滚动
PUT timeseries/_bulk
{"index": {"_id": 5}}
{"title": "testting 05"}
6-1生成滚动索引
GET timeseries-000002/_search
7、在插入一条数据(写入到timeseries-000002 索引中)
PUT timeseries/_bulk
{"index": {"_id": 6}}
{"title": "testting 06"}
GET timeseries/_search