Elasticsearch增刪改查 之 —— Delete刪除

weixin_34292959發表於2016-03-26

刪除文件也算是常用的操作了...如果把Elasticsearch當做一款普通的資料庫,那麼刪除操作自然就很常用了。如果僅僅是全文檢索,可能就不會太常用到刪除。

Delete API

刪除API,可以根據特定的ID刪除文件。

$ curl -XDELETE 'http://localhost:9200/twitter/tweet/1'

會返回下面的訊息:

{
    "_shards" : {
        "total" : 10,
        "failed" : 0,
        "successful" : 10
    },
    "found" : true,
    "_index" : "twitter",
    "_type" : "tweet",
    "_id" : "1",
    "_version" : 2
}

版本

每個索引都通過版本來維護。當想要刪除某個文件的時候,版本可以用來確認刪除的文件。而想要刪除一個已經被刪除的文件,則不會發生任何變化。

路由

如果在索引的時候提供了路由,那麼刪除的時候,也需要指定相應的路由:

$ curl -XDELETE 'http://localhost:9200/twitter/tweet/1?routing=kimchy'

上面的例子中,想要刪除id為1的索引,會通過固定的路由查詢文件。如果路由不正確,可能查不到相關的文件。對於某種情況,需要使用_routing引數,但是卻沒有任何的值,那麼刪除請求會廣播到每個分片,執行刪除操作。

Parent

刪除操作也可以指定父文件。再刪除父文件的時候,不會刪除子文件。有一種刪除子文件的方法,就是使用delete-by-query。

自動建立索引

在執行刪除操作時,如果沒有建立過索引,則會自動建立。型別也是一樣。

分散式

對於分散式的環境,主分片和副分片會維護一個共同的組ID,執行刪除操作會向這個組ID傳送請求。

Write Consistency

Control if the operation will be allowed to execute based on the number of active shards within that partition (replication group). The values allowed are one, quorum, and all. The parameter to set it isconsistency, and it defaults to the node level setting of action.write_consistency which in turn defaults toquorum.

For example, in a N shards with 2 replicas index, there will have to be at least 2 active shards within the relevant partition (quorum) for the operation to succeed. In a N shards with 1 replica scenario, there will need to be a single shard active (in this case, one and quorum is the same).

refresh

refresh引數設定為true,可以在刪除操作執行後,立即重新整理分片,保證其資料可以立即被查詢。不過要慎用!

timeout

The primary shard assigned to perform the delete operation might not be available when the delete operation is executed. Some reasons for this might be that the primary shard is currently recovering from a store or undergoing relocation. By default, the delete operation will wait on the primary shard to become available for up to 1 minute before failing and responding with an error.

當分片不可用的時候,刪除操作會等待一段時間執行。可以設定其timeout

$ curl -XDELETE 'http://localhost:9200/twitter/tweet/1?timeout=5m'

相關文章