刪除文檔也算是經常使用的操做了...若是把Elasticsearch當作一款普通的數據庫,那麼刪除操做天然就很經常使用了。若是僅僅是全文檢索,可能就不會太經常使用到刪除。node
刪除API,能夠根據特定的ID刪除文檔。數據庫
$ curl -XDELETE 'http://localhost:9200/twitter/tweet/1'
會返回下面的消息:api
{ "_shards" : { "total" : 10, "failed" : 0, "successful" : 10 }, "found" : true, "_index" : "twitter", "_type" : "tweet", "_id" : "1", "_version" : 2 }
每一個索引都經過版原本維護。當想要刪除某個文檔的時候,版本能夠用來確認刪除的文檔。而想要刪除一個已經被刪除的文檔,則不會發生任何變化。curl
若是在索引的時候提供了路由,那麼刪除的時候,也須要指定相應的路由:分佈式
$ curl -XDELETE 'http://localhost:9200/twitter/tweet/1?routing=kimchy'
上面的例子中,想要刪除id爲1的索引,會經過固定的路由查找文檔。若是路由不正確,可能查不到相關的文檔。對於某種狀況,須要使用_routing參數,可是卻沒有任何的值,那麼刪除請求會廣播到每一個分片,執行刪除操做。this
刪除操做也能夠指定父文檔。再刪除父文檔的時候,不會刪除子文檔。有一種刪除子文檔的方法,就是使用delete-by-query。url
在執行刪除操做時,若是沒有建立過索引,則會自動建立。類型也是同樣。code
對於分佈式的環境,主分片和副分片會維護一個共同的組ID,執行刪除操做會向這個組ID發送請求。orm
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參數設置爲true,能夠在刪除操做執行後,當即刷新分片,保證其數據能夠當即被查詢。不過要慎用!
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'