【问题标题】:ElasticSearch painless script for reindexing用于重新索引的 ElasticSearch 无痛脚本
【发布时间】:2018-10-01 20:16:33
【问题描述】:

我们正在尝试使用以下无痛脚本来重新索引我们在 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/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 即原始索引。

我们怎样才能做到这一点? 我们可以修改上面的无痛脚本吗?

提前致谢!

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

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

    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'
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 还是把副本复制回原来的索引名?
      • @adifferentben 可能不可行,因为例如复制需要太多时间,或者您希望应用程序的停机时间最短。切换别名只需几毫秒。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-24
      • 2017-07-31
      • 2021-05-16
      相关资源
      最近更新 更多