【Elasticsearch】_rollover API详解

解释 POST logs-foo-bar/_rollover

在Elasticsearch中,POST logs-foo-bar/_rollover 是一个API调用,用于触发索引的滚动操作。滚动操作允许您在索引达到一定大小或时间限制时,自动将写入操作切换到一个新的索引,同时保留旧索引的数据。这个操作通常用于管理时间序列数据,例如日志数据,以避免单个索引变得过大。

滚动操作的工作原理
  1. 创建索引模板

    • 首先,您需要创建一个索引模板,定义新索引的结构和设置。

    JSON复制

    复制代码
    PUT _index_template/my_template
    {
      "index_patterns": ["logs-foo-bar-*"],
      "template": {
        "settings": {
          "number_of_shards": 2,
          "number_of_replicas": 1
        }
      }
    }
  2. 创建初始索引并设置别名

    • 创建一个初始索引,并为其设置一个写入别名。

    JSON复制

    复制代码
    PUT logs-foo-bar-000001
    {
      "aliases": {
        "logs-foo-bar-write": {
          "is_write_index": true
        }
      }
    }
  3. 触发滚动操作

    • 使用 _rollover API 触发滚动操作。您可以指定滚动的条件,例如索引中的文档数量、索引的大小或索引的年龄。

    JSON复制

    复制代码
    POST logs-foo-bar-write/_rollover
    {
      "conditions": {
        "max_docs": 1000000,
        "max_size": "50gb",
        "max_age": "30d"
      }
    }
    • 如果当前索引满足滚动条件,Elasticsearch将创建一个新的索引,并将写入别名指向新索引。
  4. 验证滚动结果

    • 您可以通过查询别名来验证滚动操作是否成功。

    JSON复制

    复制代码
    GET logs-foo-bar-write
注意事项
  • 滚动条件

    • max_docs:索引中的最大文档数量。

    • max_size:索引的最大大小。

    • max_age:索引的最大年龄。

    • max_primary_shard_size:主分片的最大大小。

    • max_num_segments:索引的最大段数。

    • min_index_age:索引的最小年龄。

    • min_doc_count:索引中的最小文档数量。

  • 别名管理

    • 写入别名(is_write_index)决定了滚动后旧索引是否仍然可写。如果未配置,旧索引将不可写;如果配置为 true,旧索引将变为只读。
  • 自动化

    • 您可以通过 _rollover API 手动触发滚动操作,也可以使用索引生命周期管理(ILM)来自动化滚动操作。

通过使用 POST logs-foo-bar/_rollover,您可以有效地管理Elasticsearch中的索引,确保数据的可扩展性和查询性能。

相关推荐
周杰伦_Jay1 天前
【Elasticsearch】核心概念,倒排索引,数据操纵
大数据·elasticsearch·搜索引擎
cai_cai01 天前
springAlibaba + ollama + es 完成RAG知识库功能
大数据·elasticsearch·搜索引擎
库库茯苓1 天前
Kibana报错:Unable to retrieve version information from Elasticsearch nodes (解决方法)Window11环境
elasticsearch·kibana
nhdh1 天前
ELK(elasticsearch-7.6.2,kibana-7-6-2,Logstash-7.6.2)单节点部署
大数据·elk·elasticsearch
新元代码1 天前
Git在Windows环境下的安装与使用教程
大数据·elasticsearch·搜索引擎
小园子的小菜1 天前
深入理解Elasticsearch内部线程池:类型与核心作用解析
大数据·elasticsearch·搜索引擎
ZePingPingZe1 天前
SpringBoot整合Sharding-jdbc分库分表及ES搜索引擎解决无分片键查询
spring boot·elasticsearch·搜索引擎
Mr.wangh1 天前
SpringCloudConfig(配置中心)
大数据·elasticsearch·搜索引擎·springcloud·config
小园子的小菜1 天前
深度解析Elasticsearch网络通信原理:节点协同与链接机制
大数据·elasticsearch·搜索引擎
Tipriest_1 天前
详细解释pip及其使用方法(对比apt)
大数据·elasticsearch·apt·pip