用于重新索引的 ElasticSearch 无痛脚本

SSG*_*SSG 4 elasticsearch

我们正在尝试使用以下无痛脚本来重新索引 Elasticsearch 中的数据。

POST _reindex
{
  "source": {
    "index": "metricbeat-*"
  },
  "dest": {
    "index": "metricbeat"
  },
  "script": {
    "lang": "painless",
    "inline": "ctx._index = 'metricbeat-' + (ctx._index.substring('metricbeat-'.length(), ctx._index.length())) + '-1'"
  }
}
Run Code Online (Sandbox Code Playgroud)

参考以下网址: https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-reindex.html#_reindex_daily_indices

该脚本运行完美,并创建了我们所有索引的另一个副本。exa:如果我在运行此脚本后将原始索引设置为 metricbeat-2016.05.30,它将创建 metricbeat-2016.05.30-1,它是原始索引的精确副本,即 (metricbeat-2016.05.30)

现在我想做以下两件事:

1] 删除原始索引,即 metricbeat-2016.05.30
2] 将重新索引的索引或原始索引的副本(即(metricbeat-2016.05.30-1))重命名为 metricbeat-2016.05.30,即原始索引。

我们应该怎么做 ?我们可以修改上面的无痛脚本吗?

提前致谢 !

Bjo*_*orn 6

我这样做的方法是像 Elasticsearch 参考中的示例一样重新索引,但不是附加“-1”,而是在索引前面加上“temp-”:

POST _reindex
{
  "source": {
    "index": "metricbeat-*"
  },
  "dest": {
    "index": "metricbeat"
  },
  "script": {
    "lang": "painless",
    "source": "ctx._index = 'temp-' + ctx._index"
  }
}
Run Code Online (Sandbox Code Playgroud)

这使得删除具有“metricbeat-*”模式的原始索引变得更容易:

DELETE metricbeat-*
Run Code Online (Sandbox Code Playgroud)

然后我再次重新索引以获得原始名称:

POST _reindex
{
  "source": {
    "index": "temp-metricbeat-*"
  },
  "dest": {
    "index": "metricbeat"
  },
  "script": {
    "lang": "painless",
    "source": "ctx._index = ctx._index.substring(5)"
  }
}
Run Code Online (Sandbox Code Playgroud)

附带说明一下,Elasticsearch 参考中的示例过于复杂:

ctx._index = 'metricbeat-' + (ctx._index.substring('metricbeat-'.length(), ctx._index.length())) + '-1'
Run Code Online (Sandbox Code Playgroud)

使用以下代码可以获得相同的结果:

ctx._index = ctx._index + '-1'
Run Code Online (Sandbox Code Playgroud)