point-in-time-api.asciidoc 5.1 KB

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