update-by-query.asciidoc 23 KB

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