use-a-data-stream.asciidoc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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. * <<update-delete-docs-in-a-data-stream>>
  10. ////
  11. [source,console]
  12. ----
  13. PUT /_index_template/logs_data_stream
  14. {
  15. "index_patterns": [ "logs*" ],
  16. "data_stream": {
  17. "timestamp_field": "@timestamp"
  18. },
  19. "template": {
  20. "mappings": {
  21. "properties": {
  22. "@timestamp": {
  23. "type": "date"
  24. }
  25. }
  26. }
  27. }
  28. }
  29. PUT /_data_stream/logs
  30. ----
  31. ////
  32. [discrete]
  33. [[add-documents-to-a-data-stream]]
  34. === Add documents to a data stream
  35. You can add documents to a data stream using the following requests:
  36. * An <<docs-index_,index API>> request with an
  37. <<docs-index-api-op_type,`op_type`>> set to `create`. Specify the data
  38. stream's name in place of an index name.
  39. +
  40. --
  41. NOTE: The `op_type` parameter defaults to `create` when adding new documents.
  42. .*Example: Index API request*
  43. [%collapsible]
  44. ====
  45. The following <<docs-index_,index API>> adds a new document to the `logs` data
  46. stream.
  47. [source,console]
  48. ----
  49. POST /logs/_doc/
  50. {
  51. "@timestamp": "2020-12-07T11:06:07.000Z",
  52. "user": {
  53. "id": "8a4f500d"
  54. },
  55. "message": "Login successful"
  56. }
  57. ----
  58. // TEST[continued]
  59. ====
  60. --
  61. * A <<docs-bulk,bulk API>> request using the `create` action. Specify the data
  62. stream's name in place of an index name.
  63. +
  64. --
  65. NOTE: Data streams do not support other bulk actions, such as `index`.
  66. .*Example: Bulk API request*
  67. [%collapsible]
  68. ====
  69. The following <<docs-bulk,bulk API>> index request adds several new documents to
  70. the `logs` data stream. Note that only the `create` action is used.
  71. [source,console]
  72. ----
  73. PUT /logs/_bulk?refresh
  74. {"create":{"_index" : "logs"}}
  75. { "@timestamp": "2020-12-08T11:04:05.000Z", "user": { "id": "vlb44hny" }, "message": "Login attempt failed" }
  76. {"create":{"_index" : "logs"}}
  77. { "@timestamp": "2020-12-08T11:06:07.000Z", "user": { "id": "8a4f500d" }, "message": "Login successful" }
  78. {"create":{"_index" : "logs"}}
  79. { "@timestamp": "2020-12-09T11:07:08.000Z", "user": { "id": "l7gk7f82" }, "message": "Logout successful" }
  80. ----
  81. // TEST[continued]
  82. ====
  83. --
  84. [discrete]
  85. [[search-a-data-stream]]
  86. === Search a data stream
  87. The following search APIs support data streams:
  88. * <<search-search, Search>>
  89. * <<async-search, Async search>>
  90. * <<search-multi-search, Multi search>>
  91. * <<search-field-caps, Field capabilities>>
  92. ////
  93. * <<eql-search-api, EQL search>>
  94. ////
  95. .*Example*
  96. [%collapsible]
  97. ====
  98. The following <<search-search,search API>> request searches the `logs` data
  99. stream for documents with a timestamp between today and yesterday that also have
  100. `message` value of `login successful`.
  101. [source,console]
  102. ----
  103. GET /logs/_search
  104. {
  105. "query": {
  106. "bool": {
  107. "must": {
  108. "range": {
  109. "@timestamp": {
  110. "gte": "now-1d/d",
  111. "lt": "now/d"
  112. }
  113. }
  114. },
  115. "should": {
  116. "match": {
  117. "message": "login successful"
  118. }
  119. }
  120. }
  121. }
  122. }
  123. ----
  124. // TEST[continued]
  125. ====
  126. [discrete]
  127. [[manually-roll-over-a-data-stream]]
  128. === Manually roll over a data stream
  129. A rollover creates a new backing index for a data stream. This new backing index
  130. becomes the stream's <<data-stream-write-index,write index>> and increments
  131. the stream's <<data-streams-generation,generation>>.
  132. In most cases, we recommend using <<index-lifecycle-management,{ilm-init}>> to
  133. automate rollovers for data streams. This lets you automatically roll over the
  134. current write index when it meets specified criteria, such as a maximum age or
  135. size.
  136. However, you can also use the <<indices-rollover-index,rollover API>> to
  137. manually perform a rollover. This can be useful if you want to
  138. <<data-streams-change-mappings-and-settings,apply mapping or setting changes>>
  139. to the stream's write index after updating a data stream's template.
  140. .*Example*
  141. [%collapsible]
  142. ====
  143. The following <<indices-rollover-index,rollover API>> request submits a manual
  144. rollover request for the `logs` data stream.
  145. [source,console]
  146. ----
  147. POST /logs/_rollover/
  148. {
  149. "conditions": {
  150. "max_docs": "1"
  151. }
  152. }
  153. ----
  154. // TEST[continued]
  155. ====
  156. [discrete]
  157. [[reindex-with-a-data-stream]]
  158. === Reindex with a data stream
  159. You can use the <<docs-reindex,reindex API>> to copy documents to a data stream
  160. from an existing index, index alias, or data stream.
  161. A reindex copies documents from a _source_ to a _destination_. The source and
  162. destination can be any pre-existing index, index alias, or data stream. However,
  163. the source and destination must be different. You cannot reindex a data stream
  164. into itself.
  165. Because data streams are <<data-streams-append-only,append-only>>, a reindex
  166. request to a data stream destination must have an `op_type` of `create`. This
  167. means a reindex can only add new documents to a data stream. It cannot update
  168. existing documents in the data stream destination.
  169. A reindex can be used to:
  170. * Convert an existing index alias and collection of time-based indices into a
  171. data stream.
  172. * Apply a new or updated <<create-a-data-stream-template,composable template>>
  173. by reindexing an existing data stream into a new one. This applies mapping
  174. and setting changes in the template to each document and backing index of the
  175. data stream destination. See
  176. <<data-streams-use-reindex-to-change-mappings-settings>>.
  177. TIP: If you only want to update the mappings or settings of a data stream's
  178. write index, we recommend you update the <<create-a-data-stream-template,data
  179. stream's template>> and perform a <<manually-roll-over-a-data-stream,rollover>>.
  180. .*Example*
  181. [%collapsible]
  182. ====
  183. The following reindex request copies documents from the `archive` index alias to
  184. the existing `logs` data stream. Because the destination is a data stream, the
  185. request's `op_type` is `create`.
  186. ////
  187. [source,console]
  188. ----
  189. PUT /_bulk?refresh=wait_for
  190. {"create":{"_index" : "archive_1"}}
  191. { "@timestamp": "2020-12-08T11:04:05.000Z" }
  192. {"create":{"_index" : "archive_2"}}
  193. { "@timestamp": "2020-12-08T11:06:07.000Z" }
  194. {"create":{"_index" : "archive_2"}}
  195. { "@timestamp": "2020-12-09T11:07:08.000Z" }
  196. {"create":{"_index" : "archive_2"}}
  197. { "@timestamp": "2020-12-09T11:07:08.000Z" }
  198. POST /_aliases
  199. {
  200. "actions" : [
  201. { "add" : { "index" : "archive_1", "alias" : "archive" } },
  202. { "add" : { "index" : "archive_2", "alias" : "archive", "is_write_index" : true} }
  203. ]
  204. }
  205. ----
  206. // TEST[continued]
  207. ////
  208. [source,console]
  209. ----
  210. POST /_reindex
  211. {
  212. "source": {
  213. "index": "archive"
  214. },
  215. "dest": {
  216. "index": "logs",
  217. "op_type": "create"
  218. }
  219. }
  220. ----
  221. // TEST[continued]
  222. ====
  223. You can also reindex documents from a data stream to an index, index
  224. alias, or data stream.
  225. .*Example*
  226. [%collapsible]
  227. ====
  228. The following reindex request copies documents from the `logs` data stream
  229. to the existing `archive` index alias. Because the destination is not a data
  230. stream, the `op_type` does not need to be specified.
  231. [source,console]
  232. ----
  233. POST /_reindex
  234. {
  235. "source": {
  236. "index": "logs"
  237. },
  238. "dest": {
  239. "index": "archive"
  240. }
  241. }
  242. ----
  243. // TEST[continued]
  244. ====
  245. [discrete]
  246. [[update-delete-docs-in-a-data-stream]]
  247. === Update or delete documents in a data stream
  248. Data streams are designed to be <<data-streams-append-only,append-only>>. This
  249. means you cannot send update or deletion requests for existing documents to a
  250. data stream. However, you can send update or deletion requests to the backing
  251. index containing the document.
  252. To delete or update a document in a data stream, you first need to get:
  253. * The <<mapping-id-field,document ID>>
  254. * The name of the backing index that contains the document
  255. If you want to update a document, you must also get its current
  256. <<optimistic-concurrency-control,sequence number and primary term>>.
  257. You can use a <<search-a-data-stream,search request>> to retrieve this
  258. information.
  259. .*Example*
  260. [%collapsible]
  261. ====
  262. ////
  263. [source,console]
  264. ----
  265. PUT /logs/_create/bfspvnIBr7VVZlfp2lqX?refresh=wait_for
  266. {
  267. "@timestamp": "2020-12-07T11:06:07.000Z",
  268. "user": {
  269. "id": "yWIumJd7"
  270. },
  271. "message": "Login successful"
  272. }
  273. ----
  274. // TEST[continued]
  275. ////
  276. The following search request retrieves documents in the `logs` data stream with
  277. a `user.id` of `yWIumJd7`. By default, this search returns the document ID and
  278. backing index for any matching documents.
  279. The request includes a `"seq_no_primary_term": true` argument. This means the
  280. search also returns the sequence number and primary term for any matching
  281. documents.
  282. [source,console]
  283. ----
  284. GET /logs/_search
  285. {
  286. "seq_no_primary_term": true,
  287. "query": {
  288. "match": {
  289. "user.id": "yWIumJd7"
  290. }
  291. }
  292. }
  293. ----
  294. // TEST[continued]
  295. The API returns the following response. The `hits.hits` property contains
  296. information for any documents matching the search.
  297. [source,console-result]
  298. ----
  299. {
  300. "took": 20,
  301. "timed_out": false,
  302. "_shards": {
  303. "total": 2,
  304. "successful": 2,
  305. "skipped": 0,
  306. "failed": 0
  307. },
  308. "hits": {
  309. "total": {
  310. "value": 1,
  311. "relation": "eq"
  312. },
  313. "max_score": 0.2876821,
  314. "hits": [
  315. {
  316. "_index": ".ds-logs-000002", <1>
  317. "_id": "bfspvnIBr7VVZlfp2lqX", <2>
  318. "_seq_no": 4, <3>
  319. "_primary_term": 1, <4>
  320. "_score": 0.2876821,
  321. "_source": {
  322. "@timestamp": "2020-12-07T11:06:07.000Z",
  323. "user": {
  324. "id": "yWIumJd7"
  325. },
  326. "message": "Login successful"
  327. }
  328. }
  329. ]
  330. }
  331. }
  332. ----
  333. // TESTRESPONSE[s/"took": 20/"took": $body.took/]
  334. <1> Backing index containing the matching document
  335. <2> Document ID for the document
  336. <3> Current sequence number for the document
  337. <4> Primary term for the document
  338. ====
  339. You can use an <<docs-index_,index API>> request to update an individual
  340. document. To prevent an accidental overwrite, this request must include valid
  341. `if_seq_no` and `if_primary_term` arguments.
  342. .*Example*
  343. [%collapsible]
  344. ====
  345. The following index API request updates an existing document in the `logs` data
  346. stream. The request targets document ID `bfspvnIBr7VVZlfp2lqX` in the
  347. `.ds-logs-000002` backing index.
  348. The request also includes the current sequence number and primary term in the
  349. respective `if_seq_no` and `if_primary_term` query parameters. The request body
  350. contains a new JSON source for the document.
  351. [source,console]
  352. ----
  353. PUT /.ds-logs-000002/_doc/bfspvnIBr7VVZlfp2lqX?if_seq_no=4&if_primary_term=1
  354. {
  355. "@timestamp": "2020-12-07T11:06:07.000Z",
  356. "user": {
  357. "id": "8a4f500d"
  358. },
  359. "message": "Login successful"
  360. }
  361. ----
  362. // TEST[continued]
  363. ====
  364. You use the <<docs-delete,delete API>> to delete individual documents. Deletion
  365. requests do not require a sequence number or primary term.
  366. .*Example*
  367. [%collapsible]
  368. ====
  369. The following index API request deletes an existing document in the `logs` data
  370. stream. The request targets document ID `bfspvnIBr7VVZlfp2lqX` in the
  371. `.ds-logs-000002` backing index.
  372. [source,console]
  373. ----
  374. DELETE /.ds-logs-000002/_doc/bfspvnIBr7VVZlfp2lqX
  375. ----
  376. // TEST[continued]
  377. ====
  378. You can use the <<docs-bulk,bulk API>> to delete or update multiple documents in
  379. one request using `delete`, `index`, or `update` actions.
  380. If the action type is `index`, the action must include valid
  381. <<bulk-optimistic-concurrency-control,`if_seq_no` and `if_primary_term`>>
  382. arguments.
  383. .*Example*
  384. [%collapsible]
  385. ====
  386. ////
  387. [source,console]
  388. ----
  389. PUT /logs/_create/bfspvnIBr7VVZlfp2lqX?refresh=wait_for
  390. {
  391. "@timestamp": "2020-12-07T11:06:07.000Z",
  392. "user": {
  393. "id": "yWIumJd7"
  394. },
  395. "message": "Login successful"
  396. }
  397. ----
  398. // TEST[continued]
  399. ////
  400. The following bulk API request uses an `index` action to update an existing
  401. document in the `logs` data stream.
  402. The `index` action targets document ID `bfspvnIBr7VVZlfp2lqX` in the
  403. `.ds-logs-000002` backing index. The action also includes the current sequence
  404. number and primary term in the respective `if_seq_no` and `if_primary_term`
  405. parameters.
  406. [source,console]
  407. ----
  408. PUT /_bulk?refresh
  409. { "index": { "_index": ".ds-logs-000002", "_id": "bfspvnIBr7VVZlfp2lqX", "if_seq_no": 4, "if_primary_term": 1 } }
  410. { "@timestamp": "2020-12-07T11:06:07.000Z", "user": { "id": "8a4f500d" }, "message": "Login successful" }
  411. ----
  412. // TEST[continued]
  413. ====
  414. ////
  415. [source,console]
  416. ----
  417. DELETE /_data_stream/logs
  418. DELETE /_index_template/logs_data_stream
  419. ----
  420. // TEST[continued]
  421. ////