Elasticsearch根据字段值删除文档

时间:2017-04-06 10:05:02

标签: elasticsearch

我是Elasticsearch的新手,并尝试找出一种方法来删除包含时间戳作为其值的字段的文档。

我有一个名为 lastmodifiedtime 字段的文档。该字段的类型为long,用于存储时间戳(即自EPOCH以来的毫秒数。例如,1486709502)。

现在我有一个用例,我需要删除所有lastmodifiedtime超过一天的文档。所以我找出对应于一天较旧时间的timstamp值,然后尝试删除文档。

我尝试了如下的REST调用

DELETE http://localhost:9200/testindex/testtype/_query
"query": {
    "term" : {
        "lastmodifiedtime" : {
        "lte": 1486709504
        }
    }
 }

但得到以下回应:

{
    "found": false,
    "_index": "testindex",
    "_type": "testtype",
    "_id": "_query",
    "_version": 4,
    "result": "not_found",
    "_shards":
    {
        "total": 2,
        "successful": 1,
        "failed": 0
    }
}

你能帮我解决一下如何应对这种情况吗?

1 个答案:

答案 0 :(得分:2)

您可以使用delete_by查询API

POST http://localhost:9200/testindex/testtype/_delete_by_query
{
  "query": {
    "range": {
      "lastmodifiedtime": {
        "lte": 1486709504
      }
    }
  }
}

由于