use-a-data-stream.asciidoc 16 KB

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