繁体   English   中英

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

[英]ElasticSearch painless script for reindexing

我们正在尝试使用以下简单脚本来在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'"
  }
}

不同于以下网址: https ://www.elastic.co/guide/zh-CN/elasticsearch/reference/current/docs-reindex.html#_reindex_daily_indices

该脚本可以完美运行,并创建我们所有索引的另一个副本。 例如:如果运行此脚本后我的原始索引为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,即原始索引。

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

提前致谢 !

我这样做的方法是像从Elasticsearch参考中的示例中那样重新索引,但是我没有在索引前附加“ temp-”,而是附加了“ -1”:

POST _reindex
{
  "source": {
    "index": "metricbeat-*"
  },
  "dest": {
    "index": "metricbeat"
  },
  "script": {
    "lang": "painless",
    "source": "ctx._index = 'temp-' + ctx._index"
  }
}

这样可以更轻松地删除模式为“ metricbeat- *”的原始索引:

DELETE metricbeat-*

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

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

附带说明一下,Elasticsearch参考中的示例不必要地复杂:

ctx._index = 'metricbeat-' + (ctx._index.substring('metricbeat-'.length(), ctx._index.length())) + '-1'

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

ctx._index = ctx._index + '-1'

您不能重命名索引。 但是,在删除原始索引之后,可以使用别名

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM