首页 文章

如何定期删除弹性搜索索引?

提问于
浏览
0

我每天都创建索引来存储搜索历史记录,我在我的applciation中使用这些索引来建议,这也有助于我根据历史记录进行建议 .

现在我必须保持最近10天的历史 . 那么Elastic搜索中是否有任何功能允许我定期创建和删除索引?

2 回答

  • 2

    我唯一能想到的是使用数据数学:https://www.elastic.co/guide/en/elasticsearch/reference/current/date-math-index-names.html

    在某种意义上,你可以这样做:

    DELETE <logs-{now%2Fd-10d}>
    

    由于url编码,这在curl中不起作用 . 你可以在curl中做这样的事情:

    curl -XDELETE 'localhost:9200/<logs-%7Bnow%2Fd-10d%7D>'
    

    这两个示例都删除了10天的索引 . 它不能帮助您删除超过10天的索引,不认为这是可能的 . 而且它们不是弹性搜索中的触发器或东西 .

    所以我会坚持与策展人一起完成一项cron工作,但你也有这个选择 .

  • 7

    我不知道elasticsearch是否具有这样的内置功能,但你可以用curator和cron作业实现你想要的功能 .

    示例 curator 命令是:

    Curator 3.x语法[已弃用]:

    curator --host <IP> delete indices --older-than 10 --prefix "index-prefix-" --time-unit days  --timestring '%Y-%m-%d'
    

    策展人5.1.1语法:

    curator_cli --host <IP> --port <PORT> delete_indices --filter_list '[{"filtertype":"age","source":"creation_date","direction":"older","unit":"days","unit_count":10},{"filtertype":"pattern","kind":"prefix","value":"index-prefix-"}]'
    

    每天使用cron作业运行此命令,以删除名称以 index-prefix- 开头并且位于 <IP><PORT> 的Elasticsearch实例上的10天以上的索引 .

    有关更多策展人命令行选项,请参阅:https://www.elastic.co/guide/en/elasticsearch/client/curator/current/singleton-cli.html

    有关cron用法的更多信息,请参阅:http://kvz.io/blog/2007/07/29/schedule-tasks-on-linux-using-crontab/

相关问题