用于重新索引的ElasticSearch无痛脚本 [英] ElasticSearch painless script for reindexing
问题描述
我们正尝试使用下面的简单脚本在Elasticsearch中重新索引我们的数据。
We are trying to use following painless script to reindex our data in 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'"
}
}
此脚本可以完美运行并创建我们所有索引的另一个副本。
exa:如果我的原始索引为
metricbeat-2016.05.30
运行此脚本后,它将创建metricbeat-2016.05.30-1,它是原始索引的精确副本,即(metricbeat-2016.05 .30)
This script works perfect and creates another copy of our all indices. exa: if I have origin index as metricbeat-2016.05.30 after running this script it creates metricbeat-2016.05.30-1 which is exact copy of original index i.e (metricbeat-2016.05.30)
现在我想做以下两件事:
Now I want to do following 2 things:
1]删除原始索引,即metricbeat- 2016.05.30
2]重命名重新索引的索引或原始索引的副本,即(metricbeat-2016.05.30-1)回到metricbeat-2016.05.30,即原始索引。
1] Delete original index i.e metricbeat-2016.05.30
2] Rename reindexed index or copy of original index i.e (metricbeat-2016.05.30-1) back to metricbeat-2016.05.30 i.e original index.
我们该怎么做?
我们可以修改上面的脚本吗?
How can we do this ? can we modify above painless script ?
预先感谢!
推荐答案
我这样做的方法是像从Elasticsearch引用中的示例中那样重新索引,但是没有附加 -1,而是在索引前加上了 temp-:
The way I did it was to reindex like in the example from Elasticsearch reference, but instead of appending a "-1" I prepended the index with "temp-":
POST _reindex
{
"source": {
"index": "metricbeat-*"
},
"dest": {
"index": "metricbeat"
},
"script": {
"lang": "painless",
"source": "ctx._index = 'temp-' + ctx._index"
}
}
这样可以更轻松地删除带有 metricbeat- *模式的原始索引:
This makes it easier to delete the original indices with the pattern "metricbeat-*":
DELETE metricbeat-*
然后我再次重新索引以获取原始名称:
I then reindexed again to get the original name:
POST _reindex
{
"source": {
"index": "temp-metricbeat-*"
},
"dest": {
"index": "metricbeat"
},
"script": {
"lang": "painless",
"source": "ctx._index = ctx._index.substring(5)"
}
}
作为附带说明,Elasticsearch参考中的示例不必要地复杂:
As a side note, the example in the Elasticsearch reference is unnecessarily complex:
ctx._index = 'metricbeat-' + (ctx._index.substring('metricbeat-'.length(), ctx._index.length())) + '-1'
您得到的结果与代码相同:
You get the same result with the code:
ctx._index = ctx._index + '-1'
这篇关于用于重新索引的ElasticSearch无痛脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!