refresh.asciidoc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. [[docs-refresh]]
  2. == `?refresh`
  3. The <<docs-index_,Index>>, <<docs-update,Update>>, <<docs-delete,Delete>>, and
  4. <<docs-bulk,Bulk>> APIs support setting `refresh` to control when changes made
  5. by this request are made visible to search. These are the allowed values:
  6. Empty string or `true`::
  7. Refresh the relevant primary and replica shards (not the whole index)
  8. immediately after the operation occurs, so that the updated document appears
  9. in search results immediately. This should *ONLY* be done after careful thought
  10. and verification that it does not lead to poor performance, both from an
  11. indexing and a search standpoint.
  12. `wait_for`::
  13. Wait for the changes made by the request to be made visible by a refresh before
  14. replying. This doesn't force an immediate refresh, rather, it waits for a
  15. refresh to happen. Elasticsearch automatically refreshes shards that have changed
  16. every `index.refresh_interval` which defaults to one second. That setting is
  17. <<dynamic-index-settings,dynamic>>. Calling the <<indices-refresh>> API or
  18. setting `refresh` to `true` on any of the APIs that support it will also
  19. cause a refresh, in turn causing already running requests with `refresh=wait_for`
  20. to return.
  21. `false` (the default)::
  22. Take no refresh related actions. The changes made by this request will be made
  23. visible at some point after the request returns.
  24. [float]
  25. === Choosing which setting to use
  26. Unless you have a good reason to wait for the change to become visible always
  27. use `refresh=false`, or, because that is the default, just leave the `refresh`
  28. parameter out of the URL. That is the simplest and fastest choice.
  29. If you absolutely must have the changes made by a request visible synchronously
  30. with the request then you must pick between putting more load on
  31. Elasticsearch (`true`) and waiting longer for the response (`wait_for`). Here
  32. are a few points that should inform that decision:
  33. * The more changes being made to the index the more work `wait_for` saves
  34. compared to `true`. In the case that the index is only changed once every
  35. `index.refresh_interval` then it saves no work.
  36. * `true` creates less efficient indexes constructs (tiny segments) that must
  37. later be merged into more efficient index constructs (larger segments). Meaning
  38. that the cost of `true` is payed at index time to create the tiny segment, at
  39. search time to search the tiny segment, and at merge time to make the larger
  40. segments.
  41. * Never start multiple `refresh=wait_for` requests in a row. Instead batch them
  42. into a single bulk request with `refresh=wait_for` and Elasticsearch will start
  43. them all in parallel and return only when they have all finished.
  44. * If the refresh interval is set to `-1`, disabling the automatic refreshes,
  45. then requests with `refresh=wait_for` will wait indefinitely until some action
  46. causes a refresh. Conversely, setting `index.refresh_interval` to something
  47. shorter than the default like `200ms` will make `refresh=wait_for` come back
  48. faster, but it'll still generate inefficient segments.
  49. * `refresh=wait_for` only affects the request that it is on, but, by forcing a
  50. refresh immediately, `refresh=true` will affect other ongoing request. In
  51. general, if you have a running system you don't wish to disturb then
  52. `refresh=wait_for` is a smaller modification.
  53. [float]
  54. === `refresh=wait_for` Can Force a Refresh
  55. If a `refresh=wait_for` request comes in when there are already
  56. `index.max_refresh_listeners` (defaults to 1000) requests waiting for a refresh
  57. on that shard then that request will behave just as though it had `refresh` set
  58. to `true` instead: it will force a refresh. This keeps the promise that when a
  59. `refresh=wait_for` request returns that its changes are visible for search
  60. while preventing unchecked resource usage for blocked requests. If a request
  61. forced a refresh because it ran out of listener slots then its response will
  62. contain `"forced_refresh": true`.
  63. Bulk requests only take up one slot on each shard that they touch no matter how
  64. many times they modify the shard.
  65. [float]
  66. === Examples
  67. These will create a document and immediately refresh the index so it is visible:
  68. [source,js]
  69. --------------------------------------------------
  70. PUT /test/test/1?refresh
  71. {"test": "test"}
  72. PUT /test/test/2?refresh=true
  73. {"test": "test"}
  74. --------------------------------------------------
  75. // CONSOLE
  76. These will create a document without doing anything to make it visible for
  77. search:
  78. [source,js]
  79. --------------------------------------------------
  80. PUT /test/test/3
  81. {"test": "test"}
  82. PUT /test/test/4?refresh=false
  83. {"test": "test"}
  84. --------------------------------------------------
  85. // CONSOLE
  86. This will create a document and wait for it to become visible for search:
  87. [source,js]
  88. --------------------------------------------------
  89. PUT /test/test/4?refresh=wait_for
  90. {"test": "test"}
  91. --------------------------------------------------
  92. // CONSOLE