1. 程式人生 > >Elasticsearch如何物理刪除給定期限的歷史資料?

Elasticsearch如何物理刪除給定期限的歷史資料?

1、題記

想到刪除,基礎認知是delete,細分為刪除文件(document)和刪除索引;要刪除歷史資料,基礎認知是:刪除了給定條件的資料,用delete_by_query。 
實際操作發現: 
- 刪除文件後,磁碟空間並沒有立即減少,反而增加了? 
- 除了定時任務+delete_by_query,有沒有更好的方式呢?

2、常見的刪除操作

2.1 刪除單個文件

DELETE /twitter/_doc/1

2.2 刪除滿足給定條件的文件

POST twitter/_delete_by_query
{
  "query": { 
    "match": {
      "message": "some message"
    }
  }
}

注意:執行批量刪除的時候,可能會發生版本衝突。強制執行刪除的方式如下:

POST twitter/_doc/_delete_by_query?conflicts=proceed
{
  "query": {
    "match_all": {}
  }
}

2.3 刪除單個索引

DELETE /twitter

2.4 刪除所有索引

DELETE /_all

或者

DELETE /*

刪除所有索引是非常危險的操作,要注意謹慎操作。

3、刪除文件後臺做了什麼?

執行刪除後的返回結果:

{
  "_index": "test_index",
  "_type": "test_type",
  "_id": "22",
  "_version": 2,
  "result": "deleted",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 2,
  "_primary_term": 17
}

解讀:

索引的每個文件都是版本化的。 
刪除文件時,可以指定版本以確保我們試圖刪除的相關文件實際上被刪除,並且在此期間沒有更改。

每個在文件上執行的寫操作,包括刪除,都會使其版本增加

真正的刪除時機:

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

.

4、刪除索引和刪除文件的區別?

1)刪除索引是會立即釋放空間的,不存在所謂的“標記”邏輯。

2)刪除文件的時候,是將新文件寫入,同時將舊文件標記為已刪除。 磁碟空間是否釋放取決於新舊文件是否在同一個segment file裡面,因此ES後臺的segment merge在合併segment file的過程中有可能觸發舊文件的物理刪除

但因為一個shard可能會有上百個segment file,還是有很大機率新舊文件存在於不同的segment裡而無法物理刪除。想要手動釋放空間,只能是定期做一下force merge,並且將max_num_segments設定為1。

POST /_forcemerge

5、如何僅儲存最近100天的資料?

有了上面的認知,僅儲存近100天的資料任務分解為: 
- 1)delete_by_query設定檢索近100天資料; 
- 2)執行forcemerge操作,手動釋放磁碟空間。

刪除指令碼如下:

#!/bin/sh
curl -H'Content-Type:application/json' -d'{
    "query": {
        "range": {
            "pt": {
                "lt": "now-100d",
                "format": "epoch_millis"
            }
        }
    }
}
' -XPOST "http://192.168.1.101:9200/logstash_*/
_delete_by_query?conflicts=proceed"

merge指令碼如下:

#!/bin/sh
curl -XPOST 'http://192.168.1.101:9200/_forcemerge?
only_expunge_deletes=true&max_num_segments=1'

6、有沒有更通用的方法?

有,使用ES官網工具——curator工具。

6.1 curator簡介

主要目的:規劃和管理ES的索引。支援常見操作:建立、刪除、合併、reindex、快照等操作。

6.2 curator官網地址

6.3 curator安裝嚮導

注意: 
curator各種部落格教程層出不窮,但curator舊版本和新版本有較大差異,建議參考官網最新手冊部署。 
舊版本命令列方式新版本已不支援。

6.4 curator命令列操作

$ curator --help
Usage: curator [OPTIONS] ACTION_FILE

  Curator for Elasticsearch indices.

  See http://elastic.co/guide/en/elasticsearch/client/curator/current

Options:
  --config PATH  Path to configuration file. Default: ~/.curator/curator.yml
  --dry-run      Do not perform any changes.
  --version      Show the version and exit.
  --help         Show this message and exit.

核心: 
- 配置檔案config.yml:配置要連線的ES地址、日誌配置、日誌級別等;

  • 執行檔案action.yml: 配置要執行的操作(可批量)、配置索引的格式(字首匹配、正則匹配方式等)

6.5 curator適用場景

最重要的是:

  • 僅以刪除操作為例:curator可以非常簡單地刪除x天后的索引的前提是:索引命名要遵循特定的命名模式——如:以天為命名的索引:logstash_2018.04.05。

  • 命名模式需要和action.yml中的delete_indices下的timestring對應。

7、小結

  • 多參考官網最新的文件,歷史版本的歷史文件很容易誤導人;
  • 多真正去實踐,而不是僅限於知道;
  • medcl:ES新版本6.3 有一個 Index LifeCycle Management 可以很方便的管理索引的儲存期限。

參考: