删除文档也算是经常使用的操做了...若是把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'