use-a-data-stream.asciidoc 13 KB

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