通过将路由键更改为两个字段值的组合,将弹性搜索文档重新索引到另一个索引中

Reindexing elastic-search documents into another index by changing the routing key to a combination of two field values

我现有的弹性搜索索引具有以下文档结构,没有 routing_key

{
   "_id",
   "feild1"
   "field2"
}

我需要将数据迁移到新索引中。索引的结构保持不变,但增加了 routing_key。路由键需要更新为“field1_field2”。是否有一个简单的 Kibana 脚本将数据迁移到新索引?

可以使用简单的 painless 和弹性搜索的重新索引 API 的组合来实现这一点。

POST _reindex
{
  "source": {
    "index": "{old_index_name}",
    "size": {batch_size}
  },
  "dest": {
    "index": "{new_index_name}"
  },
  "script": {
    "lang": "painless",
    "inline": "if (ctx._source.participants.length > 0) {ctx._routing=ctx._source.field1+'-'+ctx._source.field2}"
  }
}