update-by-query.asciidoc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. [[docs-update-by-query]]
  2. == Update By Query API
  3. experimental[The update-by-query API is new and should still be considered experimental. The API may change in ways that are not backwards compatible]
  4. The simplest usage of `updateByQuery` updates each
  5. document in an index without changing the source. This usage enables
  6. <<picking-up-a-new-property,picking up a new property>> or another online
  7. mapping change.
  8. [source,java]
  9. --------------------------------------------------
  10. UpdateByQueryRequestBuilder updateByQuery = UpdateByQueryAction.INSTANCE.newRequestBuilder(client);
  11. updateByQuery.source("source_index").abortOnVersionConflict(false);
  12. BulkIndexByScrollResponse response = updateByQuery.get();
  13. --------------------------------------------------
  14. Calls to the `updateByQuery` API start by getting a snapshot of the index, indexing
  15. any documents found using the `internal` versioning.
  16. NOTE: Version conflicts happen when a document changes between the time of the
  17. snapshot and the time the index request processes.
  18. When the versions match, `updateByQuery` updates the document
  19. and increments the version number.
  20. All update and query failures cause `updateByQuery` to abort. These failures are
  21. available from the `BulkIndexByScrollResponse#getIndexingFailures` method. Any
  22. successful updates remain and are not rolled back. While the first failure
  23. causes the abort, the response contains all of the failures generated by the
  24. failed bulk request.
  25. To prevent version conflicts from causing `updateByQuery` to abort, set
  26. `abortOnVersionConflict(false)`. The first example does this because it is
  27. trying to pick up an online mapping change and a version conflict means that
  28. the conflicting document was updated between the start of the `updateByQuery`
  29. and the time when it attempted to update the document. This is fine because
  30. that update will have picked up the online mapping update.
  31. The `UpdateByQueryRequestBuilder` API supports filtering the updated documents,
  32. limiting the total number of documents to update, and updating documents
  33. with a script:
  34. [source,java]
  35. --------------------------------------------------
  36. UpdateByQueryRequestBuilder updateByQuery = UpdateByQueryAction.INSTANCE.newRequestBuilder(client);
  37. updateByQuery.source("source_index")
  38. .filter(termQuery("level", "awesome"))
  39. .size(1000)
  40. .script(new Script("ctx._source.awesome = 'absolutely'", ScriptType.INLINE, "painless", emptyMap()));
  41. BulkIndexByScrollResponse response = updateByQuery.get();
  42. --------------------------------------------------
  43. `UpdateByQueryRequestBuilder` also enables direct access to the query used
  44. to select the documents. You can use this access to change the default scroll size or
  45. otherwise modify the request for matching documents.
  46. [source,java]
  47. --------------------------------------------------
  48. UpdateByQueryRequestBuilder updateByQuery = UpdateByQueryAction.INSTANCE.newRequestBuilder(client);
  49. updateByQuery.source("source_index")
  50. .source().setSize(500);
  51. BulkIndexByScrollResponse response = updateByQuery.get();
  52. --------------------------------------------------
  53. You can also combine `size` with sorting to limit the documents updated:
  54. [source,java]
  55. --------------------------------------------------
  56. UpdateByQueryRequestBuilder updateByQuery = UpdateByQueryAction.INSTANCE.newRequestBuilder(client);
  57. updateByQuery.source("source_index").size(100)
  58. .source().addSort("cat", SortOrder.DESC);
  59. BulkIndexByScrollResponse response = updateByQuery.get();
  60. --------------------------------------------------
  61. In addition to changing the `_source` field for the document, you can use a
  62. script to change the action, similar to the Update API:
  63. [source,java]
  64. --------------------------------------------------
  65. UpdateByQueryRequestBuilder updateByQuery = UpdateByQueryAction.INSTANCE.newRequestBuilder(client);
  66. updateByQuery.source("source_index")
  67. .script(new Script(
  68. "if (ctx._source.awesome == 'absolutely) {"
  69. + " ctx.op='noop'
  70. + "} else if (ctx._source.awesome == 'lame') {"
  71. + " ctx.op='delete'"
  72. + "} else {"
  73. + "ctx._source.awesome = 'absolutely'}", ScriptType.INLINE, "painless", emptyMap()));
  74. BulkIndexByScrollResponse response = updateByQuery.get();
  75. --------------------------------------------------
  76. As in the <<docs-update,Update API>>, you can set the value of `ctx.op` to change the
  77. operation that executes:
  78. `noop`::
  79. Set `ctx.op = "noop"` if your script doesn't make any
  80. changes. The `updateByQuery` operaton then omits that document from the updates.
  81. This behavior increments the `noop` counter in the
  82. <<docs-update-by-query-response-body, response body>>.
  83. `delete`::
  84. Set `ctx.op = "delete"` if your script decides that the document must be
  85. deleted. The deletion will be reported in the `deleted` counter in the
  86. <<docs-update-by-query-response-body, response body>>.
  87. Setting `ctx.op` to any other value generates an error. Setting any
  88. other field in `ctx` generates an error.
  89. This API doesn't allow you to move the documents it touches, just modify their
  90. source. This is intentional! We've made no provisions for removing the document
  91. from its original location.
  92. You can also perform these operations on multiple indices and types at once, similar to the search API:
  93. [source,java]
  94. --------------------------------------------------
  95. UpdateByQueryRequestBuilder updateByQuery = UpdateByQueryAction.INSTANCE.newRequestBuilder(client);
  96. updateByQuery.source("foo", "bar").source().setTypes("a", "b");
  97. BulkIndexByScrollResponse response = updateByQuery.get();
  98. --------------------------------------------------
  99. If you provide a `routing` value then the process copies the routing value to the scroll query,
  100. limiting the process to the shards that match that routing value:
  101. [source,java]
  102. --------------------------------------------------
  103. UpdateByQueryRequestBuilder updateByQuery = UpdateByQueryAction.INSTANCE.newRequestBuilder(client);
  104. updateByQuery.source().setRouting("cat");
  105. BulkIndexByScrollResponse response = updateByQuery.get();
  106. --------------------------------------------------
  107. `updateByQuery` can also use the <<ingest>> feature by
  108. specifying a `pipeline` like this:
  109. [source,java]
  110. --------------------------------------------------
  111. UpdateByQueryRequestBuilder updateByQuery = UpdateByQueryAction.INSTANCE.newRequestBuilder(client);
  112. updateByQuery.setPipeline("hurray");
  113. BulkIndexByScrollResponse response = updateByQuery.get();
  114. --------------------------------------------------
  115. [float]
  116. [[docs-update-by-query-task-api]]
  117. === Works with the Task API
  118. You can fetch the status of all running update-by-query requests with the
  119. <<tasks,Task API>>:
  120. [source,java]
  121. --------------------------------------------------
  122. ListTasksResponse tasksList = client.admin().cluster().prepareListTasks()
  123. .setActions(UpdateByQueryAction.NAME).setDetailed(true).get();
  124. for (TaskInfo info: tasksList.getTasks()) {
  125. TaskId taskId = info.getTaskId();
  126. BulkByScrollTask.Status status = (BulkByScrollTask.Status) info.getStatus();
  127. // do stuff
  128. }
  129. --------------------------------------------------
  130. With the `TaskId` shown above you can look up the task directly:
  131. // provide API Example
  132. [source,java]
  133. --------------------------------------------------
  134. GetTaskResponse get = client.admin().cluster().prepareGetTask(taskId).get();
  135. --------------------------------------------------
  136. [float]
  137. [[docs-update-by-query-cancel-task-api]]
  138. === Works with the Cancel Task API
  139. Any Update By Query can be canceled using the <<tasks,Task Cancel API>>:
  140. [source,java]
  141. --------------------------------------------------
  142. // Cancel all update-by-query requests
  143. client.admin().cluster().prepareCancelTasks().setActions(UpdateByQueryAction.NAME).get().getTasks()
  144. // Cancel a specific update-by-query request
  145. client.admin().cluster().prepareCancelTasks().setTaskId(taskId).get().getTasks()
  146. --------------------------------------------------
  147. Use the `list tasks` API to find the value of `taskId`.
  148. Cancelling a request is typically a very fast process but can take up to a few seconds.
  149. The task status API continues to list the task until the cancellation is complete.
  150. [float]
  151. [[docs-update-by-query-rethrottle]]
  152. === Rethrottling
  153. Use the `_rethrottle` API to change the value of `requests_per_second` on a running update:
  154. [source,java]
  155. --------------------------------------------------
  156. RethrottleAction.INSTANCE.newRequestBuilder(client).setTaskId(taskId).setRequestsPerSecond(2.0f).get();
  157. --------------------------------------------------
  158. Use the `list tasks` API to find the value of `taskId`.
  159. As with the `updateByQuery` API, the value of `requests_per_second`
  160. can be any positive float value to set the level of the throttle, or `Float.POSITIVE_INFINITY` to disable throttling.
  161. A value of `requests_per_second` that speeds up the process takes
  162. effect immediately. `requests_per_second` values that slow the query take effect
  163. after completing the current batch in order to prevent scroll timeouts.