use-a-data-stream.asciidoc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. [[use-a-data-stream]]
  2. == Use a data stream
  3. After you <<set-up-a-data-stream,set up a data stream>>, you can do
  4. the following:
  5. * <<add-documents-to-a-data-stream>>
  6. * <<search-a-data-stream>>
  7. * <<manually-roll-over-a-data-stream>>
  8. * <<reindex-with-a-data-stream>>
  9. ////
  10. [source,console]
  11. ----
  12. PUT /_index_template/logs_data_stream
  13. {
  14. "index_patterns": [ "logs*" ],
  15. "data_stream": {
  16. "timestamp_field": "@timestamp"
  17. },
  18. "template": {
  19. "mappings": {
  20. "properties": {
  21. "@timestamp": {
  22. "type": "date"
  23. }
  24. }
  25. }
  26. }
  27. }
  28. PUT /_data_stream/logs
  29. ----
  30. ////
  31. [discrete]
  32. [[add-documents-to-a-data-stream]]
  33. === Add documents to a data stream
  34. You can add documents to a data stream using the following requests:
  35. * An <<docs-index_,index API>> request with an
  36. <<docs-index-api-op_type,`op_type`>> set to `create`. Specify the data
  37. stream's name in place of an index name.
  38. +
  39. --
  40. NOTE: The `op_type` parameter defaults to `create` when adding new documents.
  41. .*Example: Index API request*
  42. [%collapsible]
  43. ====
  44. The following <<docs-index_,index API>> adds a new document to the `logs` data
  45. stream.
  46. [source,console]
  47. ----
  48. POST /logs/_doc/
  49. {
  50. "@timestamp": "2020-12-07T11:06:07.000Z",
  51. "user": {
  52. "id": "8a4f500d"
  53. },
  54. "message": "Login successful"
  55. }
  56. ----
  57. // TEST[continued]
  58. ====
  59. --
  60. * A <<docs-bulk,bulk API>> request using the `create` action. Specify the data
  61. stream's name in place of an index name.
  62. +
  63. --
  64. NOTE: Data streams do not support other bulk actions, such as `index`.
  65. .*Example: Bulk API request*
  66. [%collapsible]
  67. ====
  68. The following <<docs-bulk,bulk API>> index request adds several new documents to
  69. the `logs` data stream. Note that only the `create` action is used.
  70. [source,console]
  71. ----
  72. PUT /logs/_bulk?refresh
  73. {"create":{"_index" : "logs"}}
  74. { "@timestamp": "2020-12-08T11:04:05.000Z", "user": { "id": "vlb44hny" }, "message": "Login attempt failed" }
  75. {"create":{"_index" : "logs"}}
  76. { "@timestamp": "2020-12-08T11:06:07.000Z", "user": { "id": "8a4f500d" }, "message": "Login successful" }
  77. {"create":{"_index" : "logs"}}
  78. { "@timestamp": "2020-12-09T11:07:08.000Z", "user": { "id": "l7gk7f82" }, "message": "Logout successful" }
  79. ----
  80. // TEST[continued]
  81. ====
  82. --
  83. [discrete]
  84. [[search-a-data-stream]]
  85. === Search a data stream
  86. The following search APIs support data streams:
  87. * <<search-search, Search>>
  88. * <<async-search, Async search>>
  89. * <<search-multi-search, Multi search>>
  90. * <<search-field-caps, Field capabilities>>
  91. ////
  92. * <<eql-search-api, EQL search>>
  93. ////
  94. .*Example*
  95. [%collapsible]
  96. ====
  97. The following <<search-search,search API>> request searches the `logs` data
  98. stream for documents with a timestamp between today and yesterday that also have
  99. `message` value of `login successful`.
  100. [source,console]
  101. ----
  102. GET /logs/_search
  103. {
  104. "query": {
  105. "bool": {
  106. "must": {
  107. "range": {
  108. "@timestamp": {
  109. "gte": "now-1d/d",
  110. "lt": "now/d"
  111. }
  112. }
  113. },
  114. "should": {
  115. "match": {
  116. "message": "login successful"
  117. }
  118. }
  119. }
  120. }
  121. }
  122. ----
  123. // TEST[continued]
  124. ====
  125. [discrete]
  126. [[manually-roll-over-a-data-stream]]
  127. === Manually roll over a data stream
  128. A rollover creates a new backing index for a data stream. This new backing index
  129. becomes the stream's <<data-stream-write-index,write index>> and increments
  130. the stream's <<data-streams-generation,generation>>.
  131. In most cases, we recommend using <<index-lifecycle-management,{ilm-init}>> to
  132. automate rollovers for data streams. This lets you automatically roll over the
  133. current write index when it meets specified criteria, such as a maximum age or
  134. size.
  135. However, you can also use the <<indices-rollover-index,rollover API>> to
  136. manually perform a rollover. This can be useful if you want to apply mapping or
  137. setting changes to the stream's write index after updating a data stream's
  138. template.
  139. .*Example*
  140. [%collapsible]
  141. ====
  142. The following <<indices-rollover-index,rollover API>> request submits a manual
  143. rollover request for the `logs` data stream.
  144. [source,console]
  145. ----
  146. POST /logs/_rollover/
  147. {
  148. "conditions": {
  149. "max_docs": "1"
  150. }
  151. }
  152. ----
  153. // TEST[continued]
  154. ====
  155. [discrete]
  156. [[reindex-with-a-data-stream]]
  157. === Reindex with a data stream
  158. You can use the <<docs-reindex,reindex API>> to copy documents to a data stream
  159. from an existing index, index alias, or data stream.
  160. A reindex copies documents from a _source_ to a _destination_. The source and
  161. destination can be any pre-existing index, index alias, or data stream. However,
  162. the source and destination must be different. You cannot reindex a data stream
  163. into itself.
  164. Because data streams are <<data-streams-append-only,append-only>>, a reindex
  165. request to a data stream destination must have an `op_type` of `create`. This
  166. means a reindex can only add new documents to a data stream. It cannot update
  167. existing documents in the data stream destination.
  168. A reindex can be used to:
  169. * Convert an existing index alias and collection of time-based indices into a
  170. data stream.
  171. * Apply a new or updated <<create-a-data-stream-template,composable template>>
  172. by reindexing an existing data stream into a new one. This applies mapping
  173. and setting changes in the template to each document and backing index of the
  174. data stream destination.
  175. TIP: If you only want to update the mappings or settings of a data stream's
  176. write index, we recommend you update the <<create-a-data-stream-template,data
  177. stream's template>> and perform a <<manually-roll-over-a-data-stream,rollover>>.
  178. .*Example*
  179. [%collapsible]
  180. ====
  181. The following reindex request copies documents from the `archive` index alias to
  182. the existing `logs` data stream. Because the destination is a data stream, the
  183. the request's `op_type` is `create`.
  184. ////
  185. [source,console]
  186. ----
  187. PUT /_bulk?refresh=wait_for
  188. {"create":{"_index" : "archive_1"}}
  189. { "@timestamp": "2020-12-08T11:04:05.000Z" }
  190. {"create":{"_index" : "archive_2"}}
  191. { "@timestamp": "2020-12-08T11:06:07.000Z" }
  192. {"create":{"_index" : "archive_2"}}
  193. { "@timestamp": "2020-12-09T11:07:08.000Z" }
  194. {"create":{"_index" : "archive_2"}}
  195. { "@timestamp": "2020-12-09T11:07:08.000Z" }
  196. POST /_aliases
  197. {
  198. "actions" : [
  199. { "add" : { "index" : "archive_1", "alias" : "archive" } },
  200. { "add" : { "index" : "archive_2", "alias" : "archive", "is_write_index" : true} }
  201. ]
  202. }
  203. ----
  204. // TEST[continued]
  205. ////
  206. [source,console]
  207. ----
  208. POST /_reindex
  209. {
  210. "source": {
  211. "index": "archive"
  212. },
  213. "dest": {
  214. "index": "logs",
  215. "op_type": "create"
  216. }
  217. }
  218. ----
  219. // TEST[continued]
  220. ====
  221. You can also reindex documents from a data stream to an index, index
  222. alias, or data stream.
  223. .*Example*
  224. [%collapsible]
  225. ====
  226. The following reindex request copies documents from the `logs` data stream
  227. to the existing `archive` index alias. Because the destination is not a data
  228. stream, the `op_type` does not need to be specified.
  229. [source,console]
  230. ----
  231. POST /_reindex
  232. {
  233. "source": {
  234. "index": "logs"
  235. },
  236. "dest": {
  237. "index": "archive"
  238. }
  239. }
  240. ----
  241. // TEST[continued]
  242. ====
  243. ////
  244. [source,console]
  245. ----
  246. DELETE /_data_stream/logs
  247. DELETE /_index_template/logs_data_stream
  248. ----
  249. // TEST[continued]
  250. ////