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

问题描述 投票:0回答:2

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

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

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

elasticsearch kibana elasticsearch-painless
2个回答
2
投票

简单的 painless 和弹性搜索的 reindex 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}"
  }
}

0
投票

让我们补上最新的查询

POST _reindex?wait_for_completion=false // async call - you can track query progress if your index is huge - do GET _tasks/{your taskId}
{
  "source": {
    "index": *source index name*
  },
  "dest": {
    "index": *destination index name*
  },
  "script": {
    "source": """
  ctx._routing = ctx._source.field1+'-'+ctx._source.field2;
  """
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.