1
0

use-a-data-stream.asciidoc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. * <<get-stats-for-a-data-stream>>
  9. * <<manually-roll-over-a-data-stream>>
  10. * <<open-closed-backing-indices>>
  11. * <<reindex-with-a-data-stream>>
  12. * <<update-docs-in-a-data-stream-by-query>>
  13. * <<delete-docs-in-a-data-stream-by-query>>
  14. * <<update-delete-docs-in-a-backing-index>>
  15. ////
  16. [source,console]
  17. ----
  18. PUT /_index_template/my-data-stream-template
  19. {
  20. "index_patterns": [ "my-data-stream*" ],
  21. "data_stream": { }
  22. }
  23. PUT /_data_stream/my-data-stream
  24. POST /my-data-stream/_rollover/
  25. POST /my-data-stream/_rollover/
  26. PUT /my-data-stream/_create/bfspvnIBr7VVZlfp2lqX?refresh=wait_for
  27. {
  28. "@timestamp": "2099-03-08T11:06:07.000Z",
  29. "user": {
  30. "id": "yWIumJd7"
  31. },
  32. "message": "Login successful"
  33. }
  34. ----
  35. // TESTSETUP
  36. [source,console]
  37. ----
  38. DELETE /_data_stream/my-data-stream*
  39. DELETE /_index_template/my-data-stream-template
  40. ----
  41. // TEARDOWN
  42. ////
  43. [discrete]
  44. [[add-documents-to-a-data-stream]]
  45. === Add documents to a data stream
  46. To add an individual document, use the <<docs-index_,index API>>.
  47. <<ingest,Ingest pipelines>> are supported.
  48. [source,console]
  49. ----
  50. POST /my-data-stream/_doc/
  51. {
  52. "@timestamp": "2099-03-08T11:06:07.000Z",
  53. "user": {
  54. "id": "8a4f500d"
  55. },
  56. "message": "Login successful"
  57. }
  58. ----
  59. You cannot add new documents to a data stream using the index API's `PUT
  60. /<target>/_doc/<_id>` request format. To specify a document ID, use the `PUT
  61. /<target>/_create/<_id>` format instead. Only an
  62. <<docs-index-api-op_type,`op_type`>> of `create` is supported.
  63. To add multiple documents with a single request, use the <<docs-bulk,bulk API>>.
  64. Only `create` actions are supported.
  65. [source,console]
  66. ----
  67. PUT /my-data-stream/_bulk?refresh
  68. {"create":{ }}
  69. { "@timestamp": "2099-03-08T11:04:05.000Z", "user": { "id": "vlb44hny" }, "message": "Login attempt failed" }
  70. {"create":{ }}
  71. { "@timestamp": "2099-03-08T11:06:07.000Z", "user": { "id": "8a4f500d" }, "message": "Login successful" }
  72. {"create":{ }}
  73. { "@timestamp": "2099-03-09T11:07:08.000Z", "user": { "id": "l7gk7f82" }, "message": "Logout successful" }
  74. ----
  75. [discrete]
  76. [[search-a-data-stream]]
  77. === Search a data stream
  78. The following search APIs support data streams:
  79. * <<search-search, Search>>
  80. * <<async-search, Async search>>
  81. * <<search-multi-search, Multi search>>
  82. * <<search-field-caps, Field capabilities>>
  83. * <<eql-search-api, EQL search>>
  84. [discrete]
  85. [[get-stats-for-a-data-stream]]
  86. === Get statistics for a data stream
  87. Use the <<data-stream-stats-api,data stream stats API>> to get
  88. statistics for one or more data streams:
  89. [source,console]
  90. ----
  91. GET /_data_stream/my-data-stream/_stats?human=true
  92. ----
  93. [discrete]
  94. [[manually-roll-over-a-data-stream]]
  95. === Manually roll over a data stream
  96. Use the <<indices-rollover-index,rollover API>> to manually
  97. <<data-streams-rollover,roll over>> a data stream. You have
  98. two options when manually rolling over:
  99. 1. To immediately trigger a rollover:
  100. +
  101. [source,console]
  102. ----
  103. POST /my-data-stream/_rollover/
  104. ----
  105. 2. Or to postpone the rollover until the next indexing event occurs:
  106. +
  107. [source,console]
  108. ----
  109. POST /my-data-stream/_rollover?lazy
  110. ----
  111. +
  112. Use the second to avoid having empty backing indices in data streams
  113. that do not get updated often.
  114. [discrete]
  115. [[open-closed-backing-indices]]
  116. === Open closed backing indices
  117. You cannot search a <<indices-close,closed>> backing index, even by searching
  118. its data stream. You also cannot <<update-docs-in-a-data-stream-by-query,update>>
  119. or <<delete-docs-in-a-data-stream-by-query,delete>> documents in a closed index.
  120. To re-open a closed backing index, submit an <<indices-open-close,open
  121. index API request>> directly to the index:
  122. [source,console]
  123. ----
  124. POST /.ds-my-data-stream-2099.03.07-000001/_open/
  125. ----
  126. // TEST[setup:my_index]
  127. // TEST[s/.ds-my-data-stream-2099.03.07-000001/my-index-000001/]
  128. To re-open all closed backing indices for a data stream, submit an open index
  129. API request to the stream:
  130. [source,console]
  131. ----
  132. POST /my-data-stream/_open/
  133. ----
  134. [discrete]
  135. [[reindex-with-a-data-stream]]
  136. === Reindex with a data stream
  137. Use the <<docs-reindex,reindex API>> to copy documents from an existing index,
  138. alias, or data stream to a data stream. Because data streams are
  139. <<data-streams-append-only,append-only>>, a reindex into a data stream must use
  140. an `op_type` of `create`. A reindex cannot update existing documents in a data
  141. stream.
  142. ////
  143. [source,console]
  144. ----
  145. PUT /_bulk?refresh=wait_for
  146. {"create":{"_index" : "archive_1"}}
  147. { "@timestamp": "2099-03-08T11:04:05.000Z" }
  148. {"create":{"_index" : "archive_2"}}
  149. { "@timestamp": "2099-03-08T11:06:07.000Z" }
  150. {"create":{"_index" : "archive_2"}}
  151. { "@timestamp": "2099-03-09T11:07:08.000Z" }
  152. {"create":{"_index" : "archive_2"}}
  153. { "@timestamp": "2099-03-09T11:07:08.000Z" }
  154. POST /_aliases
  155. {
  156. "actions" : [
  157. { "add" : { "index" : "archive_1", "alias" : "archive" } },
  158. { "add" : { "index" : "archive_2", "alias" : "archive", "is_write_index" : true} }
  159. ]
  160. }
  161. ----
  162. ////
  163. [source,console]
  164. ----
  165. POST /_reindex
  166. {
  167. "source": {
  168. "index": "archive"
  169. },
  170. "dest": {
  171. "index": "my-data-stream",
  172. "op_type": "create"
  173. }
  174. }
  175. ----
  176. // TEST[continued]
  177. [discrete]
  178. [[update-docs-in-a-data-stream-by-query]]
  179. === Update documents in a data stream by query
  180. Use the <<docs-update-by-query,update by query API>> to update documents in a
  181. data stream that match a provided query:
  182. [source,console]
  183. ----
  184. POST /my-data-stream/_update_by_query
  185. {
  186. "query": {
  187. "match": {
  188. "user.id": "l7gk7f82"
  189. }
  190. },
  191. "script": {
  192. "source": "ctx._source.user.id = params.new_id",
  193. "params": {
  194. "new_id": "XgdX0NoX"
  195. }
  196. }
  197. }
  198. ----
  199. [discrete]
  200. [[delete-docs-in-a-data-stream-by-query]]
  201. === Delete documents in a data stream by query
  202. Use the <<docs-delete-by-query,delete by query API>> to delete documents in a
  203. data stream that match a provided query:
  204. [source,console]
  205. ----
  206. POST /my-data-stream/_delete_by_query
  207. {
  208. "query": {
  209. "match": {
  210. "user.id": "vlb44hny"
  211. }
  212. }
  213. }
  214. ----
  215. [discrete]
  216. [[update-delete-docs-in-a-backing-index]]
  217. === Update or delete documents in a backing index
  218. If needed, you can update or delete documents in a data stream by sending
  219. requests to the backing index containing the document. You'll need:
  220. * The <<mapping-id-field,document ID>>
  221. * The name of the backing index containing the document
  222. * If updating the document, its <<optimistic-concurrency-control,sequence number
  223. and primary term>>
  224. To get this information, use a <<search-a-data-stream,search request>>:
  225. [source,console]
  226. ----
  227. GET /my-data-stream/_search
  228. {
  229. "seq_no_primary_term": true,
  230. "query": {
  231. "match": {
  232. "user.id": "yWIumJd7"
  233. }
  234. }
  235. }
  236. ----
  237. Response:
  238. [source,console-result]
  239. ----
  240. {
  241. "took": 20,
  242. "timed_out": false,
  243. "_shards": {
  244. "total": 3,
  245. "successful": 3,
  246. "skipped": 0,
  247. "failed": 0
  248. },
  249. "hits": {
  250. "total": {
  251. "value": 1,
  252. "relation": "eq"
  253. },
  254. "max_score": 0.2876821,
  255. "hits": [
  256. {
  257. "_index": ".ds-my-data-stream-2099.03.08-000003", <1>
  258. "_id": "bfspvnIBr7VVZlfp2lqX", <2>
  259. "_seq_no": 0, <3>
  260. "_primary_term": 1, <4>
  261. "_score": 0.2876821,
  262. "_source": {
  263. "@timestamp": "2099-03-08T11:06:07.000Z",
  264. "user": {
  265. "id": "yWIumJd7"
  266. },
  267. "message": "Login successful"
  268. }
  269. }
  270. ]
  271. }
  272. }
  273. ----
  274. // TESTRESPONSE[s/"took": 20/"took": $body.took/]
  275. // TESTRESPONSE[s/"max_score": 0.2876821/"max_score": $body.hits.max_score/]
  276. // TESTRESPONSE[s/"_index": ".ds-my-data-stream-2099.03.08-000003"/"_index": $body.hits.hits.0._index/]
  277. // TESTRESPONSE[s/"_score": 0.2876821/"_score": $body.hits.hits.0._score/]
  278. <1> Backing index containing the matching document
  279. <2> Document ID for the document
  280. <3> Current sequence number for the document
  281. <4> Primary term for the document
  282. To update the document, use an <<docs-index_,index API>> request with valid
  283. `if_seq_no` and `if_primary_term` arguments:
  284. [source,console]
  285. ----
  286. PUT /.ds-my-data-stream-2099-03-08-000003/_doc/bfspvnIBr7VVZlfp2lqX?if_seq_no=0&if_primary_term=1
  287. {
  288. "@timestamp": "2099-03-08T11:06:07.000Z",
  289. "user": {
  290. "id": "8a4f500d"
  291. },
  292. "message": "Login successful"
  293. }
  294. ----
  295. // TEST[setup:my_index]
  296. // TEST[s/.ds-my-data-stream-2099.03.08-000003/my-index-000001/]
  297. // TEST[s/bfspvnIBr7VVZlfp2lqX/1/]
  298. // TEST[s/if_seq_no=0/if_seq_no=1/]
  299. To delete the document, use the <<docs-delete,delete API>>:
  300. [source,console]
  301. ----
  302. DELETE /.ds-my-data-stream-2099.03.08-000003/_doc/bfspvnIBr7VVZlfp2lqX
  303. ----
  304. // TEST[setup:my_index]
  305. // TEST[s/.ds-my-data-stream-2099.03.08-000003/my-index-000001/]
  306. // TEST[s/bfspvnIBr7VVZlfp2lqX/1/]
  307. To delete or update multiple documents with a single request, use the
  308. <<docs-bulk,bulk API>>'s `delete`, `index`, and `update` actions. For `index`
  309. actions, include valid <<bulk-optimistic-concurrency-control,`if_seq_no` and
  310. `if_primary_term`>> arguments.
  311. [source,console]
  312. ----
  313. PUT /_bulk?refresh
  314. { "index": { "_index": ".ds-my-data-stream-2099.03.08-000003", "_id": "bfspvnIBr7VVZlfp2lqX", "if_seq_no": 0, "if_primary_term": 1 } }
  315. { "@timestamp": "2099-03-08T11:06:07.000Z", "user": { "id": "8a4f500d" }, "message": "Login successful" }
  316. ----
  317. // TEST[setup:my_index]
  318. // TEST[s/.ds-my-data-stream-2099.03.08-000003/my-index-000001/]
  319. // TEST[s/bfspvnIBr7VVZlfp2lqX/1/]