李锋镝的博客

  • 首页
  • 时间轴
  • 说说
  • 左邻右舍
  • 博友圈
  • 关于我
    • 关于我
    • 另一个网站
    • 我的导航站
    • 网站地图
    • 赞助
  • 留言
  • 走心评论
  • 系列文章
  • Now
  • 每日心情
  • 🚇开往
Destiny
自是人生长恨水长东
  1. 首页
  2. 中间件
  3. 正文

Elasticsearch零停机迁移数据

2019年12月11日 约 793 字3 分钟 5点热度 0人点赞 0条评论
本文最后更新于 2025年11月20日,距今已 247 天,其中的信息可能已经发生变化,请注意甄别。

创建索引

创建一个名为my_index的索引

PUT localhost:9200/my_index

创建映射

PUT localhost:9200/my_index/my_type/_mapping
{
    "properties": {
        "name": {
            "type": "text"
        }
    }
}

添加一个文档

PUT localhost:9200/my_index/my_type/2
{
    "name": "李锋镝"
}

查询:

GET localhost:9200/my_index/my_type/2
{
    "_index": "my_index",
    "_type": "my_type",
    "_id": "2",
    "_version": 1,
    "_seq_no": 0,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "name": "李锋镝"
    }
}

可以发现文档所属的索引是my_index。

设置别名prod_index指向my_index

PUT localhost:9200/my_index/_alias/prod_index

检测别名是否设置成功

使用

GET localhost:9200/*/_alias/prod_index

或者

GET localhost:9200/my_index/_alias/*

如果成功就会都返回如下的结果:

{
    "my_index": {
        "aliases": {
            "prod_index": {}
        }
    }
}

说明别名设置成功。

使用别名查询文档:

GET localhost:9200/prod_index/my_type/2
{
    "_index": "my_index",
    "_type": "my_type",
    "_id": "2",
    "_version": 1,
    "_seq_no": 0,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "name": "李锋镝"
    }
}

嗯...文档所属的索引同样是my_index。

然后可能过了一段时间我们发现my_index中的一个字段的映射不符合我们现今的需求,需要修改这个字段的映射。问题来了,我们不能修改现存的映射,所以我们必须重新索引数据。

重建索引

首先创建一个名为my_index1的索引

PUT localhost:9200/my_index1

创建映射

PUT localhost:9200/my_index1/my_type/_mapping
{
    "properties": {
        "name": {
            "type": "keyword"
        }
    }
}

将数据从my_index中索引到my_index1

POST localhost:9200/_reindex
{
  "source": {
    "index": "my_index"
  },
  "dest": {
    "index": "my_index1"
  }
}

操作成功,返回:

{
    "took": 1159,
    "timed_out": false,
    "total": 2,
    "updated": 0,
    "created": 2,
    "deleted": 0,
    "batches": 1,
    "version_conflicts": 0,
    "noops": 0,
    "retries": {
        "bulk": 0,
        "search": 0
    },
    "throttled_millis": 0,
    "requests_per_second": -1.0,
    "throttled_until_millis": 0,
    "failures": []
}

然后我们查询下数据,看下数据过来了没:

GET localhost:9200/my_index1/my_type/2
{
    "_index": "my_index1",
    "_type": "my_type",
    "_id": "2",
    "_version": 1,
    "_seq_no": 0,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "name": "李锋镝"
    }
}

可以看到通过新的索引已经可以查询到my_index中的数据了。

接下来就是将别名prod_index指到my_index1上,因为一个别名可以指向多个索引,所以我们还需要删除my_index的别名。这个操作需要原子化,所以我们使用_aliases API来操作。

转移索引别名

POST localhost:9200/_aliases
{
    "actions": [
        {
            "remove": {
                "index": "my_index",
                "alias": "prod_index"
            }
        },
        {
            "add": {
                "index": "my_index1",
                "alias": "prod_index"
            }
        }
    ]
}

然后我们使用别名来查询一下我们的数据:

GET localhost:9200/prod_index/my_type/2
{
    "_index": "my_index1",
    "_type": "my_type",
    "_id": "2",
    "_version": 1,
    "_seq_no": 0,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "name": "李锋镝"
    }
}

可以看到我们通过别名查询到的数据的索引已经是my_index1了。

OK,到了这里基本就大功告成,实现了在零停机的情况下从旧索引迁移到新索引了(前提是应用程序里边使用的是别名而不是索引名,这样你就可以在任何时候重建索引,而且别名的开销很小)。

其他

数据量太大,迁移时间很长怎么办?

_reindex API重建索引默认分页大小是1000,可以通过调整这个参数来增加效率。

POST localhost:9200/_reindex
{
  "source": {
    "index": "my_index",
    "size": 5000
  },
  "dest": {
    "index": "my_index1",
    "routing": "=cat"
  }
}

或者使用slices并行处理数据:

POST localhost:9200/_reindex?slices=5&refresh
{
  "source": {
    "index": "my_index"
  },
  "dest": {
    "index": "my_index1"
  }
}
除非注明,否则均为李锋镝的博客原创文章,转载必须以链接形式标明本文链接

本文链接:https://www.lifengdi.com/zhong-jian-jian/1607

推荐阅读

  • MySQL 同步 ElasticSearch 深度指南——6 种方案的原理、实战与避坑
  • Apollo配置中心中的protalDB的作用是什么
  • 什么是Meta Server?
  • 千万级大表新增字段实战指南:告别锁表与业务中断
  • 在 SQL 中做范围查询时,使用 BETWEEN AND 和直接用 >/>=/
本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可
标签: ElasticSearch 数据迁移
最后更新:2025年11月20日
相关文章
  • Elasticsearch常用查询2019年11月5日
  • Elasticsearch:cat API 介绍及其使用2020年10月27日
  • 终于有人把Elasticsearch原理讲透了!2019年8月15日
  • ElasticSearch添加mapping2019年6月14日
  • MySQL 同步 ElasticSearch 深度指南——6 种方案的原理、实战与避坑2025年10月30日

李锋镝

既然选择了远方,便只顾风雨兼程。

打赏 点赞
< 上一篇
下一篇 >
1234567891112131415161718192021222324252627282930313233343536373839404142434446474849505152535455575859606162636465666769727476777879808182858687909293949596979899
取消回复
…

文章评论

还没有评论,快来抢沙发吧~

试问岭南应不好,却道:此心安处是吾乡。

听点儿音乐吧 朋友~
文章目录
最新 热点 随机
最新 热点 随机
给主题增加了Now、每日心情、年度回顾、岁月同一天、随机漫步等功能 增加了两套复古皮肤-牛皮纸、千禧网页 Claude Design介绍与使用 Kratos+主题新功能介绍 Taste Skill 说明与使用 jasypt-spring-boot 使用说明
AI时代,个人技术博客的出路在哪里?给主题增加了Now、每日心情、年度回顾、岁月同一天、随机漫步等功能这个域名注册整整十年了,十年时间,真快啊WordPress实现用户评论等级排行榜插件WordPress网站换了个字体,差点儿把样式换崩了做了一个WordPress文章热力图插件
你的答案 【收藏】从面试官角度观察到的程序员技能瓶颈,同时给出突破瓶颈的建议 数据库更新如何实现乐观锁 跨平台版本管理神器,开发者的环境配置救星:vfox Ollama:重新定义本地大模型运行的开源革命 为什么 Spring 不建议使用 @Autowired?@Resource 才是王道
最近评论
李锋镝 发布于 2 天前(07月24日) 牛皮纸自带经典款老钱风buff~
满心 发布于 2 天前(07月24日) 牛皮纸还是好看
李锋镝 发布于 3 天前(07月23日) 都是AI的功劳~ :21:
林羽凡 发布于 3 天前(07月23日) 好久没来了,果然大佬就是大佬,太厉害了,博客的时间轴不错呀。
李锋镝 发布于 3 天前(07月23日) 布局确实有点儿像,哈哈哈
标签聚合
docker IDEA SQL AI编程 数据库 日常 ElasticSearch SpringBoot 多线程 MySQL Spring WordPress JAVA K8s Redis JVM AI 分布式 架构 设计模式
友情链接
  • 彬红茶日记
  • Mr.Sun的博客
  • 哥斯拉
  • 老张博客
  • 林羽凡
  • 风渡言
  • 临窗旋墨
  • Blogs·CN
  • 懋和道人
  • 韩情脉脉
  • 瓦匠个人小站
  • 旧时繁华
  • Honesty
  • 拾趣博客导航
  • 韩小韩博客
  • 志文工作室
  • 知向前端
  • 搬砖日记
  • 皮皮社

COPYRIGHT © 2026 lifengdi.com. ALL RIGHTS RESERVED.

域名年龄

Theme Kratos+ By Dylan Li

津ICP备2024022503号-3

京公网安备11011502039375号