如何使用Elasticsearch _reindex API

容器与中间件中间件技术服务知识库
前言

Elasticsearch 中的 _reindx API 是在运维和开发过程中非常常见的接口,它可以帮我们将数据从一个index搬运到新的 index。例如,由于 ES 不支持动态修改mapping,如果我们期望修改一个 index 的 mapping时,可以选择 功能强大的 _reindex。

_reindex API 使用条件
  1. _source 选项对所有的源 index 文档是开启的。
  2. 在使用reindex API 之前,我们应该提前设置好目标 index 的 mapping。
_reindex API 的特性

_reindex API 有如下特性:

  • 它是以批量的方式来执行的。默认的批量大小为1000。
  • 进行数据拷贝时,拷贝的数据是一个时间点的副本。
  • _reindex API 支持设置条件,只拷贝源 index 中一部分数据。
如何使用

写入一条测试数据

PUT books/_doc/1
{
  "title":"Mastering ElasticSearch 5.0",
  "description":"Master the searching, indexing, and aggregation features in ElasticSearch Improve users’ search experience with Elasticsearch’s functionalities and develop your own Elasticsearch plugins",
  "author":"Bharvi Dixit",
  "public_date":"2017",
  "cover_url":"https://images-na.ssl-images-amazon.com/images/I/51OeaMFxcML.jpg"
}

查看 这个索引的 mapping:

{
  "books" : {
    "mappings" : {
      "properties" : {
        "author" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "cover_url" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "description" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "public_date" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "title" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }
}

从上面可以看出了,ES 自动生成的 mapping 并不是最佳的,我们需要进行优化下,设置新的 mapping:

PUT books1
{
      "mappings" : {
      "properties" : {
        "author" : {"type" : "keyword"},
        "cover_url" : {"type" : "keyword","index": false},
        "description" : {"type" : "text"},
        "public_date" : {"type" : "date"},
        "title" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 100
            }
          }
        }
      }
    }
}

接下来我们使用 _reindex API 将数据 拷贝到新的 index 中

POST  _reindex?slices=5&refresh
{
  "source": {
    "index": "books"
  },
  "dest": {
    "index": "books1"
  }
}

查看 books1 中的数据:这时候我们发现数据已经同步。


GET books1/_doc/1

输出如下:

{
  "_index" : "books1",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "_seq_no" : 0,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "title" : "Mastering ElasticSearch 5.0",
    "description" : "Master the searching, indexing, and aggregation features in ElasticSearch Improve users’ search experience with Elasticsearch’s functionalities and develop your own Elasticsearch plugins",
    "author" : "Bharvi Dixit",
    "public_date" : "2017",
    "cover_url" : "https://images-na.ssl-images-amazon.com/images/I/51OeaMFxcML.jpg"
  }
}

其他功能

1. 拷贝部分数据

上面我们提到,_reindex API 可以拷贝部分数据,可以有如下实现方式:

  • 加入 query 查询
  • 使用 max_docs 参数

示例如下:只拷贝筛选条件为 city = beijing 并且最多拷贝 100 条

POST _reindex
{
  "max_docs": 100,
  "source": {
    "index": "bytedance",
    "query": {
      "match": {
        "city": "Beijing"
      }
    }
  },
  "dest": {
    "index": "bytedance-china"
  }
}

2. _reindex Throttling

在数据量大的情况下,重新索引海量数据可能导致集群失去响应甚至崩溃,因此我们可以使用 requests_per_second 限制索引操作速率,例如:

POST _reindex?requests_per_second=200
{
  "source": {
    "index": "docs",
    "size": 500
  },
  "dest": {
    "index": "new_docs"
  }
}

更多功能,您可以参考文末的官方文档。

参考文档

[1] https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-reindex.html 如果您有其他问题,欢迎您联系火山引擎技术支持服务

46
0
0
0
相关产品
评论
未登录
看完啦,登录分享一下感受吧~
暂无评论