elasticsearch 6.8.x 索引别名、动态索引扩展、滚动索引

发布于:2024-03-28 ⋅ 阅读:(18) ⋅ 点赞:(0)

引言

最近查阅了一些资料,了解到关于es索引别名,特别在此记录一下:

  • 索引别名绑定多个索引:隐藏底层索引名,对外开放索引别名进行查询和更新操作

【elasticsearch 6.8.x 官网,关于索引别名】

  • 滚动索引(_rollover):创建的索引和索引别名自动绑定,使用别名对外查询无感知

【elasticsearch 6.8.x官网,关于索引模板创建、引用】

  • 滚动索引(ILM):按照ilm索引生命周期策略,自动换绑索引别名指向的索引,分为四个周期
    • hot:热区,高查询、高修改频词
    • warm:温区,不再更新索引,仍然提供查询服务
    • cold:冷区,索引不再更新,并且很少查询。信息仍然需要可搜索,但如果这些查询速度较慢也没关系
    • delete:不再需要索引,可以安全地删除

【elasticsearch 6.8.x 官网,关于索引生命周期支持】

索引别名(alias)

索引别名的创建时机:

  1. 在创建索引和字段关系映射的时候直接settings索引和索引别名的关系
  2. 在索引已经存在并稳定运行的情况下,通过API【PUT /_aliases】动态绑定索引,能隐藏底层索引的改变,操作对用户无感知

这里不讨论索引别名的routing filter,只讨论索引和索引别名的映射关系,以及是否是写入索引(write index)。

创建索引别名

POST /_aliases

################### 新建别名参数及备注
################### 索引名test_template-202403假设已经是存在的
{
    "actions": [
        {
            "add": {
                // 别名关联的索引名
                "index": "test_template-202403",
                // 别名
                "alias": "tmp_index_alias",
                "is_write_index": false // 默认是缺省的,如果一个别名只绑定了一个索引,那他将允许写入;如果一个别名绑定了多个索引,在不指定is_write_index: true的情况下,使用别名新增数据,会出现拒绝写入的情况
            }
        }
    ]
}

image.png

查询索引别名

GET /_aliases

image.png

删除索引别名

DELETE /_aliases

################### 

{
    "actions": [
        {
            "remove": {
                // 别名关联的索引名
                "index": "test_template-202403",
                // 别名
                "alias": "tmp_index_alias"
            }
        }
    ]
}

在这里插入图片描述

重命名索引别名

POST /_aliases

########################## 别名操作的同一个actions API里面操作是原子性的
########################## 重命名的过程就是删除原先的别名关联,新增新的关联


{
    "actions": [
        {
            "remove": {
                // 别名关联的索引名
                "index": "test_template-202403",
                // 别名
                "alias": "tmp_index_alias"
            }
        },
        {
            "add": {
                // 别名关联的索引名
                "index": "test_template-202403",
                // 别名
                "alias": "tmp_index-2_alias"
            }
        }
    ]
}

在这里插入图片描述

动态索引(index template,动态匹配生成索引)

新建索引模板

es官网,索引模板

# sys-log是索引模板的名称
PUT /_template/sys-log

################### 创建索引模板的参数

{
    // 使用此模板的优先级,数值越大,优先级越高,数值大的索引模板配置会覆盖优先级小的配置
    "order": 2,
    // 索引模板匹配规则:所有创建的索引都会以此模板的配置项生成索引。 sys-log*开头的索引才会使用这个模板
    "index_patterns": ["sys-log*"],
    // 设置索引别名:后续匹配到的所有索引都会使用此别名配置
    "aliases": {
        // 设置一个索引别名
        "sys-log-alias": {}
    },
    "template": {
        "settings": {
            "index": {
                // 两个分片
                "number_of_shards": "2",
                // 0个副本
                "number_of_replicas": "0",
                // 数据插入es后,过10s才能被查到
                "refresh_interval": "10s",
                // 单次最大允许的查询条数
                "max_result_window": "1000"
            }
        },
        // 配置字段映射关系、配置字段类型、配置字段指定分词器
        "mapping": {
            "_doc": {
                "properties": {
                    "id": {
                        "type": "long"
                    },
                    // 操作记录允许分词查询
                    "operation": {
                        "type": "text",
                        // 指定分词器
                        "analyzer": "ik_smart",
                    },
                    // 创建时间
                    "create_date": {
                        "type": "date"
                    }
                }
            }
        }
    }
}

新建索引并插入数据

新建的索引只需要插入数据,关系映射和配置都使用索引模板sys-log

索引sys-log-202402

POST /sys-log-202402/_doc/_bulk

=============>
{"index": {"_id": 1}}
{"id":1,"operation":"查询:/getInfo;参数a=1&b=2","create_date":"2024-02-15"}
{"index": {"_id": 2}}
{"id":2,"operation":"查询:/listPage;参数{page: 1, size: 10}","create_date":"2024-02-16"}
{"index": {"_id": 3}}
{"id":3,"operation":"新增:/addOrUpdate;","create_date":"2024-02-17"}

索引sys-log-202403

POST /sys-log-202403/_doc/_bulk

=============>
{"index": {"_id": 1}}
{"id":1,"operation":"查询:/getInfo;参数a=1&b=2","create_date":"2024-02-15"}
{"index": {"_id": 2}}
{"id":2,"operation":"查询:/listPage;参数{page: 1, size: 10}","create_date":"2024-02-16"}
{"index": {"_id": 3}}
{"id":3,"operation":"新增:/addOrUpdate;","create_date":"2024-02-17"}

索引sys-log-202404

POST /sys-log-202404/_doc/_bulk

=============>
{"index": {"_id": 1}}
{"id":1,"operation":"查询:/getInfo;参数a=1&b=2","create_date":"2024-02-15"}
{"index": {"_id": 2}}
{"id":2,"operation":"查询:/listPage;参数{page: 1, size: 10}","create_date":"2024-02-16"}
{"index": {"_id": 3}}
{"id":3,"operation":"新增:/addOrUpdate;","create_date":"2024-02-17"}

使用动态索引指定的别名查询数据

POST /sys-log-alias/_search

=============> 查询参数
{
    "query": {
        "bool": {
            "must": {
                "match": {
                    "operation": "查询"
                }
            }
        }
    },
    "highlight": {
        "fields": {
            "comment_content": {}
        },
        "number_of_fragments": 0,
        "pre_tags": [
            "<b style='color: blue'>"
        ],
        "post_tags": [
            "</b>"
        ],
        "require_field_match": false,
        "type": "plain"
    },
    "from": 0,
    "size": 10000,
    "sort": [],
    "aggs": {}
}



============================> 返回结果

{
    "took": 5,
    "timed_out": false,
    "_shards": {
        "total": 15,
        "successful": 15,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 6,
        "max_score": 0.5753642,
        "hits": [
            {
                "_index": "sys-log-202402",
                "_type": "_doc",
                "_id": "2",
                "_score": 0.5753642,
                "_source": {
                    "id": 2,
                    "operation": "查询:/listPage;参数{page: 1, size: 10}",
                    "create_date": "2024-02-16"
                }
            },
            {
                "_index": "sys-log-202403",
                "_type": "_doc",
                "_id": "2",
                "_score": 0.5753642,
                "_source": {
                    "id": 2,
                    "operation": "查询:/listPage;参数{page: 1, size: 10}",
                    "create_date": "2024-02-16"
                }
            },
            {
                "_index": "sys-log-202404",
                "_type": "_doc",
                "_id": "2",
                "_score": 0.5753642,
                "_source": {
                    "id": 2,
                    "operation": "查询:/listPage;参数{page: 1, size: 10}",
                    "create_date": "2024-02-16"
                }
            },
            {
                "_index": "sys-log-202402",
                "_type": "_doc",
                "_id": "1",
                "_score": 0.5753642,
                "_source": {
                    "id": 1,
                    "operation": "查询:/getInfo;参数a=1&b=2",
                    "create_date": "2024-02-15"
                }
            },
            {
                "_index": "sys-log-202403",
                "_type": "_doc",
                "_id": "1",
                "_score": 0.5753642,
                "_source": {
                    "id": 1,
                    "operation": "查询:/getInfo;参数a=1&b=2",
                    "create_date": "2024-02-15"
                }
            },
            {
                "_index": "sys-log-202404",
                "_type": "_doc",
                "_id": "1",
                "_score": 0.5753642,
                "_source": {
                    "id": 1,
                    "operation": "查询:/getInfo;参数a=1&b=2",
                    "create_date": "2024-02-15"
                }
            }
        ]
    }
}

通过返回结果分析:
查询结果可以看出索引别名关联的所有索引中结果全部被查询出来,如果指定索引名称查询,那么查询结果就是单独这个索引中的数据。
sys-log-alias --> sys-log-202402
sys-log-alias --> sys-log-202403
sys-log-alias --> sys-log-202404

别名关联索引,拒绝写入异常

但是有一点需要注意,如果对应索引关联的别名未指定is_write_index=true使用别名进行插入会出现拒绝写入的错误。
错误如下:

{
  "error": {
    "root_cause": [
      {
        "type": "illegal_argument_exception",
        "reason": "no write index is defined for alias [sys-log-alias]. The write index may be explicitly disabled using is_write_index=false or the alias points to multiple indices without one being designated as a write index"
      }
    ],
    "type": "illegal_argument_exception",
    "reason": "no write index is defined for alias [sys-log-alias]. The write index may be explicitly disabled using is_write_index=false or the alias points to multiple indices without one being designated as a write index"
  },
  "status": 400
}

如果想要使用别名进行关联的索引数据写入,需要将关联的索引is_write_index字段设置为true:
点击查看【重命名索引别名

POST /_aliases

==============> 因为这个API操作的原子性,在操作的的过程中不会影响到其他
{
    "actions": [
        // 把原关联索引删除
        {
            "remove": {
                // 别名关联的索引名
                "index": "sys-log-202404",
                // 别名
                "alias": "sys-log-alias"
            }
        },
        // 建立新的别名关联索引,并设置is_write_index=true,再次使用别名进行数据写入的时候,数据就自动插入sys-log-202404索引中了
        {
            "add": {
                // 别名关联的索引名
                "index": "sys-log-202404",
                // 别名
                "alias": "sys-log-alias",
                "is_write_index": true
            }
        }
    ]
}

滚动索引(_rollover)

判断索引是否需要进行一次滚动生成时机:

  1. 每次插入数据,判断一次索引是否满足滚动条件
  2. 指定时间,判断一次索引是否满足滚动条件

es官网,关于滚动索引
执行POST /索引别名/_rollover手动滚动索,自动把索引别名换绑到新的索引上,查询和保存都在这一个索引上。滚动索引的名称需要符合滚动的命名规则如:时间 sys-log-20240325 数字sys-log-00001。对外仍然使用一个索引别名,通过不停地换绑索引实现滚动,对外无感知。但,缺点也非常明显,索引一旦换绑,之前的数据将会不可查,除非重新绑定到索引别名上。

关于滚动索引:
image.png
索引滚动满足滚动条件(满足之一)后会创建新索引并换绑:

image.png

创建符合滚动要求的索引

POST /log_rollover-202403-1


===========> 创建参数
{
    "settings": {
        "index": {
            "number_of_shards": "2",
            "number_of_replicas": "0"
        }
    },
    "aliases": {
        "log_rollover_alias": {}
    },
    "mapping": {
        "_doc": {
            "properties": {
                "id": {
                    "type": "long"
                },
                "create_date": {
                    "type": "date"
                },
                "content": {
                    "type": "text"
                }
            }
        }
    }
}

image.png

执行一次索引滚动API

PUT /log_rollover_alias/_rollover

====> 滚动条件判断,满足任何一个滚动条件,就创建一个新索引
{
  "conditions": {
    # 最大文档记录数,滚动
    "max_docs": 5,
    # 索引创建了7天,滚动
    "max_age": "7d",
    # 索引最大存储5gb,滚动
    "max_size": "5gb"
  }
}



===> 返回
{		
    # 索引没有滚动成功,不符合滚动条件
    "acknowledged": false,
    "shards_acknowledged": false,
    "old_index": "log_rollover-202403-1",
    # 如果滚动成功,新生成的索引名将会是log_rollover-202403-000002,别名log_rollover_alias指向
    "new_index": "log_rollover-202403-000002",
    "rolled_over": false,
    "dry_run": false,
    "conditions": {
        "[max_docs: 5]": false,
        "[max_size: 5gb]": false,
        "[max_age: 7d]": false
    }
}

在这里插入图片描述

插入5条测试记录,重新执行滚动API

重新执行滚动索引API

============>返回结果
{
    # 滚动成功
    "acknowledged": true,
    "shards_acknowledged": true,
    "old_index": "log_rollover-202403-1",
    # 别名绑定的新索引名称
    "new_index": "log_rollover-202403-000002",
    "rolled_over": true,
    "dry_run": false,
    "conditions": {
        "[max_docs: 5]": true,
        "[max_size: 5gb]": false,
        "[max_age: 7d]": false
    }
}

查询别名和索引绑定关系

GET /_aliases

=====> 返回

{
    "...": "...",
    "log_rollover-202403-000002": {
        "aliases": {
            "log_rollover_alias": {}
        }
    },
    "log_rollover-202403-1": {
        "aliases": {}
    },
    "...": "..."
}

image.png

缺点:

  1. 一旦索引发生滚动,索引别名和索引关联关系换绑,之前的索引数据将不可查。
  2. 索引滚动之后,之前的分片配置,字段关系也不会被复制过来。
  3. 不能使用索引模板(经测试索引模板中is_write_index设置为写入索引在生成动态索引的时候不会被复用),会出现错误:

Rollover alias [log_rollover-alias] can point to multiple indices, found duplicated alias [[log_rollover-alias]] in index template [log_rollover]

es官网又提供了索引生命周期管理策略,符合要求自动滚动,不再需要去手动执行

滚动索引(ilm:索引生命周期策略,自动滚动)

待测试…ILM生命周期测试滚动情况。

创建自定义的ilm生命周期

es官网,关于创建自定义生命周期
es官网,关于ilm API

PUT /_ilm/policy/my_policy

================> ilm 策略参数
{
    // 策略定义子对象
  "policy": {                       
    "phases": {
      "hot": {                      
        "actions": {
            // 滚动更新操作定义
          "rollover": {             
            "max_size": "50GB",
            "max_age": "30d" // 最大30天属于热门搜索、写入
          }
        }
      },
      "delete": {
        // 删除阶段在 90 天后开始
        "min_age": "90d",      
        // 删除操作定义     
        "actions": {
          "delete": {}              
        }
      }
    }
  }
}

image.png

创建索引模板并引用自定义ilm配置

es官网,关于创建索引并应用ilm策略

创建索引模板

POST /_template/log-policy

==========>
{
    // 使用此模板的优先级,数值越大,优先级越高,数值大的索引模板配置会覆盖优先级小的配置
    "order": 2,
    // 索引模板匹配规则:所有创建的索引都会以此模板的配置项生成索引。 log-policy*开头的索引才会使用这个模板
    "index_patterns": [
        "log-policy-*"
    ],
    // 设置索引别名:后续匹配到的所有索引都会使用此别名配置
    "aliases": {
        // 设置一个索引别名
        "log-policy-alias": {}
    },
    "template": {
        "settings": {
            "index": {
                // 两个分片
                "number_of_shards": "2",
                // 0个副本
                "number_of_replicas": "0",
                // 数据插入es后,过10s才能被查到
                "refresh_interval": "10s",
                // 单次最大允许的查询条数
                "max_result_window": "1000",
                "lifecycle": {
                     // 管理索引的生命周期策略的名称
                    "name": "my_policy",
                     // 用于滚动更新操作的别名,由于在策略中定义了滚动更新操作,因此需要这样做。
                    "rollover_alias": "log-policy-alias",
                }
            }
        }
    },
    // // 配置字段映射关系、配置字段类型、配置字段指定分词器
    // "mapping": {
    //     "_doc": {
    //         "properties": {
    //             "id": {
    //                 "type": "long"
    //             },
    //             // 操作记录允许分词查询
    //             "operation": {
    //                 "type": "text",
    //                 // 指定分词器
    //                 "analyzer": "ik_smart",
    //             },
    //             // 创建时间
    //             "create_date": {
    //                 "type": "date"
    //             }
    //         }
    //     }
    // }
}

创建索引

PUT /log-policy-00001

=======> 使用index template创建索引
{
  "aliases": {
    "log-policy-alias": {
      // 使用索引模板创建索引,并把当前索引指定为可写索引,一遍后面能把索引和策略正确的绑定在一切
      "is_write_index": true
    }
  }
}

查询ilm管理的索引生命周期

GET /log-policy-*/_ilm/explain


====> 返回
{
    "indices": {
        "log-policy-00001": {
            "index": "log-policy-00001",
            # 这里有问题,待测试
            "managed": false
        }
    }
}

参考链接

https://blog.csdn.net/fenglibing/article/details/92069583
https://blog.csdn.net/zqskr_/article/details/134035978
https://blog.csdn.net/Weixiaohuai/article/details/124745320
https://blog.csdn.net/qq_44849679/article/details/136330621
https://blog.csdn.net/feiying0canglang/article/details/129789161
https://zhuanlan.zhihu.com/p/137810661

本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到