elasticsearch低版本根據時間範圍刪除數據實例

今天遇到一個有趣的問題,那就是如何實現在elasticsearch低版本刪除某段時間範圍內的es數據。
高版本的ES(比如elasticsearch:5.5)有_delete_by_query方法,詳細請看api:https://www.elastic.co/guide/en/elasticsearch/reference/5.5/docs-delete-by-query.html
當前解決的ES版本爲1.7,刪除億級別數據,經測很實用。
請求配置如下:
$ curl -XDELETE 'http://localhost:9200/test-index/_query' -d '{
    "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must": {
            "bool": {
              "must": [
                {
                  "query": {
                    "match": {
                      "appId": {
                        "query": "TEST",
                        "type": "phrase"
                      }
                    }
                  }
                },
                {
                  "range": {
                    "eTime": {
                      "from": "2019-01-23T05:00:00.000Z",
                      "to": "2019-01-23T05:30:00.000Z",
                      "include_lower": true,
                      "include_upper": true
                    }
                  }
                }
              ]
            }
          }
        }
      }
    }
  }
}'
match表示模糊匹配,它會把檢索的value進行分詞,並進行檢索。"type": "phrase"等同於match_phrase方法,這裏不多說,自行百度。
include_lower代表是否包含左邊界值,默認是true ,include_upper代表是否包含右邊界值,默認是true 。
另一種使用的是term,進行精確匹配,配置如下:
$ curl -XDELETE 'http://localhost:9200/test-index/_query' -d '{
    "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must": {
            "bool": {
              "must": [
                {
                  "query": {
                    "term": {
                      "appId": "FIRS"
                    }
                  }
                },
                {
                  "range": {
                    "eTime": {
                      "from": "2019-01-23T05:00:00.000Z",
                      "to": "2019-01-23T05:30:00.000Z",
                      "include_lower": true,
                      "include_upper": true
                    }
                  }
                }
              ]
            }
          }
        }
      }
    }
  }
}'

補充:建議如果不知道查詢的json怎麼寫,可以使用essql組件幫助你,explain以下就可以得到json數據,只要你會sql語法即可。

參考api:https://www.elastic.co/guide/en/elasticsearch/reference/1.7/docs-delete-by-query.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章