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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值