索引生命周期管理 #
使用索引状态管理操作,以编程方式处理策略和托管索引。
创建策略 #
引入版本 1.0
创建一个策略。
请求示例 #
PUT _ilm/policy/ilm_test
{
"policy": {
"phases": {
"hot": {
"min_age": "0ms",
"actions": {
"rollover": {
"max_age": "10m",
"max_size": "1mb",
"max_docs": 100
},
"set_priority": {
"priority": 100
}
}
},
"delete": {
"min_age": "15m",
"actions": {
"delete": {
}
}
}
}
}
}
示例响应 #
{
"acknowledged": true
}
应用生命周期策略到索引模板 要让策略生效,需要在索引模板中指定策略名称和滚动索引的别名。
请求示例 #
PUT /_index_template/my_template?pretty
{
"index_patterns": ["log-test-*"],
"template": {
"settings": {
"number_of_shards": 1,
"number_of_replicas": 1,
"index.lifecycle.name": "ilm_test",
"index.lifecycle.rollover_alias": "log-test"
}
}
}
创建初始托管索引 当您为自己的滚动索引设置策略时,您需要手动创建第一个由策略管理的索引,并将其指定为写入索引。 索引的名称必须与索引模板中定义的模式匹配,并以数字结尾。该数字增加以生成通过rollover操作创建的索引名称。
请求示例 #
PUT /log-test-000001
{
"aliases": {
"log-test":{
"is_write_index": true
}
}
}
获取策略 #
引入版本 1.0
通过 policy_id
获取策略。
请求示例 #
GET _ilm/policy/ilm_test
示例响应 #
{
"_id": "ilm_test",
"_version": 3,
"_seq_no": 27,
"_primary_term": 2,
"policy": {
"policy_id": "ilm_test",
"description": "",
"last_updated_time": 1682049301487,
"schema_version": 17,
"default_state": "hot",
"states": [
{
"name": "hot",
"actions": [
{
"retry": {
"count": 3,
"backoff": "exponential",
"delay": "1m"
},
"rollover": {
"min_size": "1mb",
"min_doc_count": 100,
"min_index_age": "10m"
}
},
{
"retry": {
"count": 3,
"backoff": "exponential",
"delay": "1m"
},
"index_priority": {
"priority": 100
}
}
],
"transitions": [
{
"state_name": "delete",
"conditions": {
"min_index_age": "15m"
}
}
]
},
{
"name": "delete",
"actions": [
{
"retry": {
"count": 3,
"backoff": "exponential",
"delay": "1m"
},
"delete": {}
}
],
"transitions": []
}
],
"ilm_template": [
{
"index_patterns": [],
"priority": 100,
"last_updated_time": 1682049301487
}
]
}
}
解释索引策略 #
引入版本 1.0
获取索引的当前状态。您可以使用索引模式来获取多个索引的状态。
请求示例 #
GET /_ilm/explain/log-test
示例响应 #
{
"log-test-000003": {
"index.lifecycle.name": "ilm_test",
"index": "log-test-000003",
"index_uuid": "58YtMBxaRcGBvOhHcJwQ5w",
"policy_id": "ilm_test",
"policy_seq_no": -2,
"policy_primary_term": 0,
"rolled_over": false,
"index_creation_date": 1682060631245,
"state": {
"name": "hot",
"start_time": 1682060706227
},
"action": {
"name": "rollover",
"start_time": 1682060764614,
"index": 0,
"failed": false,
"consumed_retries": 0,
"last_retry_time": 0
},
"step": {
"name": "attempt_rollover",
"start_time": 1682060764614,
"step_status": "condition_not_met"
},
"retry_info": {
"failed": false,
"consumed_retries": 0
},
"info": {
"message": "Pending rollover of index [index=log-test-000003]",
"conditions": {
"min_index_age": {
"condition": "10m",
"current": "2.2m",
"creationDate": 1682060631245
},
"min_size": {
"condition": "1mb",
"current": "0b"
},
"min_doc_count": {
"condition": 100,
"current": 0
}
}
},
"enabled": true
},
"log-test-000002": {
"index.lifecycle.name": "ilm_test",
"index": "log-test-000002",
"index_uuid": "uxZpQXqdRmGZT5xS1WW6ZA",
"policy_id": "ilm_test",
"policy_seq_no": -2,
"policy_primary_term": 0,
"rolled_over": true,
"index_creation_date": 1682060028261,
"state": {
"name": "hot",
"start_time": 1682060109834
},
"action": {
"name": "transition",
"start_time": 1682060767610,
"index": -1,
"failed": false,
"consumed_retries": 0,
"last_retry_time": 0
},
"step": {
"name": "attempt_transition_step",
"start_time": 1682060767610,
"step_status": "condition_not_met"
},
"retry_info": {
"failed": false,
"consumed_retries": 0
},
"info": {
"message": "Evaluating transition conditions [index=log-test-000002]"
},
"enabled": true
},
"total_managed_indices": 2
}
更新策略 #
需要在请求url里指定 seq_no 和 primary_term ,并注意名称要改成 if_seq_no=7&if_primary_term=1 这种
请求示例 #
PUT _ilm/policy/my_policy?if_seq_no=2545&if_primary_term=1
{
"policy": {
"phases": {
"delete": {
"actions": {
"wait_for_snapshot" : {
"policy": "daily-policy5"
},
"delete": {}
}
}
}
}
}
删除策略 #
引入版本 1.0
通过 policy_id
删除策略。
请求示例 #
DELETE _ilm/policy/ilm_test
示例响应 #
{
"_index": ".easysearch-ilm-config",
"_type": "_doc",
"_id": "ilm_test",
"_version": 2,
"result": "deleted",
"forced_refresh": true,
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 10,
"_primary_term": 1
}
操作 #
forcemerge #
通过合并单个分片的段(segment)来减少 Lucene 段的数量。此操作在开始合并过程之前尝试将索引设置为 read-only 状态。
参数 | 描述 | 类型 | 是否必需 |
---|---|---|---|
max_num_segments | 需要将分片合并到的段数目。 | number | Yes |
PUT /_ilm/policy/my_policy?pretty
{
"policy": {
"phases": {
"warm": {
"actions": {
"forcemerge" : {
"max_num_segments": 1
}
}
}
}
}
}
readonly #
将托管索引设置为只读。
PUT _ilm/policy/my_policy
{
"policy": {
"phases": {
"warm": {
"actions": {
"readonly" : { }
}
}
}
}
}
为托管索引设置 index.blocks.write
索引设置为 true
。注意: 该设置并不能阻止索引刷新。
read_write #
将托管索引设置为可写。
PUT _ilm/policy/my_policy
{
"policy": {
"phases": {
"warm": {
"actions": {
"readonly" : { }
}
}
}
}
}
replica_count #
设置要分配给索引的副本数量。
Parameter | Description | Type | Required |
---|---|---|---|
number_of_replicas | 定义要分配给索引的副本数量。 | number | Yes |
PUT _ilm/policy/my_policy
{
"policy": {
"phases": {
"warm": {
"actions": {
"replica_count": {
"number_of_replicas": 2
}
}
}
}
}
}
close #
关闭托管索引。
关闭的索引仍然存在于磁盘上,但不会消耗 CPU 或内存。关闭的索引不能读取、写入或搜索。
如果您需要保留数据的时间长于需要主动搜索它的时间,并且在数据节点上有足够的磁盘空间,则关闭索引是一个不错的选择。如果需要再次搜索数据,则重新打开关闭的索引比从快照中恢复索引更简单。
open #
打开一个托管索引。
delete #
删除一个托管索引,将其从集群中完全移除。
Parameter | Description | Type | Required |
---|---|---|---|
timestamp_field | 用于判断索引数据年龄的时间戳字段名称。如果指定了此参数,则必须同时指定min_data_age 参数。 | string | No |
min_data_age | 索引中最新数据的最小年龄要求,只有当最新数据的年龄大于此值时才会执行删除操作。必须以天为单位(例如"7d"),并且最小值为1天。 | string | No |
当timestamp_field
和min_data_age
同时指定时,系统会执行以下检查:
- 查询索引中按照指定时间戳字段排序的最新文档
- 计算最新文档的时间与当前时间的差值
- 只有当差值大于或等于
min_data_age
指定的时间时,才会执行删除操作
这种机制可以防止意外删除还在活跃使用的索引数据。
基本用法示例:
PUT _ilm/policy/my_policy
{
"policy": {
"phases": {
"delete": {
"min_age": "30d",
"actions": {
"delete": {}
}
}
}
}
}
带条件检查的删除示例:
PUT _ilm/policy/conditional_delete_policy
{
"policy": {
"phases": {
"delete": {
"min_age": "30d",
"actions": {
"delete": {
"timestamp_field": "@timestamp",
"min_data_age": "90d"
}
}
}
}
}
}
在上面的示例中,即使索引满足了30天的min_age
条件进入删除阶段,也只有当索引中最新的文档(基于@timestamp
字段)已经至少90天未更新时,才会执行实际的删除操作。
rollover #
当现有索引满足指定的滚动条件时,该操作会将目标切换到新索引。
滚动目标可以是数据流或索引别名。当目标为数据流时,新索引将变为数据流的写索引,并且其代数将递增。
如果要滚动索引别名,别名及其写索引必须满足以下条件:
- 索引名称必须符合模式
^.\*-\d+$
,例如(my-index-000001)。 - 必须将 index.lifecycle.rollover_alias 配置为要滚动的别名。
- 索引必须是别名的写索引。
参数 | 描述 | 类型 | 示例 | 是否必需 |
---|---|---|---|---|
max_size | (可选,字节单位)当索引达到一定大小时触发滚动。这是索引中所有主分片的总大小。副本不计算在最大索引大小内。 | string | 20gb or 5mb | No |
max_docs | (可选,整数)在达到指定的最大文档数后触发滚动。自上次刷新后添加的文档不包括在文档计数中。文档计数不包括副本分片中的文档。 | number | 2000000 | No |
max_age | (可选,时间单位)在达到自索引创建开始的最大经过时间后触发滚动。经过的时间始终是从索引创建时间开始计算的,即使索引起始日期被配置为自定义日期. 支持的单位 d (days), h (hours), m (minutes), s (seconds), ms (milliseconds), and micros (microseconds)。 | string | 5d or 7h | No |
PUT _ilm/policy/my_policy
{
"policy": {
"phases": {
"hot": {
"actions": {
"rollover" : {
"max_size": "100gb"
}
}
}
}
}
}
PUT _ilm/policy/my_policy
{
"policy": {
"phases": {
"hot": {
"actions": {
"rollover" : {
"max_docs": 100000000
}
}
}
}
}
}
PUT _ilm/policy/my_policy
{
"policy": {
"phases": {
"hot": {
"actions": {
"rollover" : {
"max_age": "7d"
}
}
}
}
}
}
snapshot #
备份你的集群索引和状态。
snapshot
操作具有以下参数:
参数 | 描述 | 类型 | 是否必须 | 默认 |
---|---|---|---|---|
repository | 通过本机快照 API 操作注册的仓库名称。 | string | Yes | - |
snapshot | 快照的名称。接受字符串和 Mustache 变量 and。如果 Mustache 变量无效,则快照名称默认为索引的名称。 | string or Mustache template | Yes | - |
PUT _ilm/policy/my_policy2
{
"policy": {
"phases": {
"hot": {
"actions": {
"rollover": {
"max_size": "25GB"
}
}
},
"cold": {
"actions": {
"snapshot": {
"repository": "my_repository",
"snapshot": ""
}
}
}
}
}
}
allocate #
将索引分配到具有特定属性集的节点上类似于此。例如,将 require
设置为 warm
,只会将数据移动到“warm”节点上。
allocate
操作有以下参数:
参数 | 描述 | 类型 | 是否必须 |
---|---|---|---|
require | 将索引分配给带有指定属性的节点。 | string | Yes |
include | 将索引分配给具有任何指定属性的节点。 | string | Yes |
exclude | 不将索引分配给具有任何指定属性的节点。 | string | Yes |
以下策略中的分配操作将索引分配给 box_type 为 hot 或 warm 的节点。
PUT _ilm/policy/my_policy
{
"policy": {
"phases": {
"warm": {
"actions": {
"allocate" : {
"include" : {
"box_type": "hot,warm"
}
}
}
}
}
}
}
以下策略中的 allocate 操作会更新索引,使其每个 shard 有一个副本,并将其分配给 box_type 为 cold 的节点。
PUT _ilm/policy/my_policy
{
"policy": {
"phases": {
"warm": {
"actions": {
"allocate": {
"require": {
"box_type": "cold"
}
},
"replica_count": {
"number_of_replicas": 1
}
}
}
}
}
}
wait_for_snapshot #
在删除索引之前,等待执行指定的快照管理策略。这样可以确保已删除索引的快照可用。
wait_for_snapshot
操作具有以下参数:
参数 | 描述 | 类型 | 是否必须 | 默认 |
---|---|---|---|---|
policy | 删除操作应等待的快照管理策略的名称。 | string | Yes | - |
PUT _ilm/policy/my_policy
{
"policy": {
"phases": {
"delete": {
"actions": {
"wait_for_snapshot" : {
"policy": "daily-policy5"
},
"delete": {}
}
}
}
}
}