2017-04-06 268 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