update-by-query.asciidoc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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 `_update_by_query` just performs an update on every
  5. document in the index without changing the source. This is useful to
  6. <<picking-up-a-new-property,pick up a new property>> or some other online
  7. mapping change. Here is the API:
  8. [source,js]
  9. --------------------------------------------------
  10. POST twitter/_update_by_query?conflicts=proceed
  11. --------------------------------------------------
  12. // CONSOLE
  13. // TEST[setup:big_twitter]
  14. That will return something like this:
  15. [source,js]
  16. --------------------------------------------------
  17. {
  18. "took" : 147,
  19. "timed_out": false,
  20. "updated": 120,
  21. "batches": 1,
  22. "version_conflicts": 0,
  23. "noops": 0,
  24. "retries": {
  25. "bulk": 0,
  26. "search": 0
  27. },
  28. "throttled_millis": 0,
  29. "requests_per_second": "unlimited",
  30. "throttled_until_millis": 0,
  31. "total": 120,
  32. "failures" : [ ]
  33. }
  34. --------------------------------------------------
  35. // TESTRESPONSE[s/"took" : 147/"took" : "$body.took"/]
  36. `_update_by_query` gets a snapshot of the index when it starts and indexes what
  37. it finds using `internal` versioning. That means that you'll get a version
  38. conflict if the document changes between the time when the snapshot was taken
  39. and when the index request is processed. When the versions match the document
  40. is updated and the version number is incremented.
  41. All update and query failures cause the `_update_by_query` to abort and are
  42. returned in the `failures` of the response. The updates that have been
  43. performed still stick. In other words, the process is not rolled back, only
  44. aborted. While the first failure causes the abort all failures that are
  45. returned by the failing bulk request are returned in the `failures` element so
  46. it's possible for there to be quite a few.
  47. If you want to simply count version conflicts not cause the `_update_by_query`
  48. to abort you can set `conflicts=proceed` on the url or `"conflicts": "proceed"`
  49. in the request body. The first example does this because it is just trying to
  50. pick up an online mapping change and a version conflict simply means that the
  51. conflicting document was updated between the start of the `_update_by_query`
  52. and the time when it attempted to update the document. This is fine because
  53. that update will have picked up the online mapping update.
  54. Back to the API format, you can limit `_update_by_query` to a single type. This
  55. will only update `tweet` documents from the `twitter` index:
  56. [source,js]
  57. --------------------------------------------------
  58. POST twitter/tweet/_update_by_query?conflicts=proceed
  59. --------------------------------------------------
  60. // CONSOLE
  61. // TEST[setup:twitter]
  62. You can also limit `_update_by_query` using the
  63. <<query-dsl,Query DSL>>. This will update all documents from the
  64. `twitter` index for the user `kimchy`:
  65. [source,js]
  66. --------------------------------------------------
  67. POST twitter/_update_by_query?conflicts=proceed
  68. {
  69. "query": { <1>
  70. "term": {
  71. "user": "kimchy"
  72. }
  73. }
  74. }
  75. --------------------------------------------------
  76. // CONSOLE
  77. // TEST[setup:twitter]
  78. <1> The query must be passed as a value to the `query` key, in the same
  79. way as the <<search-search,Search API>>. You can also use the `q`
  80. parameter in the same way as the search api.
  81. So far we've only been updating documents without changing their source. That
  82. is genuinely useful for things like
  83. <<picking-up-a-new-property,picking up new properties>> but it's only half the
  84. fun. `_update_by_query` supports a `script` object to update the document. This
  85. will increment the `likes` field on all of kimchy's tweets:
  86. [source,js]
  87. --------------------------------------------------
  88. POST twitter/_update_by_query
  89. {
  90. "script": {
  91. "inline": "ctx._source.likes++"
  92. },
  93. "query": {
  94. "term": {
  95. "user": "kimchy"
  96. }
  97. }
  98. }
  99. --------------------------------------------------
  100. // CONSOLE
  101. // TEST[setup:twitter]
  102. Just as in <<docs-update,Update API>> you can set `ctx.op = "noop"` if
  103. your script decides that it doesn't have to make any changes. That will cause
  104. `_update_by_query` to omit that document from its updates. Setting `ctx.op` to
  105. anything else is an error. If you want to delete by a query you can use the
  106. <<docs-delete-by-query,Delete By Query API>> instead. Setting any
  107. other field in `ctx` is an error.
  108. Note that we stopped specifying `conflicts=proceed`. In this case we want a
  109. version conflict to abort the process so we can handle the failure.
  110. This API doesn't allow you to move the documents it touches, just modify their
  111. source. This is intentional! We've made no provisions for removing the document
  112. from its original location.
  113. It's also possible to do this whole thing on multiple indexes and multiple
  114. types at once, just like the search API:
  115. [source,js]
  116. --------------------------------------------------
  117. POST twitter,blog/tweet,post/_update_by_query
  118. --------------------------------------------------
  119. // CONSOLE
  120. // TEST[s/^/PUT twitter\nPUT blog\nGET _cluster\/health?wait_for_status=yellow\n/]
  121. If you provide `routing` then the routing is copied to the scroll query,
  122. limiting the process to the shards that match that routing value:
  123. [source,js]
  124. --------------------------------------------------
  125. POST twitter/_update_by_query?routing=1
  126. --------------------------------------------------
  127. // CONSOLE
  128. // TEST[setup:twitter]
  129. By default `_update_by_query` uses scroll batches of 1000. You can change the
  130. batch size with the `scroll_size` URL parameter:
  131. [source,js]
  132. --------------------------------------------------
  133. POST twitter/_update_by_query?scroll_size=100
  134. --------------------------------------------------
  135. // CONSOLE
  136. // TEST[setup:twitter]
  137. `_update_by_query` can also use the <<ingest>> feature by
  138. specifying a `pipeline` like this:
  139. [source,js]
  140. --------------------------------------------------
  141. PUT _ingest/pipeline/set-foo
  142. {
  143. "description" : "sets foo",
  144. "processors" : [ {
  145. "set" : {
  146. "field": "foo",
  147. "value": "bar"
  148. }
  149. } ]
  150. }
  151. POST twitter/_update_by_query?pipeline=set-foo
  152. --------------------------------------------------
  153. // CONSOLE
  154. // TEST[setup:twitter]
  155. [float]
  156. === URL Parameters
  157. In addition to the standard parameters like `pretty`, the Update By Query API
  158. also supports `refresh`, `wait_for_completion`, `consistency`, and `timeout`.
  159. Sending the `refresh` will update all shards in the index being updated when
  160. the request completes. This is different than the Index API's `refresh`
  161. parameter which causes just the shard that received the new data to be indexed.
  162. If the request contains `wait_for_completion=false` then Elasticsearch will
  163. perform some preflight checks, launch the request, and then return a `task`
  164. which can be used with <<docs-update-by-query-task-api,Tasks APIs>> to cancel
  165. or get the status of the task. For now, once the request is finished the task
  166. is gone and the only place to look for the ultimate result of the task is in
  167. the Elasticsearch log file. This will be fixed soon.
  168. `consistency` controls how many copies of a shard must respond to each write
  169. request. `timeout` controls how long each write request waits for unavailable
  170. shards to become available. Both work exactly how they work in the
  171. <<docs-bulk,Bulk API>>.
  172. `requests_per_second` can be set to any decimal number (`1.4`, `6`, `1000`, etc)
  173. and throttles the number of requests per second that the update by query issues.
  174. The throttling is done waiting between bulk batches so that it can manipulate
  175. the scroll timeout. The wait time is the difference between the time it took the
  176. batch to complete and the time `requests_per_second * requests_in_the_batch`.
  177. Since the batch isn't broken into multiple bulk requests large batch sizes will
  178. cause Elasticsearch to create many requests and then wait for a while before
  179. starting the next set. This is "bursty" instead of "smooth". The default is
  180. `unlimited` which is also the only non-number value that it accepts.
  181. [float]
  182. === Response body
  183. The JSON response looks like this:
  184. [source,js]
  185. --------------------------------------------------
  186. {
  187. "took" : 639,
  188. "updated": 0,
  189. "batches": 1,
  190. "version_conflicts": 2,
  191. "retries": {
  192. "bulk": 0,
  193. "search": 0
  194. }
  195. "throttled_millis": 0,
  196. "failures" : [ ]
  197. }
  198. --------------------------------------------------
  199. `took`::
  200. The number of milliseconds from start to end of the whole operation.
  201. `updated`::
  202. The number of documents that were successfully updated.
  203. `batches`::
  204. The number of scroll responses pulled back by the the update by query.
  205. `version_conflicts`::
  206. The number of version conflicts that the update by query hit.
  207. `retries`::
  208. The number of retries attempted by update-by-query. `bulk` is the number of bulk
  209. actions retried and `search` is the number of search actions retried.
  210. `throttled_millis`::
  211. Number of milliseconds the request slept to conform to `requests_per_second`.
  212. `failures`::
  213. Array of all indexing failures. If this is non-empty then the request aborted
  214. because of those failures. See `conflicts` for how to prevent version conflicts
  215. from aborting the operation.
  216. [float]
  217. [[docs-update-by-query-task-api]]
  218. === Works with the Task API
  219. While Update By Query is running you can fetch their status using the
  220. <<tasks,Task API>>:
  221. [source,js]
  222. --------------------------------------------------
  223. GET _tasks?detailed=true&action=*byquery
  224. --------------------------------------------------
  225. // CONSOLE
  226. The responses looks like:
  227. [source,js]
  228. --------------------------------------------------
  229. {
  230. "nodes" : {
  231. "r1A2WoRbTwKZ516z6NEs5A" : {
  232. "name" : "Tyrannus",
  233. "transport_address" : "127.0.0.1:9300",
  234. "host" : "127.0.0.1",
  235. "ip" : "127.0.0.1:9300",
  236. "attributes" : {
  237. "testattr" : "test",
  238. "portsfile" : "true"
  239. },
  240. "tasks" : {
  241. "r1A2WoRbTwKZ516z6NEs5A:36619" : {
  242. "node" : "r1A2WoRbTwKZ516z6NEs5A",
  243. "id" : 36619,
  244. "type" : "transport",
  245. "action" : "indices:data/write/update/byquery",
  246. "status" : { <1>
  247. "total" : 6154,
  248. "updated" : 3500,
  249. "created" : 0,
  250. "deleted" : 0,
  251. "batches" : 4,
  252. "version_conflicts" : 0,
  253. "noops" : 0,
  254. "retries": {
  255. "bulk": 0,
  256. "search": 0
  257. }
  258. "throttled_millis": 0
  259. },
  260. "description" : ""
  261. }
  262. }
  263. }
  264. }
  265. }
  266. --------------------------------------------------
  267. <1> this object contains the actual status. It is just like the response json
  268. with the important addition of the `total` field. `total` is the total number
  269. of operations that the reindex expects to perform. You can estimate the
  270. progress by adding the `updated`, `created`, and `deleted` fields. The request
  271. will finish when their sum is equal to the `total` field.
  272. [float]
  273. [[docs-update-by-query-cancel-task-api]]
  274. === Works with the Cancel Task API
  275. Any Update By Query can be canceled using the <<tasks,Task Cancel API>>:
  276. [source,js]
  277. --------------------------------------------------
  278. POST _tasks/taskid:1/_cancel
  279. --------------------------------------------------
  280. // CONSOLE
  281. The `task_id` can be found using the tasks API above.
  282. Cancelation should happen quickly but might take a few seconds. The task status
  283. API above will continue to list the task until it is wakes to cancel itself.
  284. [float]
  285. [[docs-update-by-query-rethrottle]]
  286. === Rethrottling
  287. The value of `requests_per_second` can be changed on a running update by query
  288. using the `_rethrottle` API:
  289. [source,js]
  290. --------------------------------------------------
  291. POST _update_by_query/taskid:1/_rethrottle?requests_per_second=unlimited
  292. --------------------------------------------------
  293. // CONSOLE
  294. The `task_id` can be found using the tasks API above.
  295. Just like when setting it on the `_update_by_query` API `requests_per_second`
  296. can be either `unlimited` to disable throttling or any decimal number like `1.7`
  297. or `12` to throttle to that level. Rethrottling that speeds up the query takes
  298. effect immediately but rethrotting that slows down the query will take effect
  299. on after completing the current batch. This prevents scroll timeouts.
  300. [float]
  301. [[picking-up-a-new-property]]
  302. === Pick up a new property
  303. Say you created an index without dynamic mapping, filled it with data, and then
  304. added a mapping value to pick up more fields from the data:
  305. [source,js]
  306. --------------------------------------------------
  307. PUT test
  308. {
  309. "mappings": {
  310. "test": {
  311. "dynamic": false, <1>
  312. "properties": {
  313. "text": {"type": "text"}
  314. }
  315. }
  316. }
  317. }
  318. POST test/test?refresh
  319. {
  320. "text": "words words",
  321. "flag": "bar"
  322. }
  323. POST test/test?refresh
  324. {
  325. "text": "words words",
  326. "flag": "foo"
  327. }
  328. PUT test/_mapping/test <2>
  329. {
  330. "properties": {
  331. "text": {"type": "text"},
  332. "flag": {"type": "text", "analyzer": "keyword"}
  333. }
  334. }
  335. --------------------------------------------------
  336. // CONSOLE
  337. <1> This means that new fields won't be indexed, just stored in `_source`.
  338. <2> This updates the mapping to add the new `flag` field. To pick up the new
  339. field you have to reindex all documents with it.
  340. Searching for the data won't find anything:
  341. [source,js]
  342. --------------------------------------------------
  343. POST test/_search?filter_path=hits.total
  344. {
  345. "query": {
  346. "match": {
  347. "flag": "foo"
  348. }
  349. }
  350. }
  351. --------------------------------------------------
  352. // CONSOLE
  353. // TEST[continued]
  354. [source,js]
  355. --------------------------------------------------
  356. {
  357. "hits" : {
  358. "total" : 0
  359. }
  360. }
  361. --------------------------------------------------
  362. // TESTRESPONSE
  363. But you can issue an `_update_by_query` request to pick up the new mapping:
  364. [source,js]
  365. --------------------------------------------------
  366. POST test/_update_by_query?refresh&conflicts=proceed
  367. POST test/_search?filter_path=hits.total
  368. {
  369. "query": {
  370. "match": {
  371. "flag": "foo"
  372. }
  373. }
  374. }
  375. --------------------------------------------------
  376. // CONSOLE
  377. // TEST[continued]
  378. [source,js]
  379. --------------------------------------------------
  380. {
  381. "hits" : {
  382. "total" : 1
  383. }
  384. }
  385. --------------------------------------------------
  386. // TESTRESPONSE
  387. You can do the exact same thing when adding a field to a multifield.