use-a-data-stream.asciidoc 15 KB

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