在我们开发的过程中,我们有很多时候需要用到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. GET 请求和 POST 请求的区别和使用

    作为前端开发, HTTP 中的 POST 请求和 GET 请求是经常会用到的东西,有的人可能知道,但对其原理和如何使用并不特别清楚,那么今天来浅谈一下两者的区别与如何使用. GET请求和POST请求的 ...

  2. 千万不要把Request传递到异步线程里面!有坑!

    你好哇,我是歪歪. 前几天在网上冲浪的时候看到一篇技术文章,讲的是他把一个 request 请求传递到了线程池里面,然后遇到了一个匪夷所思的情况. 他写了这篇文章,把自己针对这个问题的探索过程分享了出 ...

  3. 【学习笔记】带你从0开始学习 01Trie

    01Trie Section 1:普通 Trie Section 1.1 什么是 Trie Trie 树,即字典树,是一种树形结构.典型应用是用于统计和排序大量的字符串前缀来减少查询时间,最大限度地减 ...

  4. shell中各种括号的用法

    一.单小括号()1.将某个命令的返回值作为某个变量的值进行传递 #!/bin/bash USER=$(whoami) echo $USER [root@jump ~]# for i in $(seq ...

  5. Mpvue1.0+Python3.7+Django2.0.4实现微信小程序的支付功能

    原文转载自「刘悦的技术博客」https://v3u.cn/a_id_112 其实微信支付有很多种形式,刷脸,扫码,APP支付,小程序支付等,这边只说明小程序支付的实现,不过原理上都大同小异. 首先,需 ...

  6. odoo14 编辑状态和非编辑状态下隐藏

    1 <div class="oe_edit_only"> 2 <a name="remove_group_id" type="obj ...

  7. pytest-fixture执行顺序

    作用域-scope 作用域越大,越先执行,session>package>module>class>function. 是否自动调用fixture 自动调用(autouse=T ...

  8. git submodule 实战

    1.git submodule指什么 关于git submodule是什么,可以看下面这个链接. https://www.cnblogs.com/hwx0000/p/14146838.html 2.g ...

  9. 一个非常简单用.NET操作RabbitMQ的方法

    RabbitMQ作为一款主流的消息队列工具早已广受欢迎.相比于其它的MQ工具,RabbitMQ支持的语言更多.功能更完善. 本文提供一种市面上最/极简单的使用RabbitMQ的方式(支持.NET/.N ...

  10. java-分支结构(四种基本分支结构的认识)

    分支结构:有条件的执行某语句,并非每句必走 1)if结构:1条路 2)if...else结构:2条路 3)if...else if结构:多条路 4)switch...case结构:多条路 优点:效率高 ...