在我们开发的过程中,我们有很多时候需要用到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。

  1. PUT twitter2/_doc/1
  2. {
  3. "user" : "双榆树-张三",
  4. "message" : "今儿天气不错啊,出去转转去",
  5. "uid" : 2,
  6. "age" : 20,
  7. "city" : "北京",
  8. "province" : "北京",
  9. "country" : "中国",
  10. "address" : "中国北京市海淀区",
  11. "location" : {
  12. "lat" : "39.970718",
  13. "lon" : "116.325747"
  14. }
  15. }

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

  1. GET /twitter2/_mapping

显示的结果是:

  1. {
  2. "twitter2" : {
  3. "mappings" : {
  4. "properties" : {
  5. "address" : {
  6. "type" : "text",
  7. "fields" : {
  8. "keyword" : {
  9. "type" : "keyword",
  10. "ignore_above" : 256
  11. }
  12. }
  13. },
  14. "age" : {
  15. "type" : "long"
  16. },
  17. "city" : {
  18. "type" : "text",
  19. "fields" : {
  20. "keyword" : {
  21. "type" : "keyword",
  22. "ignore_above" : 256
  23. }
  24. }
  25. },
  26. "country" : {
  27. "type" : "text",
  28. "fields" : {
  29. "keyword" : {
  30. "type" : "keyword",
  31. "ignore_above" : 256
  32. }
  33. }
  34. },
  35. "location" : {
  36. "properties" : {
  37. "lat" : {
  38. "type" : "text",
  39. "fields" : {
  40. "keyword" : {
  41. "type" : "keyword",
  42. "ignore_above" : 256
  43. }
  44. }
  45. },
  46. "lon" : {
  47. "type" : "text",
  48. "fields" : {
  49. "keyword" : {
  50. "type" : "keyword",
  51. "ignore_above" : 256
  52. }
  53. }
  54. }
  55. }
  56. },
  57. "message" : {
  58. "type" : "text",
  59. "fields" : {
  60. "keyword" : {
  61. "type" : "keyword",
  62. "ignore_above" : 256
  63. }
  64. }
  65. },
  66. "province" : {
  67. "type" : "text",
  68. "fields" : {
  69. "keyword" : {
  70. "type" : "keyword",
  71. "ignore_above" : 256
  72. }
  73. }
  74. },
  75. "uid" : {
  76. "type" : "long"
  77. },
  78. "user" : {
  79. "type" : "text",
  80. "fields" : {
  81. "keyword" : {
  82. "type" : "keyword",
  83. "ignore_above" : 256
  84. }
  85. }
  86. }
  87. }
  88. }
  89. }
  90. }

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

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

  1. PUT twitter3
  2. {
  3. "settings": {
  4. "number_of_shards": 1,
  5. "number_of_replicas": 1
  6. },
  7. "mappings": {
  8. "properties": {
  9. "address": {
  10. "type": "text",
  11. "fields": {
  12. "keyword": {
  13. "type": "keyword",
  14. "ignore_above": 256
  15. }
  16. }
  17. },
  18. "age": {
  19. "type": "long"
  20. },
  21. "city": {
  22. "type": "text"
  23. },
  24. "country": {
  25. "type": "text"
  26. },
  27. "location": {
  28. "type": "geo_point"
  29. },
  30. "message": {
  31. "type": "text",
  32. "fields": {
  33. "keyword": {
  34. "type": "keyword",
  35. "ignore_above": 256
  36. }
  37. }
  38. },
  39. "province": {
  40. "type": "text"
  41. },
  42. "uid": {
  43. "type": "long"
  44. },
  45. "user": {
  46. "type": "text"
  47. }
  48. }
  49. }
  50. }

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

  1. POST _reindex
  2. {
  3. "source": {
  4. "index": "twitter2"
  5. },
  6. "dest": {
  7. "index": "twitter3"
  8. }
  9. }

显示的结果是:

  1. {
  2. "took" : 52,
  3. "timed_out" : false,
  4. "total" : 1,
  5. "updated" : 0,
  6. "created" : 1,
  7. "deleted" : 0,
  8. "batches" : 1,
  9. "version_conflicts" : 0,
  10. "noops" : 0,
  11. "retries" : {
  12. "bulk" : 0,
  13. "search" : 0
  14. },
  15. "throttled_millis" : 0,
  16. "requests_per_second" : -1.0,
  17. "throttled_until_millis" : 0,
  18. "failures" : [ ]
  19. }

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

  1. GET /twitter3/_search

显示的结果是:

  1. {
  2. "took" : 100,
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 1,
  6. "successful" : 1,
  7. "skipped" : 0,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : {
  12. "value" : 1,
  13. "relation" : "eq"
  14. },
  15. "max_score" : 1.0,
  16. "hits" : [
  17. {
  18. "_index" : "twitter3",
  19. "_type" : "_doc",
  20. "_id" : "1",
  21. "_score" : 1.0,
  22. "_source" : {
  23. "user" : "双榆树-张三",
  24. "message" : "今儿天气不错啊,出去转转去",
  25. "uid" : 2,
  26. "age" : 20,
  27. "city" : "北京",
  28. "province" : "北京",
  29. "country" : "中国",
  30. "address" : "中国北京市海淀区",
  31. "location" : {
  32. "lat" : "39.970718",
  33. "lon" : "116.325747"
  34. }
  35. }
  36. }
  37. ]
  38. }
  39. }

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

Reindex执行

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

比如:

  1. POST _reindex
  2. {
  3. "max_docs": 100,
  4. "source": {
  5. "index": "twitter2",
  6. "query": {
  7. "match": {
  8. "city": "北京"
  9. }
  10. }
  11. },
  12. "dest": {
  13. "index": "twitter3"
  14. }
  15. }

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

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

  1. POST _reindex
  2. {
  3. "source": {
  4. "index": "twitter2"
  5. },
  6. "dest": {
  7. "index": "twitter3",
  8. "op_type": "create"
  9. }
  10. }

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

  1. {
  2. "took": 2,
  3. "timed_out": false,
  4. "total": 1,
  5. "updated": 0,
  6. "created": 0,
  7. "deleted": 0,
  8. "batches": 1,
  9. "version_conflicts": 1,
  10. "noops": 0,
  11. "retries": {
  12. "bulk": 0,
  13. "search": 0
  14. },
  15. "throttled_millis": 0,
  16. "requests_per_second": -1,
  17. "throttled_until_millis": 0,
  18. "failures": [
  19. {
  20. "index": "twitter3",
  21. "type": "_doc",
  22. "id": "1",
  23. "cause": {
  24. "type": "version_conflict_engine_exception",
  25. "reason": "[1]: version conflict, document already exists (current version [5])",
  26. "index_uuid": "ffz2LNIIQqqDx211R5f4fQ",
  27. "shard": "0",
  28. "index": "twitter3"
  29. },
  30. "status": 409
  31. }
  32. ]
  33. }

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

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

  1. POST _reindex
  2. {
  3. "conflicts": "proceed",
  4. "source": {
  5. "index": "twitter"
  6. },
  7. "dest": {
  8. "index": "new_twitter",
  9. "op_type": "create"
  10. }
  11. }

Throttling

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

  1. POST _reindex?requests_per_second=500
  2. {
  3. "source": {
  4. "index": "blogs",
  5. "size": 500
  6. },
  7. "dest": {
  8. "index": "blogs_fixed"
  9. }
  10. }

运用index别名来进行reindex

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

  1. PUT test
  2. PUT test_2
  3. POST /_aliases
  4. {
  5. "actions" : [
  6. { "add": { "index": "test_2", "alias": "test" } },
  7. { "remove_index": { "index": "test" } }
  8. ]
  9. }

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

从远处进行reindex

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

  1. POST _reindex
  2. {
  3. "source": {
  4. "index": "blogs",
  5. "remote": {
  6. "host": "http://remote_cluster_node1:9200",
  7. "username": "USERNAME",
  8. "password": "PASSWORD"
  9. }
  10. },
  11. "dest": {
  12. "index": "blogs"
  13. }
  14. }

这里显示它从一个在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. Linux操作系统(4):磁盘分区、挂载

    Outline: ① lsblk :查看所有设备挂载情况 ② df -h :查询系统整体磁盘使用情况 ③ du -h /目录 :查询指定目录的磁盘占用情况 ④ mount :查询系统中已经挂载的设备 ...

  2. Unique -「企划」新生守则(?

    随想随记,主要是整活. 红色贝雷帽大爷会在校园不定期游走,遇见记得打招呼. 面食堂冰沙类饮品请快速解决或者边喝边搅,如果发现饮品甜度骤减请快速前往最近的垃圾桶扔掉. 关于散养猫小黄和小黑. 如果看见小 ...

  3. Solution -「Luogu 4135」作诗

    写在前面 & 前置芝士   好像是好久没有打理 blog 了.感觉上学期是有点颓.嘶,初三了好好冲一次吧.   那么回到这道题目.你会分块就能看懂. 题目大意   先挂个来自洛谷的 link. ...

  4. Hadoop学习 Hadoop-HA 解释和概念介绍

    一.Hadoop-HA 1.1 Hadoop1.x带来的问题 1.单点故障 a. 每个群集只有一个NameNode,NameNode存在单点故障(SPOF). ​ b. 如果该计算机或进程不可用,则整 ...

  5. Linux学习系列--如何在Linux中进行文件的管理

    文件 在常见的Linux的文件系统中,经常使用能了解到的文件管理系统是分为多个文件夹进行管理的. 如何查看文件路径 pwd ,在文件目录中,会有一个点(.)代表的是当前目录,两个点(..)代表的是当前 ...

  6. LyScript 获取上一条与下一条指令

    LyScript 插件默认并没有提供上一条与下一条汇编指令的获取功能,当然你可以使用LyScriptTools工具包直接调用内置命令得到,不过这种方式显然在效率上并不理想,我们需要在LyScript插 ...

  7. YII behaviors使用

    文件 frontend/libs/FilterTest.php <?php /** * Created by PhpStorm. * Date: 2016/5/27 * Time: 14:16 ...

  8. Linux—搭建Apache(httpd)服务

    1.httpd简介? http是Apache超文本传输协议服务器的主程序.它是一个独立的后台进程,能够处理请求的子进程和线程. http常用用的两个版本是httpd-2.2和httpd-2.4 Cen ...

  9. 从零开始Blazor Server(7)--使用Furion权限验证

    序 上面两篇我们讲了怎么用OnNavigateAsync来验证权限,又写了怎么用策略来验证权限. 其实我们既然集成了Fution,就可以用Furion带的方式来验证. 创建AdminHandler 我 ...

  10. 【web自动化测试】Playwright快速入门,5分钟上手

    我喜欢Playwright! 这是微软开源的一款非常强大的自动化工具,再过几年,他很有可能取代Selenium在浏览器自动化的通知地位.使用过一段时间,我没有找到很好的中文资料可以参考,导致很多问题无 ...