【elasticsearch】reindex 操作将索引的数据复制到另一个索引

发布于:2025-02-10 ⋅ 阅读:(39) ⋅ 点赞:(0)

在Elasticsearch中,reindex 操作用于将一个索引的数据复制到另一个索引。常用的 reindex 命令有很多细节,下面是一些常见用法和命令详解:

基本命令

  1. 基础Reindex命令

    POST /_reindex
    {
      "source": {
        "index": "source_index"
      },
      "dest": {
        "index": "destination_index"
      }
    }
    
    • source: 需要复制数据的源索引。
    • dest: 数据需要复制到的目标索引。

    这个命令会将 source_index 中的所有文档复制到 destination_index 中。

配图示例:

+-------------------+     Reindex       +----------------------+
|                   |    ----------->   |                      |
|    source_index   |                   |   destination_index  |
|                   |                   |                      |
+-------------------+                   +----------------------+

使用查询过滤数据

  1. 基于查询的Reindex(只复制符合条件的文档)

    POST /_reindex
    {
      "source": {
        "index": "source_index",
        "query": {
          "range": {
            "date": {
              "gte": "2024-01-01",
              "lte": "2024-12-31"
            }
          }
        }
      },
      "dest": {
        "index": "destination_index"
      }
    }
    
    • query: 用于过滤哪些文档需要被复制。在此示例中,只有 date 字段在 2024 年的文档会被复制到目标索引。

配图示例:

+-------------------+    Query Filter    +----------------------+
|                   |     ----------->   |                      |
|    source_index   |  (range: date:2024)|   destination_index  |
|                   |                    |                      |
+-------------------+                    +----------------------+

重建索引时更新文档

  1. 重建时更新文档(在复制时修改字段)

    如果在重建时需要对文档做一些变更,可以使用 script 字段。

    POST /_reindex
    {
      "source": {
        "index": "source_index"
      },
      "dest": {
        "index": "destination_index"
      },
      "script": {
        "source": "ctx._source['field_name'] = 'new_value'"
      }
    }
    
    • script: 用于在重建时修改文档内容。在此示例中,字段 field_name 会被更新为 new_value

在复制文档时,还可以通过 script 修改文档中的字段。例如,修改 price 字段的值。

配图示例:

+-------------------+   Script Modify    +----------------------+
|                   |     ----------->   |                      |
|    source_index   |    (price * 1.1)   |   destination_index  |
|                   |                    |                      |
+-------------------+                    +----------------------+

限制批量操作数量

  1. 限制每批次处理的文档数量

    默认情况下,reindex 会一次性处理所有文档,可以通过设置 sizescroll 控制批量处理的数量。

    POST /_reindex
    {
      "source": {
        "index": "source_index",
        "size": 1000
      },
      "dest": {
        "index": "destination_index"
      }
    }
    
    • size: 每次从源索引中读取的文档数量(每次处理多少条数据)。

配图示例:

+-------------------+     Batch Size      +----------------------+
|                   |     ----------->    |                      |
|    source_index   |     (size: 1000)    |   destination_index  |
|                   |                     |                      |
+-------------------+                     +----------------------+

使用 wait_for_completion 控制命令同步/异步执行

  1. 同步或异步执行Reindex

    默认情况下,reindex 会异步执行。可以使用 wait_for_completion 控制它是否等待执行完成。

    POST /_reindex?wait_for_completion=true
    
    • wait_for_completion: 设置为 true 表示等待操作完成后才返回结果,设置为 false 时会异步执行,立即返回。

错误处理和重试

  1. 处理错误和重试

    reindex 命令还支持 on_failure 来处理失败的文档(如记录日志、重试等)。

    POST /_reindex
    {
      "source": {
        "index": "source_index"
      },
      "dest": {
        "index": "destination_index"
      },
      "on_failure": [
        {
          "op": "index",
          "index": "error_index",
          "doc_as_upsert": true
        }
      ]
    }
    
    • on_failure: 用于处理错误的文档,比如将失败的文档发送到另一个索引 error_index

网站公告

今日签到

点亮在社区的每一天
去签到