ES reindex 接口说明和脚本应用

1. 接口和参数

_reindex 接口

POST _reindex
{
  "source": {
    "index": "my-index-000001",
    "slice": {
      "id": 0, # The id of the slice
      "max": 2 # The maximum number of slices
    }, # 可使用 auto 作为 slice 的值
    "query": {
      "match": {
        "company": "cat"
      }
    },
    "size": 100
  },
  "dest": {
    "index": "my-new-index-000001",
    "routing": "=cat"
  }
}

请求参数

请求参数位于 POST _reindex? 之后


查询参数

查询参数位于请求体中


2. 在 reindex 期间修改文档

_update_by_query API类似,_reindex API 支持使用 script 修改文档。与 _update_by_query API不同的是,script 允许修改文档的元数据。例:

POST _reindex
{
  "source": {
    "index": "my-index-000001"
  },
  "dest": {
    "index": "my-new-index-000001",
    "version_type": "external"
  },
  "script": {
    "source": "if (ctx._source.foo == 'bar') {ctx._version++; ctx._source.remove('foo')}",
    "lang": "painless"
  }
}

设置 ctx.op 可以修改在 destination 执行的操作

  • noop: 设置 ctx.op = "noop",文档将不必 index 到 destination。该操作会在 response body 中被计入 noop 计数器
  • delete: 设置 ctx.op = "delete",文档将从 destination 删除。该操作会在 resopnse body 中被计入 delete 计数器
    设置 ctx.op 为其他值将会返回错误,设置 ctx 的其他字段也一样
    还可以修改的字段有:
  • _id
  • _index
  • _version
  • _routing
    设置 _versionnull 或从 ctx map 清除该字段,如同在 index 请求中不发送 version。该操作会导致目标端文档会被覆盖,并无视目标的版本或在 _reindex 请求中使用的 version 类型
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容