delete-by-query.asciidoc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. [[docs-delete-by-query]]
  2. == Delete By Query API
  3. The delete by query API allows to delete documents from one or more
  4. indices and one or more types based on a query. The query can either be
  5. provided using a simple query string as a parameter, or using the
  6. <<query-dsl,Query DSL>> defined within the request
  7. body. Here is an example:
  8. [source,js]
  9. --------------------------------------------------
  10. $ curl -XDELETE 'http://localhost:9200/twitter/tweet/_query?q=user:kimchy'
  11. $ curl -XDELETE 'http://localhost:9200/twitter/tweet/_query' -d '{
  12. "term" : { "user" : "kimchy" }
  13. }
  14. '
  15. --------------------------------------------------
  16. Both above examples end up doing the same thing, which is delete all
  17. tweets from the twitter index for a certain user. The result of the
  18. commands is:
  19. [source,js]
  20. --------------------------------------------------
  21. {
  22. "ok" : true,
  23. "_indices" : {
  24. "twitter" : {
  25. "_shards" : {
  26. "total" : 5,
  27. "successful" : 5,
  28. "failed" : 0
  29. }
  30. }
  31. }
  32. }
  33. --------------------------------------------------
  34. Note, delete by query bypasses versioning support. Also, it is not
  35. recommended to delete "large chunks of the data in an index", many
  36. times, it's better to simply reindex into a new index.
  37. [float]
  38. [[multiple-indices]]
  39. === Multiple Indices and Types
  40. The delete by query API can be applied to multiple types within an
  41. index, and across multiple indices. For example, we can delete all
  42. documents across all types within the twitter index:
  43. [source,js]
  44. --------------------------------------------------
  45. $ curl -XDELETE 'http://localhost:9200/twitter/_query?q=user:kimchy'
  46. --------------------------------------------------
  47. We can also delete within specific types:
  48. [source,js]
  49. --------------------------------------------------
  50. $ curl -XDELETE 'http://localhost:9200/twitter/tweet,user/_query?q=user:kimchy'
  51. --------------------------------------------------
  52. We can also delete all tweets with a certain tag across several indices
  53. (for example, when each user has his own index):
  54. [source,js]
  55. --------------------------------------------------
  56. $ curl -XDELETE 'http://localhost:9200/kimchy,elasticsearch/_query?q=tag:wow'
  57. --------------------------------------------------
  58. Or even delete across all indices:
  59. [source,js]
  60. --------------------------------------------------
  61. $ curl -XDELETE 'http://localhost:9200/_all/_query?q=tag:wow'
  62. --------------------------------------------------
  63. [float]
  64. [[parameters]]
  65. === Request Parameters
  66. When executing a delete by query using the query parameter `q`, the
  67. query passed is a query string using Lucene query parser. There are
  68. additional parameters that can be passed:
  69. [cols="<,<",options="header",]
  70. |=======================================================================
  71. |Name |Description
  72. |df |The default field to use when no field prefix is defined within the
  73. query.
  74. |analyzer |The analyzer name to be used when analyzing the query string.
  75. |default_operator |The default operator to be used, can be `AND` or
  76. `OR`. Defaults to `OR`.
  77. |=======================================================================
  78. [float]
  79. [[request-body]]
  80. === Request Body
  81. The delete by query can use the <<query-dsl,Query
  82. DSL>> within its body in order to express the query that should be
  83. executed and delete all documents. The body content can also be passed
  84. as a REST parameter named `source`.
  85. [float]
  86. [[distributed]]
  87. === Distributed
  88. The delete by query API is broadcast across all primary shards, and from
  89. there, replicated across all shards replicas.
  90. [float]
  91. [[routing]]
  92. === Routing
  93. The routing value (a comma separated list of the routing values) can be
  94. specified to control which shards the delete by query request will be
  95. executed on.
  96. [float]
  97. [[replication-type]]
  98. === Replication Type
  99. The replication of the operation can be done in an asynchronous manner
  100. to the replicas (the operation will return once it has be executed on
  101. the primary shard). The `replication` parameter can be set to `async`
  102. (defaults to `sync`) in order to enable it.
  103. [float]
  104. [[consistency]]
  105. === Write Consistency
  106. Control if the operation will be allowed to execute based on the number
  107. of active shards within that partition (replication group). The values
  108. allowed are `one`, `quorum`, and `all`. The parameter to set it is
  109. `consistency`, and it defaults to the node level setting of
  110. `action.write_consistency` which in turn defaults to `quorum`.
  111. For example, in a N shards with 2 replicas index, there will have to be
  112. at least 2 active shards within the relevant partition (`quorum`) for
  113. the operation to succeed. In a N shards with 1 replica scenario, there
  114. will need to be a single shard active (in this case, `one` and `quorum`
  115. is the same).