use-a-data-stream.asciidoc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  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. * <<open-closed-backing-indices>>
  9. * <<reindex-with-a-data-stream>>
  10. * <<update-delete-docs-in-a-data-stream>>
  11. * <<update-delete-docs-in-a-backing-index>>
  12. ////
  13. [source,console]
  14. ----
  15. PUT /_index_template/logs_data_stream
  16. {
  17. "index_patterns": [ "logs*" ],
  18. "data_stream": {
  19. "timestamp_field": "@timestamp"
  20. },
  21. "template": {
  22. "mappings": {
  23. "properties": {
  24. "@timestamp": {
  25. "type": "date"
  26. }
  27. }
  28. }
  29. }
  30. }
  31. PUT /_data_stream/logs
  32. ----
  33. ////
  34. [discrete]
  35. [[add-documents-to-a-data-stream]]
  36. === Add documents to a data stream
  37. You can add documents to a data stream using the following requests:
  38. * An <<docs-index_,index API>> request with an
  39. <<docs-index-api-op_type,`op_type`>> set to `create`. Specify the data
  40. stream's name in place of an index name.
  41. +
  42. --
  43. NOTE: The `op_type` parameter defaults to `create` when adding new documents.
  44. .*Example: Index API request*
  45. [%collapsible]
  46. ====
  47. The following index API request adds a new document to the `logs` data
  48. stream.
  49. [source,console]
  50. ----
  51. POST /logs/_doc/
  52. {
  53. "@timestamp": "2020-12-07T11:06:07.000Z",
  54. "user": {
  55. "id": "8a4f500d"
  56. },
  57. "message": "Login successful"
  58. }
  59. ----
  60. // TEST[continued]
  61. ====
  62. IMPORTANT: You cannot add new documents to a data stream using the index API's
  63. `PUT /<target>/_doc/<_id>` request format. Use the `PUT /<target>/_create/<_id>`
  64. format instead.
  65. --
  66. * A <<docs-bulk,bulk API>> request using the `create` action. Specify the data
  67. stream's name in place of an index name.
  68. +
  69. --
  70. NOTE: Data streams do not support other bulk actions, such as `index`.
  71. .*Example: Bulk API request*
  72. [%collapsible]
  73. ====
  74. The following bulk API request adds several new documents to
  75. the `logs` data stream. Note that only the `create` action is used.
  76. [source,console]
  77. ----
  78. PUT /logs/_bulk?refresh
  79. {"create":{"_index" : "logs"}}
  80. { "@timestamp": "2020-12-08T11:04:05.000Z", "user": { "id": "vlb44hny" }, "message": "Login attempt failed" }
  81. {"create":{"_index" : "logs"}}
  82. { "@timestamp": "2020-12-08T11:06:07.000Z", "user": { "id": "8a4f500d" }, "message": "Login successful" }
  83. {"create":{"_index" : "logs"}}
  84. { "@timestamp": "2020-12-09T11:07:08.000Z", "user": { "id": "l7gk7f82" }, "message": "Logout successful" }
  85. ----
  86. // TEST[continued]
  87. ====
  88. --
  89. [discrete]
  90. [[search-a-data-stream]]
  91. === Search a data stream
  92. The following search APIs support data streams:
  93. * <<search-search, Search>>
  94. * <<async-search, Async search>>
  95. * <<search-multi-search, Multi search>>
  96. * <<search-field-caps, Field capabilities>>
  97. ////
  98. * <<eql-search-api, EQL search>>
  99. ////
  100. .*Example*
  101. [%collapsible]
  102. ====
  103. The following <<search-search,search API>> request searches the `logs` data
  104. stream for documents with a timestamp between today and yesterday that also have
  105. `message` value of `login successful`.
  106. [source,console]
  107. ----
  108. GET /logs/_search
  109. {
  110. "query": {
  111. "bool": {
  112. "must": {
  113. "range": {
  114. "@timestamp": {
  115. "gte": "now-1d/d",
  116. "lt": "now/d"
  117. }
  118. }
  119. },
  120. "should": {
  121. "match": {
  122. "message": "login successful"
  123. }
  124. }
  125. }
  126. }
  127. }
  128. ----
  129. // TEST[continued]
  130. ====
  131. You can use a comma-separated list or wildcard (`*`) expression to search
  132. multiple data streams, indices, and index aliases in the same request.
  133. .*Example*
  134. [%collapsible]
  135. ====
  136. ////
  137. [source,console]
  138. ----
  139. PUT /_data_stream/logs_alt
  140. ----
  141. // TEST[continued]
  142. ////
  143. The following request searches the `logs` and `logs_alt` data streams, which are
  144. specified as a comma-separated list in the request path.
  145. [source,console]
  146. ----
  147. GET /logs,logs_alt/_search
  148. {
  149. "query": {
  150. "match": {
  151. "user.id": "8a4f500d"
  152. }
  153. }
  154. }
  155. ----
  156. // TEST[continued]
  157. The following request uses the `logs*` wildcard expression to search any data
  158. stream, index, or index alias beginning with `logs`.
  159. [source,console]
  160. ----
  161. GET /logs*/_search
  162. {
  163. "query": {
  164. "match": {
  165. "user.id": "vlb44hny"
  166. }
  167. }
  168. }
  169. ----
  170. // TEST[continued]
  171. The following search request omits a target in the request path. The request
  172. searches all data streams and indices in the cluster.
  173. [source,console]
  174. ----
  175. GET /_search
  176. {
  177. "query": {
  178. "match": {
  179. "user.id": "l7gk7f82"
  180. }
  181. }
  182. }
  183. ----
  184. // TEST[continued]
  185. ====
  186. [discrete]
  187. [[manually-roll-over-a-data-stream]]
  188. === Manually roll over a data stream
  189. A rollover creates a new backing index for a data stream. This new backing index
  190. becomes the stream's <<data-stream-write-index,write index>> and increments
  191. the stream's <<data-streams-generation,generation>>.
  192. In most cases, we recommend using <<index-lifecycle-management,{ilm-init}>> to
  193. automate rollovers for data streams. This lets you automatically roll over the
  194. current write index when it meets specified criteria, such as a maximum age or
  195. size.
  196. However, you can also use the <<indices-rollover-index,rollover API>> to
  197. manually perform a rollover. This can be useful if you want to
  198. <<data-streams-change-mappings-and-settings,apply mapping or setting changes>>
  199. to the stream's write index after updating a data stream's template.
  200. .*Example*
  201. [%collapsible]
  202. ====
  203. The following <<indices-rollover-index,rollover API>> request submits a manual
  204. rollover request for the `logs` data stream.
  205. [source,console]
  206. ----
  207. POST /logs/_rollover/
  208. ----
  209. // TEST[continued]
  210. ====
  211. [discrete]
  212. [[open-closed-backing-indices]]
  213. === Open closed backing indices
  214. You may <<indices-close,close>> one or more of a data stream's backing indices
  215. as part of its {ilm-init} lifecycle or another workflow. A closed backing index
  216. cannot be searched, even for searches targeting its data stream. You also can't
  217. <<update-delete-docs-in-a-data-stream,update or delete documents>> in a closed
  218. index.
  219. You can re-open individual backing indices by sending an
  220. <<indices-open-close,open request>> directly to the index.
  221. You also can conveniently re-open all closed backing indices for a data stream
  222. by sending an open request directly to the stream.
  223. .*Example*
  224. [%collapsible]
  225. ====
  226. ////
  227. [source,console]
  228. ----
  229. POST /logs/_rollover/
  230. POST /.ds-logs-000001,.ds-logs-000002/_close/
  231. ----
  232. // TEST[continued]
  233. ////
  234. The following <<cat-indices,cat indices>> API request retrieves the status for
  235. the `logs` data stream's backing indices.
  236. [source,console]
  237. ----
  238. GET /_cat/indices/logs?v&s=index&h=index,status
  239. ----
  240. // TEST[continued]
  241. The API returns the following response. The response indicates the `logs` data
  242. stream contains two closed backing indices: `.ds-logs-000001` and
  243. `.ds-logs-000002`.
  244. [source,txt]
  245. ----
  246. index status
  247. .ds-logs-000001 close
  248. .ds-logs-000002 close
  249. .ds-logs-000003 open
  250. ----
  251. // TESTRESPONSE[non_json]
  252. The following <<indices-open-close,open API>> request re-opens any closed
  253. backing indices for the `logs` data stream, including `.ds-logs-000001` and
  254. `.ds-logs-000002`.
  255. [source,console]
  256. ----
  257. POST /logs/_open/
  258. ----
  259. // TEST[continued]
  260. You can resubmit the original cat indices API request to verify the
  261. `.ds-logs-000001` and `.ds-logs-000002` backing indices were re-opened.
  262. [source,console]
  263. ----
  264. GET /_cat/indices/logs?v&s=index&h=index,status
  265. ----
  266. // TEST[continued]
  267. The API returns the following response.
  268. [source,txt]
  269. ----
  270. index status
  271. .ds-logs-000001 open
  272. .ds-logs-000002 open
  273. .ds-logs-000003 open
  274. ----
  275. // TESTRESPONSE[non_json]
  276. ====
  277. [discrete]
  278. [[reindex-with-a-data-stream]]
  279. === Reindex with a data stream
  280. You can use the <<docs-reindex,reindex API>> to copy documents to a data stream
  281. from an existing index, index alias, or data stream.
  282. A reindex copies documents from a _source_ to a _destination_. The source and
  283. destination can be any pre-existing index, index alias, or data stream. However,
  284. the source and destination must be different. You cannot reindex a data stream
  285. into itself.
  286. Because data streams are <<data-streams-append-only,append-only>>, a reindex
  287. request to a data stream destination must have an `op_type` of `create`. This
  288. means a reindex can only add new documents to a data stream. It cannot update
  289. existing documents in the data stream destination.
  290. A reindex can be used to:
  291. * Convert an existing index alias and collection of time-based indices into a
  292. data stream.
  293. * Apply a new or updated <<create-a-data-stream-template,index template>>
  294. by reindexing an existing data stream into a new one. This applies mapping
  295. and setting changes in the template to each document and backing index of the
  296. data stream destination. See
  297. <<data-streams-use-reindex-to-change-mappings-settings>>.
  298. TIP: If you only want to update the mappings or settings of a data stream's
  299. write index, we recommend you update the <<create-a-data-stream-template,data
  300. stream's template>> and perform a <<manually-roll-over-a-data-stream,rollover>>.
  301. .*Example*
  302. [%collapsible]
  303. ====
  304. The following reindex request copies documents from the `archive` index alias to
  305. the existing `logs` data stream. Because the destination is a data stream, the
  306. request's `op_type` is `create`.
  307. ////
  308. [source,console]
  309. ----
  310. PUT /_bulk?refresh=wait_for
  311. {"create":{"_index" : "archive_1"}}
  312. { "@timestamp": "2020-12-08T11:04:05.000Z" }
  313. {"create":{"_index" : "archive_2"}}
  314. { "@timestamp": "2020-12-08T11:06:07.000Z" }
  315. {"create":{"_index" : "archive_2"}}
  316. { "@timestamp": "2020-12-09T11:07:08.000Z" }
  317. {"create":{"_index" : "archive_2"}}
  318. { "@timestamp": "2020-12-09T11:07:08.000Z" }
  319. POST /_aliases
  320. {
  321. "actions" : [
  322. { "add" : { "index" : "archive_1", "alias" : "archive" } },
  323. { "add" : { "index" : "archive_2", "alias" : "archive", "is_write_index" : true} }
  324. ]
  325. }
  326. ----
  327. // TEST[continued]
  328. ////
  329. [source,console]
  330. ----
  331. POST /_reindex
  332. {
  333. "source": {
  334. "index": "archive"
  335. },
  336. "dest": {
  337. "index": "logs",
  338. "op_type": "create"
  339. }
  340. }
  341. ----
  342. // TEST[continued]
  343. ====
  344. You can also reindex documents from a data stream to an index, index
  345. alias, or data stream.
  346. .*Example*
  347. [%collapsible]
  348. ====
  349. The following reindex request copies documents from the `logs` data stream
  350. to the existing `archive` index alias. Because the destination is not a data
  351. stream, the `op_type` does not need to be specified.
  352. [source,console]
  353. ----
  354. POST /_reindex
  355. {
  356. "source": {
  357. "index": "logs"
  358. },
  359. "dest": {
  360. "index": "archive"
  361. }
  362. }
  363. ----
  364. // TEST[continued]
  365. ====
  366. [discrete]
  367. [[update-delete-docs-in-a-data-stream]]
  368. === Update or delete documents in a data stream
  369. You can update or delete documents in a data stream using the following
  370. requests:
  371. * An <<docs-update-by-query,update by query API>> request
  372. +
  373. .*Example*
  374. [%collapsible]
  375. ====
  376. The following update by query API request updates documents in the `logs` data
  377. stream with a `user.id` of `i96BP1mA`. The request uses a
  378. <<modules-scripting-using,script>> to assign matching documents a new `user.id`
  379. value of `XgdX0NoX`.
  380. ////
  381. [source,console]
  382. ----
  383. PUT /logs/_create/2?refresh=wait_for
  384. {
  385. "@timestamp": "2020-12-07T11:06:07.000Z",
  386. "user": {
  387. "id": "i96BP1mA"
  388. }
  389. }
  390. ----
  391. // TEST[continued]
  392. ////
  393. [source,console]
  394. ----
  395. POST /logs/_update_by_query
  396. {
  397. "query": {
  398. "match": {
  399. "user.id": "i96BP1mA"
  400. }
  401. },
  402. "script": {
  403. "source": "ctx._source.user.id = params.new_id",
  404. "params": {
  405. "new_id": "XgdX0NoX"
  406. }
  407. }
  408. }
  409. ----
  410. // TEST[continued]
  411. ====
  412. * A <<docs-delete-by-query,delete by query API>> request
  413. +
  414. .*Example*
  415. [%collapsible]
  416. ====
  417. The following delete by query API request deletes documents in the `logs` data
  418. stream with a `user.id` of `zVZMamUM`.
  419. ////
  420. [source,console]
  421. ----
  422. PUT /logs/_create/1?refresh=wait_for
  423. {
  424. "@timestamp": "2020-12-07T11:06:07.000Z",
  425. "user": {
  426. "id": "zVZMamUM"
  427. }
  428. }
  429. ----
  430. // TEST[continued]
  431. ////
  432. [source,console]
  433. ----
  434. POST /logs/_delete_by_query
  435. {
  436. "query": {
  437. "match": {
  438. "user.id": "zVZMamUM"
  439. }
  440. }
  441. }
  442. ----
  443. // TEST[continued]
  444. ====
  445. [discrete]
  446. [[update-delete-docs-in-a-backing-index]]
  447. === Update or delete documents in a backing index
  448. Alternatively, you can update or delete documents in a data stream by sending
  449. the update or deletion request to the backing index containing the document. To
  450. do this, you first need to get:
  451. * The <<mapping-id-field,document ID>>
  452. * The name of the backing index that contains the document
  453. If you want to update a document, you must also get its current
  454. <<optimistic-concurrency-control,sequence number and primary term>>.
  455. You can use a <<search-a-data-stream,search request>> to retrieve this
  456. information.
  457. .*Example*
  458. [%collapsible]
  459. ====
  460. ////
  461. [source,console]
  462. ----
  463. PUT /logs/_create/bfspvnIBr7VVZlfp2lqX?refresh=wait_for
  464. {
  465. "@timestamp": "2020-12-07T11:06:07.000Z",
  466. "user": {
  467. "id": "yWIumJd7"
  468. },
  469. "message": "Login successful"
  470. }
  471. ----
  472. // TEST[continued]
  473. ////
  474. The following search request retrieves documents in the `logs` data stream with
  475. a `user.id` of `yWIumJd7`. By default, this search returns the document ID and
  476. backing index for any matching documents.
  477. The request includes a `"seq_no_primary_term": true` argument. This means the
  478. search also returns the sequence number and primary term for any matching
  479. documents.
  480. [source,console]
  481. ----
  482. GET /logs/_search
  483. {
  484. "seq_no_primary_term": true,
  485. "query": {
  486. "match": {
  487. "user.id": "yWIumJd7"
  488. }
  489. }
  490. }
  491. ----
  492. // TEST[continued]
  493. The API returns the following response. The `hits.hits` property contains
  494. information for any documents matching the search.
  495. [source,console-result]
  496. ----
  497. {
  498. "took": 20,
  499. "timed_out": false,
  500. "_shards": {
  501. "total": 3,
  502. "successful": 3,
  503. "skipped": 0,
  504. "failed": 0
  505. },
  506. "hits": {
  507. "total": {
  508. "value": 1,
  509. "relation": "eq"
  510. },
  511. "max_score": 0.2876821,
  512. "hits": [
  513. {
  514. "_index": ".ds-logs-000003", <1>
  515. "_id": "bfspvnIBr7VVZlfp2lqX", <2>
  516. "_seq_no": 8, <3>
  517. "_primary_term": 1, <4>
  518. "_score": 0.2876821,
  519. "_source": {
  520. "@timestamp": "2020-12-07T11:06:07.000Z",
  521. "user": {
  522. "id": "yWIumJd7"
  523. },
  524. "message": "Login successful"
  525. }
  526. }
  527. ]
  528. }
  529. }
  530. ----
  531. // TESTRESPONSE[s/"took": 20/"took": $body.took/]
  532. // TESTRESPONSE[s/"max_score": 0.2876821/"max_score": $body.hits.max_score/]
  533. // TESTRESPONSE[s/"_score": 0.2876821/"_score": $body.hits.hits.0._score/]
  534. <1> Backing index containing the matching document
  535. <2> Document ID for the document
  536. <3> Current sequence number for the document
  537. <4> Primary term for the document
  538. ====
  539. You can use an <<docs-index_,index API>> request to update an individual
  540. document. To prevent an accidental overwrite, this request must include valid
  541. `if_seq_no` and `if_primary_term` arguments.
  542. .*Example*
  543. [%collapsible]
  544. ====
  545. The following index API request updates an existing document in the `logs` data
  546. stream. The request targets document ID `bfspvnIBr7VVZlfp2lqX` in the
  547. `.ds-logs-000003` backing index.
  548. The request also includes the current sequence number and primary term in the
  549. respective `if_seq_no` and `if_primary_term` query parameters. The request body
  550. contains a new JSON source for the document.
  551. [source,console]
  552. ----
  553. PUT /.ds-logs-000003/_doc/bfspvnIBr7VVZlfp2lqX?if_seq_no=8&if_primary_term=1
  554. {
  555. "@timestamp": "2020-12-07T11:06:07.000Z",
  556. "user": {
  557. "id": "8a4f500d"
  558. },
  559. "message": "Login successful"
  560. }
  561. ----
  562. // TEST[continued]
  563. ====
  564. You use the <<docs-delete,delete API>> to delete individual documents. Deletion
  565. requests do not require a sequence number or primary term.
  566. .*Example*
  567. [%collapsible]
  568. ====
  569. The following index API request deletes an existing document in the `logs` data
  570. stream. The request targets document ID `bfspvnIBr7VVZlfp2lqX` in the
  571. `.ds-logs-000003` backing index.
  572. [source,console]
  573. ----
  574. DELETE /.ds-logs-000003/_doc/bfspvnIBr7VVZlfp2lqX
  575. ----
  576. // TEST[continued]
  577. ====
  578. You can use the <<docs-bulk,bulk API>> to delete or update multiple documents in
  579. one request using `delete`, `index`, or `update` actions.
  580. If the action type is `index`, the action must include valid
  581. <<bulk-optimistic-concurrency-control,`if_seq_no` and `if_primary_term`>>
  582. arguments.
  583. .*Example*
  584. [%collapsible]
  585. ====
  586. ////
  587. [source,console]
  588. ----
  589. PUT /logs/_create/bfspvnIBr7VVZlfp2lqX?refresh=wait_for
  590. {
  591. "@timestamp": "2020-12-07T11:06:07.000Z",
  592. "user": {
  593. "id": "yWIumJd7"
  594. },
  595. "message": "Login successful"
  596. }
  597. ----
  598. // TEST[continued]
  599. ////
  600. The following bulk API request uses an `index` action to update an existing
  601. document in the `logs` data stream.
  602. The `index` action targets document ID `bfspvnIBr7VVZlfp2lqX` in the
  603. `.ds-logs-000003` backing index. The action also includes the current sequence
  604. number and primary term in the respective `if_seq_no` and `if_primary_term`
  605. parameters.
  606. [source,console]
  607. ----
  608. PUT /_bulk?refresh
  609. { "index": { "_index": ".ds-logs-000003", "_id": "bfspvnIBr7VVZlfp2lqX", "if_seq_no": 8, "if_primary_term": 1 } }
  610. { "@timestamp": "2020-12-07T11:06:07.000Z", "user": { "id": "8a4f500d" }, "message": "Login successful" }
  611. ----
  612. // TEST[continued]
  613. ====
  614. ////
  615. [source,console]
  616. ----
  617. DELETE /_data_stream/logs
  618. DELETE /_data_stream/logs_alt
  619. DELETE /_index_template/logs_data_stream
  620. ----
  621. // TEST[continued]
  622. ////