use-a-data-stream.asciidoc 17 KB

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