想到删除,基础认知是delete,细分为删除文档(document)和删除索引;要删除历史数据,基础认知是:删除了给定条件的数据,用delete_by_query。 实际操作发现:
1DELETE /twitter/_doc/1
1POST twitter/_delete_by_query
2{
3 "query": {
4 "match": {
5 "message": "some message"
6 }
7 }
8}
注意:执行批量删除的时候,可能会发生版本冲突。强制执行删除的方式如下:
1POST twitter/_doc/_delete_by_query?conflicts=proceed
2{
3 "query": {
4 "match_all": {}
5 }
6}
1DELETE /twitter
1DELETE /_all
或者
1DELETE /*
删除所有索引是非常危险的操作,要注意谨慎操作。
执行删除后的返回结果:
1{
2 "_index": "test_index",
3 "_type": "test_type",
4 "_id": "22",
5 "_version": 2,
6 "result": "deleted",
7 "_shards": {
8 "total": 2,
9 "successful": 1,
10 "failed": 0
11 },
12 "_seq_no": 2,
13 "_primary_term": 17
14}
解读:
索引的每个文档都是版本化的。 删除文档时,可以指定版本以确保我们试图删除的相关文档实际上被删除,并且在此期间没有更改。
每个在文档上执行的写操作,包括删除,都会使其版本增加。
真正的删除时机:
deleting a document doesn’t immediately remove the document from disk; it just marks it as deleted. Elasticsearch will clean up deleted documents in the background as you continue to index more data.
1)删除索引是会立即释放空间的,不存在所谓的“标记”逻辑。
2)删除文档的时候,是将新文档写入,同时将旧文档标记为已删除。 磁盘空间是否释放取决于新旧文档是否在同一个segment file里面,因此ES后台的segment merge在合并segment file的过程中有可能触发旧文档的物理删除。
但因为一个shard可能会有上百个segment file,还是有很大几率新旧文档存在于不同的segment里而无法物理删除。想要手动释放空间,只能是定期做一下force merge,并且将max_num_segments设置为1。
1POST /_forcemerge
有了上面的认知,仅保存近100天的数据任务分解为:
删除脚本如下:
1#!/bin/sh
2curl -H'Content-Type:application/json' -d'{
3 "query": {
4 "range": {
5 "pt": {
6 "lt": "now-100d",
7 "format": "epoch_millis"
8 }
9 }
10 }
11}
12' -XPOST "http://192.168.1.101:9200/logstash_*/
13_delete_by_query?conflicts=proceed"
merge脚本如下:
1#!/bin/sh
2curl -XPOST 'http://192.168.1.101:9200/_forcemerge?
3only_expunge_deletes=true&max_num_segments=1'
有,使用ES官网工具——curator工具。
主要目的:规划和管理ES的索引。支持常见操作:创建、删除、合并、reindex、快照等操作。
http://t.cn/RuwN0oM Git地址:https://github.com/elastic/curator
地址:http://t.cn/RuwCkBD
注意: curator各种博客教程层出不穷,但curator旧版本和新版本有较大差异,建议参考官网最新手册部署。 旧版本命令行方式新版本已不支持。
1$ curator --help
2Usage: curator [OPTIONS] ACTION_FILE
3 Curator for Elasticsearch indices.
4 See http://elastic.co/guide/en/elasticsearch/client/curator/current
5Options:
6 --config PATH Path to configuration file. Default: ~/.curator/curator.yml
7 --dry-run Do not perform any changes.
8 --version Show the version and exit.
9 --help Show this message and exit.
核心:
最重要的是:
参考:
[1]http://t.cn/RuwOTv [2]http://t.cn/RuwXHBr [3]http://t.cn/RuwOofC
本文分享自 铭毅天下Elasticsearch 微信公众号,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文参与 腾讯云自媒体分享计划 ,欢迎热爱写作的你一起参与!