use-a-data-stream.asciidoc 16 KB

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