在我们开发的过程中,我们有很多时候需要用到Reindex接口。它可以帮我们把数据从一个index到另外一个index进行重新reindex。这个对于特别适用于我们在修改我们数据的mapping后,需要重新把数据从现有的index转到新的index建立新的索引,这是因为我们不能修改现有的index的mapping一旦已经定下来了。在接下来的介绍中,我们将学习如何使用reindex接口。

为了能够使用reindex接口,我们必须满足一下的条件:

  • _source选项对所有的源index文档是启动的,也即源index的source是被存储的
  • reindex不是帮我们尝试设置好目的地index。它不拷贝源index的设置到目的地的index里去。你应该在做reindex之前把目的地的源的index设置好,这其中包括mapping, shard数目,replica等

下面,我们来一个具体的例子,比如建立一个blogs的index。

    PUT twitter2/_doc/1
{
"user" : "双榆树-张三",
"message" : "今儿天气不错啊,出去转转去",
"uid" : 2,
"age" : 20,
"city" : "北京",
"province" : "北京",
"country" : "中国",
"address" : "中国北京市海淀区",
"location" : {
"lat" : "39.970718",
"lon" : "116.325747"
}
}

上面的命令让我们建立了一个叫做twitter2的index,并同时帮我们生产了一个如下的mapping:

GET /twitter2/_mapping

显示的结果是:

    {
"twitter2" : {
"mappings" : {
"properties" : {
"address" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"age" : {
"type" : "long"
},
"city" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"country" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"location" : {
"properties" : {
"lat" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"lon" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
},
"message" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"province" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"uid" : {
"type" : "long"
},
"user" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}

显然系统帮我们生产的location数据类型是不对的,我们必须进行修改。一种办法是删除现有的twitter2索引,让后修改它的mapping,再重新索引所有的数据。这对于一个两个文档还是可以的,但是如果已经有很多的数据了,这个方法并不可取。另外一种方式,是建立一个完全新的index,使用新的mapping进行reindex。下面我们展示如何使用这种方法。

创建一个新的twitter3的index,使用如下的mapping:

    PUT twitter3
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 1
},
"mappings": {
"properties": {
"address": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"age": {
"type": "long"
},
"city": {
"type": "text"
},
"country": {
"type": "text"
},
"location": {
"type": "geo_point"
},
"message": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"province": {
"type": "text"
},
"uid": {
"type": "long"
},
"user": {
"type": "text"
}
}
}
}

这里我们我们修改了location及其它的一些数据项的数据类型。运行上面的指令,我们就可以创建一个完全新的twitter3的index。我们可以通过如下的命令来进行reindex:

    POST _reindex
{
"source": {
"index": "twitter2"
},
"dest": {
"index": "twitter3"
}
}

显示的结果是:

    {
"took" : 52,
"timed_out" : false,
"total" : 1,
"updated" : 0,
"created" : 1,
"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" : [ ]
}

我们可以通过如下的命令来检查我们的twitter3是否已经有新的数据:

GET /twitter3/_search

显示的结果是:

    {
"took" : 100,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "twitter3",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"user" : "双榆树-张三",
"message" : "今儿天气不错啊,出去转转去",
"uid" : 2,
"age" : 20,
"city" : "北京",
"province" : "北京",
"country" : "中国",
"address" : "中国北京市海淀区",
"location" : {
"lat" : "39.970718",
"lon" : "116.325747"
}
}
}
]
}
}

显然我们的数据已经从twitter2到twitter3,并且它的数据类型已经是完全符合我们要求的数据类型。

Reindex执行

  • Reindex是一个时间点的副本
  • 就像上面返回的结果显示的那样,它是以batch(批量)的方式来执行的。默认的批量大小为1000
  • 你也可以只拷贝源index其中的一部分数据
    • 通过加入query到source中
    • 通过定义max_docs参数

比如:

    POST _reindex
{
"max_docs": 100,
"source": {
"index": "twitter2",
"query": {
"match": {
"city": "北京"
}
}
},
"dest": {
"index": "twitter3"
}
}

这里,我们定义最多不超过100个文档,同时,我们只拷贝来自“北京”的twitter记录。

设置op_type to create将导致_reindex仅在目标索引中创建缺少的文档。 所有现有文档都会导致版本冲突,比如:

    POST _reindex
{
"source": {
"index": "twitter2"
},
"dest": {
"index": "twitter3",
"op_type": "create"
}
}

如果我们之前已经做过reindex,那么我们可以看到如下的结果:

    {
"took": 2,
"timed_out": false,
"total": 1,
"updated": 0,
"created": 0,
"deleted": 0,
"batches": 1,
"version_conflicts": 1,
"noops": 0,
"retries": {
"bulk": 0,
"search": 0
},
"throttled_millis": 0,
"requests_per_second": -1,
"throttled_until_millis": 0,
"failures": [
{
"index": "twitter3",
"type": "_doc",
"id": "1",
"cause": {
"type": "version_conflict_engine_exception",
"reason": "[1]: version conflict, document already exists (current version [5])",
"index_uuid": "ffz2LNIIQqqDx211R5f4fQ",
"shard": "0",
"index": "twitter3"
},
"status": 409
}
]
}

它表明我们之前的文档id为1的有版本上的冲突。

默认情况下,版本冲突会中止_reindex进程。 “conflict”请求body参数可用于指示_reindex继续处理版本冲突的下一个文档。 请务必注意,其他错误类型的处理不受“conflict”参数的影响。 当“conflict”:在请求正文中设置“proceed”时,_reindex进程将继续发生版本冲突并返回遇到的版本冲突计数:

    POST _reindex
{
"conflicts": "proceed",
"source": {
"index": "twitter"
},
"dest": {
"index": "new_twitter",
"op_type": "create"
}
}

Throttling

重新索引大量文档可能会使您的群集泛滥甚至崩溃。requests_per_second限制索引操作速率。

    POST _reindex?requests_per_second=500
{
"source": {
"index": "blogs",
"size": 500
},
"dest": {
"index": "blogs_fixed"
}
}

运用index别名来进行reindex

我们可以通过如下的方法来实现从一个index到另外一个index的数据转移:

    PUT test
PUT test_2
POST /_aliases
{
"actions" : [
{ "add": { "index": "test_2", "alias": "test" } },
{ "remove_index": { "index": "test" } }
]
}

在上面的例子中,假如我们地添加了一个叫做test的index,而test_2是我们想要的。我们直接可以通过上面的方法吧test中的数据交换到test_2中,并同时把test索引删除。

从远处进行reindex

_reindex也支持从一个远处的Elasticsearch的服务器进行reindex,它的语法为:

    POST _reindex
{
"source": {
"index": "blogs",
"remote": {
"host": "http://remote_cluster_node1:9200",
"username": "USERNAME",
"password": "PASSWORD"
}
},
"dest": {
"index": "blogs"
}
}

这里显示它从一个在http://remote_cluster_node1:9200的服务器来拷贝文件从一个index到另外一个index。

参考:

【1】https://www.elastic.co/guide/en/elasticsearch/reference/7.3/docs-reindex.html

Elasticsearch: Reindex接口的更多相关文章

  1. Elasticsearch Reindex性能提升10倍+实战

    文章转载自: https://mp.weixin.qq.com/s?__biz=MzI2NDY1MTA3OQ==&mid=2247484134&idx=1&sn=750249a ...

  2. ElasticSearch reindex报错:the final mapping would have more than 1 type

    ElasticSearch reindex报错:the final mapping would have more than 1 type 学习了:https://blog.csdn.net/qq_2 ...

  3. Springboot整合elasticsearch以及接口开发

    Springboot整合elasticsearch以及接口开发 搭建elasticsearch集群 搭建过程略(我这里用的是elasticsearch5.5.2版本) 写入测试数据 新建索引book( ...

  4. post 中文数据到elasticsearch restful接口报json_parse_exception 问题

    我们的客户端程序直接调用es 的restful接口, 通过post json数据去查询, 但post数据有中文的时候,有些中文会报异常,有些中文不会 {"error":{" ...

  5. Elasticsearch:使用_update_by_query更新文档

    转载自: https://blog.csdn.net/UbuntuTouch/article/details/105564270 在很多的情况下,我们我们想更新我们所有的文档: 添加一个新的field ...

  6. Elasticsearch学习总结 (Centos7下Elasticsearch集群部署记录)

    一.  ElasticSearch简单介绍 ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.Elasticse ...

  7. Elasticsearch 基础理论 & 配置调优

    一.简介 ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.Elasticsearch是用Java开发的,并作为 ...

  8. ElasticSearch 5.0 简介

    参考:http://blog.csdn.net/wzhg0508/article/details/52063676 Elasticsearch 5.0 简介(medcl微信直播实录) 大家好,非常高兴 ...

  9. Elasticsearch之elasticsearch5.x 新特性

    其实,elasticsearch5.x 和 elasticsearch2.x 并不区别很大. 是因为,ELK里之前版本各种很混乱,直接升级到5.0了. 其实,elasticsearch5.x 按理来说 ...

随机推荐

  1. 集合-Collection工具类

    一.概念 二.常用方法 1.Collection和Collections的区别 Collection:是创建集合的接口,Collections是一个操作Collection工具类 2.常用方法 点击查 ...

  2. Git Rebase操作

    概括 rebase翻译过来为"变基",可以理解为改变基础,它可以用于分支合并和修改提交记录. 合并分支的区别 我们知道merge操作也可以用于分支合并,但是其和rebase操作有着 ...

  3. Windows 远程连接后,自动断开,所有程序都自动关闭(待验证,待更新)

    win+r输入regedit打开注册表编辑SecurityLayer,将值改为2 计算机\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Ter ...

  4. ansible概述、安装、模块介绍

    一.Ansible介绍 Ansible是一 个基于Python开发的配置管理和应用部署工具,现在也在自动化管理领域大放异彩. 它融合了众多老牌运维工具的优点,Pubbet和Saltstack能实现的功 ...

  5. 第46届ICPC澳门站 K - Link-Cut Tree // 贪心 + 并查集 + DFS

    原题链接:K-Link-Cut Tree_第46屆ICPC 東亞洲區域賽(澳門)(正式賽) (nowcoder.com) 题意: 要求一个边权值总和最小的环,并从小到大输出边权值(2的次幂):若不存在 ...

  6. angular好文

    Angular常见问题:subscribe()还是 async 管道 ? 终极答案就在这里 Angular Development #10 – RouteReuseStrategy – Maintai ...

  7. ZJOI2016 小星星 题解

    我一生之敌是状压 本文发表于 洛谷博客:https://www.luogu.com.cn/blog/LoveMC/solution-p3349 Cnblogs:https://www.cnblogs. ...

  8. 【原创】Python 网易易盾滑块验证

    本文仅供学习交流使用,如侵立删! 记一次 网易易盾滑块验证分析并通过 操作环境 win10 . mac Python3.9 selenium.PIL.numpy.scipy.matplotlib 分析 ...

  9. k8s暴露集群内和集群外服务的方法

    集群内服务 一般 pod 都是根据 service 资源来进行集群内的暴露,因为 k8s 在 pod 启动前就已经给调度节点上的 pod 分配好 ip 地址了,因此我们并不能提前知道提供服务的 pod ...

  10. 【Harmony OS】【ArkUI】ets开发 简易视频播放器

    ​前言:这一次我们来使用ets的Swiper组件.List组件和Video组件制作一个简易的视频播放器.本篇是以HarmonyOS官网的codelab简易视频播放器(eTS)为基础进行编写.本篇最主要 ...