kibana重建es索引

kibana如何重命名es索引名

背景

在初期设计es索引文档的时候考虑不是很周全,会多出很多无效字段。如果不删除或禁用对后续数据增量以及文档维护会有不良影响。

技术实现

使用 _reindex

1.执行Reindex

bash 复制代码
# 复制旧索引数据到新索引
POST _reindex
{
  "source": {
    "index": "old_index_name"
  },
  "dest": {
    "index": "new_index_name"
  }
}

优化参数

bash 复制代码
POST _reindex?wait_for_completion=false  # 异步执行
{
  "source": {
    "index": "old_index_name",
    "size": 1000  # 分批处理(默认 1000)
  },
  "dest": {
    "index": "new_index_name",
    "op_type": "create"  # 防止覆盖已存在文档
  }
}

2.删除旧索引

bash 复制代码
DELETE /old_index_name

3.更新索引别名

bash 复制代码
# 创建别名
POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "new_index_name",
        "alias": "index_alias"
      }
    }
  ]
}

# 验证别名
GET /_cat/aliases

风险

一.数据一致性风险

1.源索引写入冲突

  • 场景:重建过程中源索引持续写入新数据,导致新旧索引数据不一致

  • 解决方案

    • 全量+增量迁移

      bash 复制代码
      # 1. 全量迁移
      POST _reindex
      {
        "source": {
          "index": "old_index"
        },
        "dest": {
          "index": "new_index"
        }
      }
      
      # 2. 增量迁移(假设存在时间字段 @timestamp)
      POST _reindex
      {
        "source": {
          "index": "old_index",
          "query": {
            "range": {
              "@timestamp": {
                "gte": "now-10m"  # 同步最近10分钟的数据
              }
            }
          }
        },
        "dest": {
          "index": "new_index"
        }
      }
    • 使用别名切换

      bash 复制代码
      # 1. 创建临时别名指向旧索引
      POST /_aliases
      {
        "actions": [
          {
            "add": {
              "index": "old_index",
              "alias": "temp_alias"
            }
          }
        ]
      }
      
      # 2. 重建索引后切换别名
      POST /_aliases
      {
        "actions": [
          {
            "remove": {
              "index": "old_index",
              "alias": "temp_alias"
            }
          },
          {
            "add": {
              "index": "new_index",
              "alias": "temp_alias"
            }
          }
        ]
      }

2.字段类型冲突

  • 场景:源索引目标索引的字段类型不匹配(如源为 text,目标为 keyword

  • 解决方案

    bash 复制代码
    # 1. 提前创建目标索引并指定映射
    PUT /new_index
    {
      "mappings": {
        "properties": {
          "field1": { "type": "keyword" }
        }
      }
    }
    
    # 2. 执行 Reindex 时忽略冲突
    POST _reindex
    {
      "source": {
        "index": "old_index"
      },
      "dest": {
        "index": "new_index"
      },
      "conflicts": "proceed"
    }

二.性能与资源风险

1.集群负载过高

  • 重建大索引(如 TB 级) 导致 CPU/内存 使用率飙升,影响其他业务

  • 解决方案:

    • 分批次处理
    bash 复制代码
    POST _reindex?wait_for_completion=false&slice=0&num_slices=5
    {
      "source": {
        "index": "old_index",
        "size": 10000
      },
      "dest": {
        "index": "new_index"
      }
    }
    • 降低副本数
    bash 复制代码
    PUT /new_index/_settings
    {
      "number_of_replicas": 0
    }

2.磁盘空间不足

  • 场景:重建索引导致磁盘使用率超过85%,触发 es 限流

三.案例

  1. reindex任务中途失败

  2. 源索引与目标索引的文档数量相同,但部分字段值不同

总结

通过以下策略可有效降低风险:

  1. 分阶段操作:全量迁移 > 增量同步 > 别名切换
  2. 资源隔离:使用专有节点或临时扩容集群
  3. 自动化验证:通过脚本对比源与目标索引的数据哈希值
  4. 灰度发布:先重建部分索引(如 10% 数据),验证无误后再全量执行
相关推荐
GUET_一路向前4 小时前
【git工作常用指令】
大数据·git·elasticsearch
Elasticsearch4 小时前
通过 Elastic MCP Server 将 Cursor 连接到生产日志
elasticsearch
身如柳絮随风扬5 小时前
Spring Boot + Spring Cloud 集成 Elasticsearch:从零搭建企业级搜索服务
spring boot·elasticsearch·spring cloud
半部论语6 小时前
CentOS7 + pyenv 安装 Python 3.11 完整指南)
大数据·elasticsearch·python3.11
fan_music6 小时前
git使用教程
大数据·elasticsearch·搜索引擎
Elastic 中国社区官方博客7 小时前
通过受管控的控制平面加速商品陈列优化
大数据·数据库·人工智能·elasticsearch·搜索引擎·平面·ai
逸Y 仙X8 小时前
文章十五:ElasticSearch 运用ingest加工索引数据
java·大数据·elasticsearch·搜索引擎·全文检索
Elastic 中国社区官方博客8 小时前
Kibana 中的查询活动:用于长时间运行搜索的实时控制塔
大数据·运维·elasticsearch·搜索引擎·全文检索·kibana
byoass8 小时前
企业云盘全文检索实战:Elasticsearch集成与分布式搜索
网络·分布式·安全·elasticsearch·云计算·全文检索
Volunteer Technology9 小时前
Elasticsearch分布式原理
大数据·分布式·elasticsearch