今天遇到一个有趣的问题,那就是如何实现在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