refresh.asciidoc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. [discrete]
  25. ==== Choosing which setting to use
  26. // tag::refresh-default[]
  27. Unless you have a good reason to wait for the change to become visible, always
  28. use `refresh=false` (the default setting). The simplest and fastest choice is to omit the `refresh` parameter from the URL.
  29. If you absolutely must have the changes made by a request visible synchronously
  30. with the request, you must choose between putting more load on
  31. Elasticsearch (`true`) and waiting longer for the response (`wait_for`).
  32. // end::refresh-default[]
  33. Here are a few points that should inform that decision:
  34. * The more changes being made to the index the more work `wait_for` saves
  35. compared to `true`. In the case that the index is only changed once every
  36. `index.refresh_interval` then it saves no work.
  37. * `true` creates less efficient indexes constructs (tiny segments) that must
  38. later be merged into more efficient index constructs (larger segments). Meaning
  39. that the cost of `true` is paid at index time to create the tiny segment, at
  40. search time to search the tiny segment, and at merge time to make the larger
  41. segments.
  42. * Never start multiple `refresh=wait_for` requests in a row. Instead batch them
  43. into a single bulk request with `refresh=wait_for` and Elasticsearch will start
  44. them all in parallel and return only when they have all finished.
  45. * If the refresh interval is set to `-1`, disabling the automatic refreshes,
  46. then requests with `refresh=wait_for` will wait indefinitely until some action
  47. causes a refresh. Conversely, setting `index.refresh_interval` to something
  48. shorter than the default like `200ms` will make `refresh=wait_for` come back
  49. faster, but it'll still generate inefficient segments.
  50. * `refresh=wait_for` only affects the request that it is on, but, by forcing a
  51. refresh immediately, `refresh=true` will affect other ongoing request. In
  52. general, if you have a running system you don't wish to disturb then
  53. `refresh=wait_for` is a smaller modification.
  54. [discrete]
  55. [[refresh_wait_for-force-refresh]]
  56. ==== `refresh=wait_for` Can Force a Refresh
  57. If a `refresh=wait_for` request comes in when there are already
  58. `index.max_refresh_listeners` (defaults to 1000) requests waiting for a refresh
  59. on that shard then that request will behave just as though it had `refresh` set
  60. to `true` instead: it will force a refresh. This keeps the promise that when a
  61. `refresh=wait_for` request returns that its changes are visible for search
  62. while preventing unchecked resource usage for blocked requests. If a request
  63. forced a refresh because it ran out of listener slots then its response will
  64. contain `"forced_refresh": true`.
  65. Bulk requests only take up one slot on each shard that they touch no matter how
  66. many times they modify the shard.
  67. [discrete]
  68. ==== Examples
  69. These will create a document and immediately refresh the index so it is visible:
  70. [source,console]
  71. --------------------------------------------------
  72. PUT /test/_doc/1?refresh
  73. {"test": "test"}
  74. PUT /test/_doc/2?refresh=true
  75. {"test": "test"}
  76. --------------------------------------------------
  77. These will create a document without doing anything to make it visible for
  78. search:
  79. [source,console]
  80. --------------------------------------------------
  81. PUT /test/_doc/3
  82. {"test": "test"}
  83. PUT /test/_doc/4?refresh=false
  84. {"test": "test"}
  85. --------------------------------------------------
  86. This will create a document and wait for it to become visible for search:
  87. [source,console]
  88. --------------------------------------------------
  89. PUT /test/_doc/4?refresh=wait_for
  90. {"test": "test"}
  91. --------------------------------------------------