update-by-query.asciidoc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  1. [[docs-update-by-query]]
  2. === Update By Query API
  3. ++++
  4. <titleabbrev>Update by query</titleabbrev>
  5. ++++
  6. Updates documents that match the specified query.
  7. If no query is specified, performs an update on every document in the data stream or index without
  8. modifying the source, which is useful for picking up mapping changes.
  9. [source,console]
  10. --------------------------------------------------
  11. POST my-index-000001/_update_by_query?conflicts=proceed
  12. --------------------------------------------------
  13. // TEST[setup:my_index_big]
  14. ////
  15. [source,console-result]
  16. --------------------------------------------------
  17. {
  18. "took" : 147,
  19. "timed_out": false,
  20. "updated": 120,
  21. "deleted": 0,
  22. "batches": 1,
  23. "version_conflicts": 0,
  24. "noops": 0,
  25. "retries": {
  26. "bulk": 0,
  27. "search": 0
  28. },
  29. "throttled_millis": 0,
  30. "requests_per_second": -1.0,
  31. "throttled_until_millis": 0,
  32. "total": 120,
  33. "failures" : [ ]
  34. }
  35. --------------------------------------------------
  36. // TESTRESPONSE[s/"took" : 147/"took" : "$body.took"/]
  37. ////
  38. [[docs-update-by-query-api-request]]
  39. ==== {api-request-title}
  40. `POST /<target>/_update_by_query`
  41. [[docs-update-by-query-api-prereqs]]
  42. ==== {api-prereq-title}
  43. * If the {es} {security-features} are enabled, you must have the following
  44. <<privileges-list-indices,index privileges>> for the target data stream, index,
  45. or index alias:
  46. ** `read`
  47. ** `index` or `write`
  48. [[docs-update-by-query-api-desc]]
  49. ==== {api-description-title}
  50. You can specify the query criteria in the request URI or the request body
  51. using the same syntax as the <<search-search,Search API>>.
  52. When you submit an update by query request, {es} gets a snapshot of the data stream or index
  53. when it begins processing the request and updates matching documents using
  54. `internal` versioning.
  55. When the versions match, the document is updated and the version number is incremented.
  56. If a document changes between the time that the snapshot is taken and
  57. the update operation is processed, it results in a version conflict and the operation fails.
  58. You can opt to count version conflicts instead of halting and returning by
  59. setting `conflicts` to `proceed`.
  60. NOTE: Documents with a version equal to 0 cannot be updated using update by
  61. query because `internal` versioning does not support 0 as a valid
  62. version number.
  63. While processing an update by query request, {es} performs multiple search
  64. requests sequentially to find all of the matching documents.
  65. A bulk update request is performed for each batch of matching documents.
  66. Any query or update failures cause the update by query request to fail and
  67. the failures are shown in the response.
  68. Any update requests that completed successfully still stick, they are not rolled back.
  69. ===== Refreshing shards
  70. Specifying the `refresh` parameter refreshes all shards once the request completes.
  71. This is different than the update API's `refresh` parameter, which causes just the shard
  72. that received the request to be refreshed. Unlike the update API, it does not support
  73. `wait_for`.
  74. [[docs-update-by-query-task-api]]
  75. ===== Running update by query asynchronously
  76. If the request contains `wait_for_completion=false`, {es}
  77. performs some preflight checks, launches the request, and returns a
  78. <<tasks,`task`>> you can use to cancel or get the status of the task.
  79. {es} creates a record of this task as a document at `.tasks/task/${taskId}`.
  80. When you are done with a task, you should delete the task document so
  81. {es} can reclaim the space.
  82. ===== Waiting for active shards
  83. `wait_for_active_shards` controls how many copies of a shard must be active
  84. before proceeding with the request. See <<index-wait-for-active-shards>>
  85. for details. `timeout` controls how long each write request waits for unavailable
  86. shards to become available. Both work exactly the way they work in the
  87. <<docs-bulk,Bulk API>>. Update by query uses scrolled searches, so you can also
  88. specify the `scroll` parameter to control how long it keeps the search context
  89. alive, for example `?scroll=10m`. The default is 5 minutes.
  90. ===== Throttling update requests
  91. To control the rate at which update by query issues batches of update operations,
  92. you can set `requests_per_second` to any positive decimal number. This pads each
  93. batch with a wait time to throttle the rate. Set `requests_per_second` to `-1`
  94. to disable throttling.
  95. Throttling uses a wait time between batches so that the internal scroll requests
  96. can be given a timeout that takes the request padding into account. The padding
  97. time is the difference between the batch size divided by the
  98. `requests_per_second` and the time spent writing. By default the batch size is
  99. `1000`, so if `requests_per_second` is set to `500`:
  100. [source,txt]
  101. --------------------------------------------------
  102. target_time = 1000 / 500 per second = 2 seconds
  103. wait_time = target_time - write_time = 2 seconds - .5 seconds = 1.5 seconds
  104. --------------------------------------------------
  105. Since the batch is issued as a single `_bulk` request, large batch sizes
  106. cause {es} to create many requests and wait before starting the next set.
  107. This is "bursty" instead of "smooth".
  108. [[docs-update-by-query-slice]]
  109. ===== Slicing
  110. Update by query supports <<slice-scroll, sliced scroll>> to parallelize the
  111. update process. This can improve efficiency and provide a
  112. convenient way to break the request down into smaller parts.
  113. Setting `slices` to `auto` chooses a reasonable number for most data streams and indices.
  114. If you're slicing manually or otherwise tuning automatic slicing, keep in mind
  115. that:
  116. * Query performance is most efficient when the number of `slices` is equal to
  117. the number of shards in the index or backing index. If that number is large (for example,
  118. 500), choose a lower number as too many `slices` hurts performance. Setting
  119. `slices` higher than the number of shards generally does not improve efficiency
  120. and adds overhead.
  121. * Update performance scales linearly across available resources with the
  122. number of slices.
  123. Whether query or update performance dominates the runtime depends on the
  124. documents being reindexed and cluster resources.
  125. [[docs-update-by-query-api-path-params]]
  126. ==== {api-path-parms-title}
  127. `<target>`::
  128. (Optional, string)
  129. Comma-separated list of data streams, indices, and index aliases to search.
  130. Wildcard (`*`) expressions are supported.
  131. +
  132. To search all data streams or indices in a cluster, omit this parameter or use
  133. `_all` or `*`.
  134. [[docs-update-by-query-api-query-params]]
  135. ==== {api-query-parms-title}
  136. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=allow-no-indices]
  137. +
  138. Defaults to `true`.
  139. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=analyzer]
  140. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=analyze_wildcard]
  141. `conflicts`::
  142. (Optional, string) What to do if update by query hits version conflicts:
  143. `abort` or `proceed`. Defaults to `abort`.
  144. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=default_operator]
  145. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=df]
  146. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=expand-wildcards]
  147. +
  148. Defaults to `open`.
  149. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=from]
  150. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=index-ignore-unavailable]
  151. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=lenient]
  152. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=max_docs]
  153. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=pipeline]
  154. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=preference]
  155. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=search-q]
  156. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=request_cache]
  157. `refresh`::
  158. (Optional, Boolean)
  159. If `true`, {es} refreshes affected shards to make the operation visible to
  160. search. Defaults to `false`.
  161. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=requests_per_second]
  162. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=routing]
  163. `scroll`::
  164. (Optional, <<time-units,time value>>)
  165. Period to retain the <<scroll-search-context,search context>> for scrolling. See
  166. <<scroll-search-results>>.
  167. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=scroll_size]
  168. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=search_type]
  169. `search_timeout`::
  170. (Optional, <<time-units, time units>>)
  171. Explicit timeout for each search request.
  172. Defaults to no timeout.
  173. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=slices]
  174. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=sort]
  175. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=source]
  176. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=source_excludes]
  177. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=source_includes]
  178. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=stats]
  179. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=terminate_after]
  180. `timeout`::
  181. +
  182. --
  183. (Optional, <<time-units, time units>>)
  184. Period each update request waits for the following operations:
  185. * Dynamic mapping updates
  186. * <<index-wait-for-active-shards,Waiting for active shards>>
  187. Defaults to `1m` (one minute). This guarantees {es} waits for at least the
  188. timeout before failing. The actual wait time could be longer, particularly when
  189. multiple waits occur.
  190. --
  191. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=version]
  192. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=wait_for_active_shards]
  193. [[docs-update-by-query-api-request-body]]
  194. ==== {api-request-body-title}
  195. `query`::
  196. (Optional, <<query-dsl,query object>>) Specifies the documents to update
  197. using the <<query-dsl,Query DSL>>.
  198. [[docs-update-by-query-api-response-body]]
  199. ==== Response body
  200. `took`::
  201. The number of milliseconds from start to end of the whole operation.
  202. `timed_out`::
  203. This flag is set to `true` if any of the requests executed during the
  204. update by query execution has timed out.
  205. `total`::
  206. The number of documents that were successfully processed.
  207. `updated`::
  208. The number of documents that were successfully updated.
  209. `deleted`::
  210. The number of documents that were successfully deleted.
  211. `batches`::
  212. The number of scroll responses pulled back by the update by query.
  213. `version_conflicts`::
  214. The number of version conflicts that the update by query hit.
  215. `noops`::
  216. The number of documents that were ignored because the script used for
  217. the update by query returned a `noop` value for `ctx.op`.
  218. `retries`::
  219. The number of retries attempted by update by query. `bulk` is the number of bulk
  220. actions retried, and `search` is the number of search actions retried.
  221. `throttled_millis`::
  222. Number of milliseconds the request slept to conform to `requests_per_second`.
  223. `requests_per_second`::
  224. The number of requests per second effectively executed during the update by query.
  225. `throttled_until_millis`::
  226. This field should always be equal to zero in an `_update_by_query` response. It only
  227. has meaning when using the <<docs-update-by-query-task-api, Task API>>, where it
  228. indicates the next time (in milliseconds since epoch) a throttled request will be
  229. executed again in order to conform to `requests_per_second`.
  230. `failures`::
  231. Array of failures if there were any unrecoverable errors during the process. If
  232. this is non-empty then the request aborted because of those failures.
  233. Update by query is implemented using batches. Any failure causes the entire
  234. process to abort, but all failures in the current batch are collected into the
  235. array. You can use the `conflicts` option to prevent reindex from aborting on
  236. version conflicts.
  237. [[docs-update-by-query-api-example]]
  238. ==== {api-examples-title}
  239. The simplest usage of `_update_by_query` just performs an update on every
  240. document in the data stream or index without changing the source. This is useful to
  241. <<picking-up-a-new-property,pick up a new property>> or some other online
  242. mapping change.
  243. To update selected documents, specify a query in the request body:
  244. [source,console]
  245. --------------------------------------------------
  246. POST my-index-000001/_update_by_query?conflicts=proceed
  247. {
  248. "query": { <1>
  249. "term": {
  250. "user.id": "kimchy"
  251. }
  252. }
  253. }
  254. --------------------------------------------------
  255. // TEST[setup:my_index]
  256. <1> The query must be passed as a value to the `query` key, in the same
  257. way as the <<search-search,Search API>>. You can also use the `q`
  258. parameter in the same way as the search API.
  259. Update documents in multiple data streams or indices:
  260. [source,console]
  261. --------------------------------------------------
  262. POST my-index-000001,my-index-000002/_update_by_query
  263. --------------------------------------------------
  264. // TEST[s/^/PUT my-index-000001\nPUT my-index-000002\n/]
  265. Limit the update by query operation to shards that a particular routing value:
  266. [source,console]
  267. --------------------------------------------------
  268. POST my-index-000001/_update_by_query?routing=1
  269. --------------------------------------------------
  270. // TEST[setup:my_index]
  271. By default update by query uses scroll batches of 1000.
  272. You can change the batch size with the `scroll_size` parameter:
  273. [source,console]
  274. --------------------------------------------------
  275. POST my-index-000001/_update_by_query?scroll_size=100
  276. --------------------------------------------------
  277. // TEST[setup:my_index]
  278. [[docs-update-by-query-api-source]]
  279. ===== Update the document source
  280. Update by query supports scripts to update the document source.
  281. For example, the following request increments the `count` field for all
  282. documents with a `user.id` of `kimchy` in `my-index-000001`:
  283. ////
  284. [source,console]
  285. ----
  286. PUT my-index-000001/_create/1
  287. {
  288. "user": {
  289. "id": "kimchy"
  290. },
  291. "count": 1
  292. }
  293. ----
  294. ////
  295. [source,console]
  296. --------------------------------------------------
  297. POST my-index-000001/_update_by_query
  298. {
  299. "script": {
  300. "source": "ctx._source.count++",
  301. "lang": "painless"
  302. },
  303. "query": {
  304. "term": {
  305. "user.id": "kimchy"
  306. }
  307. }
  308. }
  309. --------------------------------------------------
  310. // TEST[continued]
  311. Note that `conflicts=proceed` is not specified in this example. In this case, a
  312. version conflict should halt the process so you can handle the failure.
  313. As with the <<docs-update,Update API>>, you can set `ctx.op` to change the
  314. operation that is performed:
  315. [horizontal]
  316. `noop`::
  317. Set `ctx.op = "noop"` if your script decides that it doesn't have to make any changes.
  318. The update by query operation skips updating the document and increments the `noop` counter.
  319. `delete`::
  320. Set `ctx.op = "delete"` if your script decides that the document should be deleted.
  321. The update by query operation deletes the document and increments the `deleted` counter.
  322. Update by query only supports `update`, `noop`, and `delete`.
  323. Setting `ctx.op` to anything else is an error. Setting any other field in `ctx` is an error.
  324. This API only enables you to modify the source of matching documents, you cannot move them.
  325. [[docs-update-by-query-api-ingest-pipeline]]
  326. ===== Update documents using an ingest pipeline
  327. Update by query can use the <<ingest>> feature by specifying a `pipeline`:
  328. [source,console]
  329. --------------------------------------------------
  330. PUT _ingest/pipeline/set-foo
  331. {
  332. "description" : "sets foo",
  333. "processors" : [ {
  334. "set" : {
  335. "field": "foo",
  336. "value": "bar"
  337. }
  338. } ]
  339. }
  340. POST my-index-000001/_update_by_query?pipeline=set-foo
  341. --------------------------------------------------
  342. // TEST[setup:my_index]
  343. [discrete]
  344. [[docs-update-by-query-fetch-tasks]]
  345. ===== Get the status of update by query operations
  346. You can fetch the status of all running update by query requests with the
  347. <<tasks,Task API>>:
  348. [source,console]
  349. --------------------------------------------------
  350. GET _tasks?detailed=true&actions=*byquery
  351. --------------------------------------------------
  352. // TEST[skip:No tasks to retrieve]
  353. The responses looks like:
  354. [source,console-result]
  355. --------------------------------------------------
  356. {
  357. "nodes" : {
  358. "r1A2WoRbTwKZ516z6NEs5A" : {
  359. "name" : "r1A2WoR",
  360. "transport_address" : "127.0.0.1:9300",
  361. "host" : "127.0.0.1",
  362. "ip" : "127.0.0.1:9300",
  363. "attributes" : {
  364. "testattr" : "test",
  365. "portsfile" : "true"
  366. },
  367. "tasks" : {
  368. "r1A2WoRbTwKZ516z6NEs5A:36619" : {
  369. "node" : "r1A2WoRbTwKZ516z6NEs5A",
  370. "id" : 36619,
  371. "type" : "transport",
  372. "action" : "indices:data/write/update/byquery",
  373. "status" : { <1>
  374. "total" : 6154,
  375. "updated" : 3500,
  376. "created" : 0,
  377. "deleted" : 0,
  378. "batches" : 4,
  379. "version_conflicts" : 0,
  380. "noops" : 0,
  381. "retries": {
  382. "bulk": 0,
  383. "search": 0
  384. },
  385. "throttled_millis": 0
  386. },
  387. "description" : ""
  388. }
  389. }
  390. }
  391. }
  392. }
  393. --------------------------------------------------
  394. <1> This object contains the actual status. It is just like the response JSON
  395. with the important addition of the `total` field. `total` is the total number
  396. of operations that the reindex expects to perform. You can estimate the
  397. progress by adding the `updated`, `created`, and `deleted` fields. The request
  398. will finish when their sum is equal to the `total` field.
  399. With the task id you can look up the task directly. The following example
  400. retrieves information about task `r1A2WoRbTwKZ516z6NEs5A:36619`:
  401. [source,console]
  402. --------------------------------------------------
  403. GET /_tasks/r1A2WoRbTwKZ516z6NEs5A:36619
  404. --------------------------------------------------
  405. // TEST[catch:missing]
  406. The advantage of this API is that it integrates with `wait_for_completion=false`
  407. to transparently return the status of completed tasks. If the task is completed
  408. and `wait_for_completion=false` was set on it, then it'll come back with a
  409. `results` or an `error` field. The cost of this feature is the document that
  410. `wait_for_completion=false` creates at `.tasks/task/${taskId}`. It is up to
  411. you to delete that document.
  412. [discrete]
  413. [[docs-update-by-query-cancel-task-api]]
  414. ===== Cancel an update by query operation
  415. Any update by query can be cancelled using the <<tasks,Task Cancel API>>:
  416. [source,console]
  417. --------------------------------------------------
  418. POST _tasks/r1A2WoRbTwKZ516z6NEs5A:36619/_cancel
  419. --------------------------------------------------
  420. The task ID can be found using the <<tasks,tasks API>>.
  421. Cancellation should happen quickly but might take a few seconds. The task status
  422. API above will continue to list the update by query task until this task checks
  423. that it has been cancelled and terminates itself.
  424. [discrete]
  425. [[docs-update-by-query-rethrottle]]
  426. ===== Change throttling for a request
  427. The value of `requests_per_second` can be changed on a running update by query
  428. using the `_rethrottle` API:
  429. [source,console]
  430. --------------------------------------------------
  431. POST _update_by_query/r1A2WoRbTwKZ516z6NEs5A:36619/_rethrottle?requests_per_second=-1
  432. --------------------------------------------------
  433. The task ID can be found using the <<tasks, tasks API>>.
  434. Just like when setting it on the `_update_by_query` API, `requests_per_second`
  435. can be either `-1` to disable throttling or any decimal number
  436. like `1.7` or `12` to throttle to that level. Rethrottling that speeds up the
  437. query takes effect immediately, but rethrotting that slows down the query will
  438. take effect after completing the current batch. This prevents scroll
  439. timeouts.
  440. [discrete]
  441. [[docs-update-by-query-manual-slice]]
  442. ===== Slice manually
  443. Slice an update by query manually by providing a slice id and total number of
  444. slices to each request:
  445. [source,console]
  446. ----------------------------------------------------------------
  447. POST my-index-000001/_update_by_query
  448. {
  449. "slice": {
  450. "id": 0,
  451. "max": 2
  452. },
  453. "script": {
  454. "source": "ctx._source['extra'] = 'test'"
  455. }
  456. }
  457. POST my-index-000001/_update_by_query
  458. {
  459. "slice": {
  460. "id": 1,
  461. "max": 2
  462. },
  463. "script": {
  464. "source": "ctx._source['extra'] = 'test'"
  465. }
  466. }
  467. ----------------------------------------------------------------
  468. // TEST[setup:my_index_big]
  469. Which you can verify works with:
  470. [source,console]
  471. ----------------------------------------------------------------
  472. GET _refresh
  473. POST my-index-000001/_search?size=0&q=extra:test&filter_path=hits.total
  474. ----------------------------------------------------------------
  475. // TEST[continued]
  476. Which results in a sensible `total` like this one:
  477. [source,console-result]
  478. ----------------------------------------------------------------
  479. {
  480. "hits": {
  481. "total": {
  482. "value": 120,
  483. "relation": "eq"
  484. }
  485. }
  486. }
  487. ----------------------------------------------------------------
  488. [discrete]
  489. [[docs-update-by-query-automatic-slice]]
  490. ===== Use automatic slicing
  491. You can also let update by query automatically parallelize using
  492. <<slice-scroll>> to slice on `_id`. Use `slices` to specify the number of
  493. slices to use:
  494. [source,console]
  495. ----------------------------------------------------------------
  496. POST my-index-000001/_update_by_query?refresh&slices=5
  497. {
  498. "script": {
  499. "source": "ctx._source['extra'] = 'test'"
  500. }
  501. }
  502. ----------------------------------------------------------------
  503. // TEST[setup:my_index_big]
  504. Which you also can verify works with:
  505. [source,console]
  506. ----------------------------------------------------------------
  507. POST my-index-000001/_search?size=0&q=extra:test&filter_path=hits.total
  508. ----------------------------------------------------------------
  509. // TEST[continued]
  510. Which results in a sensible `total` like this one:
  511. [source,console-result]
  512. ----------------------------------------------------------------
  513. {
  514. "hits": {
  515. "total": {
  516. "value": 120,
  517. "relation": "eq"
  518. }
  519. }
  520. }
  521. ----------------------------------------------------------------
  522. Setting `slices` to `auto` will let Elasticsearch choose the number of slices
  523. to use. This setting will use one slice per shard, up to a certain limit. If
  524. there are multiple source data streams or indices, it will choose the number of slices based
  525. on the index or backing index with the smallest number of shards.
  526. Adding `slices` to `_update_by_query` just automates the manual process used in
  527. the section above, creating sub-requests which means it has some quirks:
  528. * You can see these requests in the
  529. <<docs-update-by-query-task-api,Tasks APIs>>. These sub-requests are "child"
  530. tasks of the task for the request with `slices`.
  531. * Fetching the status of the task for the request with `slices` only contains
  532. the status of completed slices.
  533. * These sub-requests are individually addressable for things like cancellation
  534. and rethrottling.
  535. * Rethrottling the request with `slices` will rethrottle the unfinished
  536. sub-request proportionally.
  537. * Canceling the request with `slices` will cancel each sub-request.
  538. * Due to the nature of `slices` each sub-request won't get a perfectly even
  539. portion of the documents. All documents will be addressed, but some slices may
  540. be larger than others. Expect larger slices to have a more even distribution.
  541. * Parameters like `requests_per_second` and `max_docs` on a request with
  542. `slices` are distributed proportionally to each sub-request. Combine that with
  543. the point above about distribution being uneven and you should conclude that
  544. using `max_docs` with `slices` might not result in exactly `max_docs` documents
  545. being updated.
  546. * Each sub-request gets a slightly different snapshot of the source data stream or index
  547. though these are all taken at approximately the same time.
  548. [discrete]
  549. [[picking-up-a-new-property]]
  550. ===== Pick up a new property
  551. Say you created an index without dynamic mapping, filled it with data, and then
  552. added a mapping value to pick up more fields from the data:
  553. [source,console]
  554. --------------------------------------------------
  555. PUT test
  556. {
  557. "mappings": {
  558. "dynamic": false, <1>
  559. "properties": {
  560. "text": {"type": "text"}
  561. }
  562. }
  563. }
  564. POST test/_doc?refresh
  565. {
  566. "text": "words words",
  567. "flag": "bar"
  568. }
  569. POST test/_doc?refresh
  570. {
  571. "text": "words words",
  572. "flag": "foo"
  573. }
  574. PUT test/_mapping <2>
  575. {
  576. "properties": {
  577. "text": {"type": "text"},
  578. "flag": {"type": "text", "analyzer": "keyword"}
  579. }
  580. }
  581. --------------------------------------------------
  582. <1> This means that new fields won't be indexed, just stored in `_source`.
  583. <2> This updates the mapping to add the new `flag` field. To pick up the new
  584. field you have to reindex all documents with it.
  585. Searching for the data won't find anything:
  586. [source,console]
  587. --------------------------------------------------
  588. POST test/_search?filter_path=hits.total
  589. {
  590. "query": {
  591. "match": {
  592. "flag": "foo"
  593. }
  594. }
  595. }
  596. --------------------------------------------------
  597. // TEST[continued]
  598. [source,console-result]
  599. --------------------------------------------------
  600. {
  601. "hits" : {
  602. "total": {
  603. "value": 0,
  604. "relation": "eq"
  605. }
  606. }
  607. }
  608. --------------------------------------------------
  609. But you can issue an `_update_by_query` request to pick up the new mapping:
  610. [source,console]
  611. --------------------------------------------------
  612. POST test/_update_by_query?refresh&conflicts=proceed
  613. POST test/_search?filter_path=hits.total
  614. {
  615. "query": {
  616. "match": {
  617. "flag": "foo"
  618. }
  619. }
  620. }
  621. --------------------------------------------------
  622. // TEST[continued]
  623. [source,console-result]
  624. --------------------------------------------------
  625. {
  626. "hits" : {
  627. "total": {
  628. "value": 1,
  629. "relation": "eq"
  630. }
  631. }
  632. }
  633. --------------------------------------------------
  634. You can do the exact same thing when adding a field to a multifield.