2017-04-06 266 views
0

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

我有一个文档,其字段名为lastmodifiedtime。此字段的类型是长且被用于存储时间戳(因为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

这只是意味着你的查询没有返回任何比赛。你确定文件是否存在(只运行文件查询最后修改时间LTE 1486709504并检查是否返回结果 –

+0

如果您使用的是> = 5.2 elasticsearch试试这个:https://www.elastic.co/guide/en/elasticsearch /reference/current/docs-delete-by-query.html – rad11

+0

'1486709504'是秒数,'1486709504000'是毫秒数。试一试。 – Val

回答

2

可以使用delete_by查询API

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

感谢

+0

我试过这个,但没有运气:(。获得以下错误: { “错误”: { “ROOT_CAUSE”: [ { “类型”: “parse_exception”, “原因”: “无法导出xcontent” } ], “类型”: “parse_exception”, “原因”:“未能派生x内容” }, “status”:400 } –

+0

ahh ,,,你是否想要做一个范围查询? – user3775217

+0

我已经编辑我的答案,这个现在应该为你工作 – user3775217