point-in-time-api.asciidoc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. [[point-in-time-api]]
  2. === Point in time API
  3. ++++
  4. <titleabbrev>Point in time</titleabbrev>
  5. ++++
  6. A search request by default executes against the most recent visible data of
  7. the target indices, which is called point in time. Elasticsearch pit (point in time)
  8. is a lightweight view into the state of the data as it existed when initiated.
  9. In some cases, it's preferred to perform multiple search requests using
  10. the same point in time. For example, if <<indices-refresh,refreshes>> happen between
  11. search_after requests, then the results of those requests might not be consistent as
  12. changes happening between searches are only visible to the more recent point in time.
  13. [[point-in-time-api-prereqs]]
  14. ==== {api-prereq-title}
  15. * If the {es} {security-features} are enabled, you must have the `read`
  16. <<privileges-list-indices,index privilege>> for the target data stream, index,
  17. or alias.
  18. +
  19. To search a <<point-in-time-api,point in time (PIT)>> for an alias, you
  20. must have the `read` index privilege for the alias's data streams or indices.
  21. [[point-in-time-api-example]]
  22. ==== {api-examples-title}
  23. A point in time must be opened explicitly before being used in search requests. The
  24. keep_alive parameter tells Elasticsearch how long it should keep a point in time alive,
  25. e.g. `?keep_alive=5m`.
  26. [source,console]
  27. --------------------------------------------------
  28. POST /my-index-000001/_pit?keep_alive=1m
  29. --------------------------------------------------
  30. // TEST[setup:my_index]
  31. The result from the above request includes a `id`, which should
  32. be passed to the `id` of the `pit` parameter of a search request.
  33. [source,console]
  34. --------------------------------------------------
  35. POST /_search <1>
  36. {
  37. "size": 100,
  38. "query": {
  39. "match" : {
  40. "title" : "elasticsearch"
  41. }
  42. },
  43. "pit": {
  44. "id": "46ToAwMDaWR5BXV1aWQyKwZub2RlXzMAAAAAAAAAACoBYwADaWR4BXV1aWQxAgZub2RlXzEAAAAAAAAAAAEBYQADaWR5BXV1aWQyKgZub2RlXzIAAAAAAAAAAAwBYgACBXV1aWQyAAAFdXVpZDEAAQltYXRjaF9hbGw_gAAAAA==", <2>
  45. "keep_alive": "1m" <3>
  46. }
  47. }
  48. --------------------------------------------------
  49. // TEST[catch:missing]
  50. <1> A search request with the `pit` parameter must not specify `index`, `routing`,
  51. and {ref}/search-request-body.html#request-body-search-preference[`preference`]
  52. as these parameters are copied from the point in time.
  53. <2> The `id` parameter tells Elasticsearch to execute the request using contexts
  54. from this point in time.
  55. <3> The `keep_alive` parameter tells Elasticsearch how long it should extend
  56. the time to live of the point in time.
  57. IMPORTANT: The open point in time request and each subsequent search request can
  58. return different `id`; thus always use the most recently received `id` for the
  59. next search request.
  60. [[point-in-time-keep-alive]]
  61. ==== Keeping point in time alive
  62. The `keep_alive` parameter, which is passed to a open point in time request and
  63. search request, extends the time to live of the corresponding point in time.
  64. The value (e.g. `1m`, see <<time-units>>) does not need to be long enough to
  65. process all data -- it just needs to be long enough for the next request.
  66. Normally, the background merge process optimizes the index by merging together
  67. smaller segments to create new, bigger segments. Once the smaller segments are
  68. no longer needed they are deleted. However, open point-in-times prevent the
  69. old segments from being deleted since they are still in use.
  70. TIP: Keeping older segments alive means that more disk space and file handles
  71. are needed. Ensure that you have configured your nodes to have ample free file
  72. handles. See <<file-descriptors>>.
  73. Additionally, if a segment contains deleted or updated documents then the
  74. point in time must keep track of whether each document in the segment was live at
  75. the time of the initial search request. Ensure that your nodes have sufficient heap
  76. space if you have many open point-in-times on an index that is subject to ongoing
  77. deletes or updates.
  78. You can check how many point-in-times (i.e, search contexts) are open with the
  79. <<cluster-nodes-stats,nodes stats API>>:
  80. [source,console]
  81. ---------------------------------------
  82. GET /_nodes/stats/indices/search
  83. ---------------------------------------
  84. [[close-point-in-time-api]]
  85. ==== Close point in time API
  86. Point-in-time is automatically closed when its `keep_alive` has
  87. been elapsed. However keeping point-in-times has a cost, as discussed in the
  88. <<point-in-time-keep-alive,previous section>>. Point-in-times should be closed
  89. as soon as they are no longer used in search requests.
  90. [source,console]
  91. ---------------------------------------
  92. DELETE /_pit
  93. {
  94. "id" : "46ToAwMDaWR5BXV1aWQyKwZub2RlXzMAAAAAAAAAACoBYwADaWR4BXV1aWQxAgZub2RlXzEAAAAAAAAAAAEBYQADaWR5BXV1aWQyKgZub2RlXzIAAAAAAAAAAAwBYgACBXV1aWQyAAAFdXVpZDEAAQltYXRjaF9hbGw_gAAAAA=="
  95. }
  96. ---------------------------------------
  97. // TEST[catch:missing]
  98. The API returns the following response:
  99. [source,console-result]
  100. --------------------------------------------------
  101. {
  102. "succeeded": true, <1>
  103. "num_freed": 3 <2>
  104. }
  105. --------------------------------------------------
  106. // TESTRESPONSE[s/"succeeded": true/"succeeded": $body.succeeded/]
  107. // TESTRESPONSE[s/"num_freed": 3/"num_freed": $body.num_freed/]
  108. <1> If true, all search contexts associated with the point-in-time id are successfully closed
  109. <2> The number of search contexts have been successfully closed