use-a-data-stream.asciidoc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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/*
  39. DELETE /_index_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:
  98. [source,console]
  99. ----
  100. POST /my-data-stream/_rollover/
  101. ----
  102. [discrete]
  103. [[open-closed-backing-indices]]
  104. === Open closed backing indices
  105. You cannot search a <<indices-close,closed>> backing index, even by searching
  106. its data stream. You also cannot <<update-docs-in-a-data-stream-by-query,update>>
  107. or <<delete-docs-in-a-data-stream-by-query,delete>> documents in a closed index.
  108. To re-open a closed backing index, submit an <<indices-open-close,open
  109. index API request>> directly to the index:
  110. [source,console]
  111. ----
  112. POST /.ds-my-data-stream-2099.03.07-000001/_open/
  113. ----
  114. // TEST[setup:my_index]
  115. // TEST[s/.ds-my-data-stream-2099.03.07-000001/my-index-000001/]
  116. To re-open all closed backing indices for a data stream, submit an open index
  117. API request to the stream:
  118. [source,console]
  119. ----
  120. POST /my-data-stream/_open/
  121. ----
  122. [discrete]
  123. [[reindex-with-a-data-stream]]
  124. === Reindex with a data stream
  125. Use the <<docs-reindex,reindex API>> to copy documents from an existing index,
  126. alias, or data stream to a data stream. Because data streams are
  127. <<data-streams-append-only,append-only>>, a reindex into a data stream must use
  128. an `op_type` of `create`. A reindex cannot update existing documents in a data
  129. stream.
  130. ////
  131. [source,console]
  132. ----
  133. PUT /_bulk?refresh=wait_for
  134. {"create":{"_index" : "archive_1"}}
  135. { "@timestamp": "2099-03-08T11:04:05.000Z" }
  136. {"create":{"_index" : "archive_2"}}
  137. { "@timestamp": "2099-03-08T11:06:07.000Z" }
  138. {"create":{"_index" : "archive_2"}}
  139. { "@timestamp": "2099-03-09T11:07:08.000Z" }
  140. {"create":{"_index" : "archive_2"}}
  141. { "@timestamp": "2099-03-09T11:07:08.000Z" }
  142. POST /_aliases
  143. {
  144. "actions" : [
  145. { "add" : { "index" : "archive_1", "alias" : "archive" } },
  146. { "add" : { "index" : "archive_2", "alias" : "archive", "is_write_index" : true} }
  147. ]
  148. }
  149. ----
  150. ////
  151. [source,console]
  152. ----
  153. POST /_reindex
  154. {
  155. "source": {
  156. "index": "archive"
  157. },
  158. "dest": {
  159. "index": "my-data-stream",
  160. "op_type": "create"
  161. }
  162. }
  163. ----
  164. // TEST[continued]
  165. [discrete]
  166. [[update-docs-in-a-data-stream-by-query]]
  167. === Update documents in a data stream by query
  168. Use the <<docs-update-by-query,update by query API>> to update documents in a
  169. data stream that match a provided query:
  170. [source,console]
  171. ----
  172. POST /my-data-stream/_update_by_query
  173. {
  174. "query": {
  175. "match": {
  176. "user.id": "l7gk7f82"
  177. }
  178. },
  179. "script": {
  180. "source": "ctx._source.user.id = params.new_id",
  181. "params": {
  182. "new_id": "XgdX0NoX"
  183. }
  184. }
  185. }
  186. ----
  187. [discrete]
  188. [[delete-docs-in-a-data-stream-by-query]]
  189. === Delete documents in a data stream by query
  190. Use the <<docs-delete-by-query,delete by query API>> to delete documents in a
  191. data stream that match a provided query:
  192. [source,console]
  193. ----
  194. POST /my-data-stream/_delete_by_query
  195. {
  196. "query": {
  197. "match": {
  198. "user.id": "vlb44hny"
  199. }
  200. }
  201. }
  202. ----
  203. [discrete]
  204. [[update-delete-docs-in-a-backing-index]]
  205. === Update or delete documents in a backing index
  206. If needed, you can update or delete documents in a data stream by sending
  207. requests to the backing index containing the document. You'll need:
  208. * The <<mapping-id-field,document ID>>
  209. * The name of the backing index containing the document
  210. * If updating the document, its <<optimistic-concurrency-control,sequence number
  211. and primary term>>
  212. To get this information, use a <<search-a-data-stream,search request>>:
  213. [source,console]
  214. ----
  215. GET /my-data-stream/_search
  216. {
  217. "seq_no_primary_term": true,
  218. "query": {
  219. "match": {
  220. "user.id": "yWIumJd7"
  221. }
  222. }
  223. }
  224. ----
  225. Response:
  226. [source,console-result]
  227. ----
  228. {
  229. "took": 20,
  230. "timed_out": false,
  231. "_shards": {
  232. "total": 3,
  233. "successful": 3,
  234. "skipped": 0,
  235. "failed": 0
  236. },
  237. "hits": {
  238. "total": {
  239. "value": 1,
  240. "relation": "eq"
  241. },
  242. "max_score": 0.2876821,
  243. "hits": [
  244. {
  245. "_index": ".ds-my-data-stream-2099.03.08-000003", <1>
  246. "_id": "bfspvnIBr7VVZlfp2lqX", <2>
  247. "_seq_no": 0, <3>
  248. "_primary_term": 1, <4>
  249. "_score": 0.2876821,
  250. "_source": {
  251. "@timestamp": "2099-03-08T11:06:07.000Z",
  252. "user": {
  253. "id": "yWIumJd7"
  254. },
  255. "message": "Login successful"
  256. }
  257. }
  258. ]
  259. }
  260. }
  261. ----
  262. // TESTRESPONSE[s/"took": 20/"took": $body.took/]
  263. // TESTRESPONSE[s/"max_score": 0.2876821/"max_score": $body.hits.max_score/]
  264. // TESTRESPONSE[s/"_index": ".ds-my-data-stream-2099.03.08-000003"/"_index": $body.hits.hits.0._index/]
  265. // TESTRESPONSE[s/"_score": 0.2876821/"_score": $body.hits.hits.0._score/]
  266. <1> Backing index containing the matching document
  267. <2> Document ID for the document
  268. <3> Current sequence number for the document
  269. <4> Primary term for the document
  270. To update the document, use an <<docs-index_,index API>> request with valid
  271. `if_seq_no` and `if_primary_term` arguments:
  272. [source,console]
  273. ----
  274. PUT /.ds-my-data-stream-2099-03-08-000003/_doc/bfspvnIBr7VVZlfp2lqX?if_seq_no=0&if_primary_term=1
  275. {
  276. "@timestamp": "2099-03-08T11:06:07.000Z",
  277. "user": {
  278. "id": "8a4f500d"
  279. },
  280. "message": "Login successful"
  281. }
  282. ----
  283. // TEST[setup:my_index]
  284. // TEST[s/.ds-my-data-stream-2099.03.08-000003/my-index-000001/]
  285. // TEST[s/bfspvnIBr7VVZlfp2lqX/1/]
  286. // TEST[s/if_seq_no=0/if_seq_no=1/]
  287. To delete the document, use the <<docs-delete,delete API>>:
  288. [source,console]
  289. ----
  290. DELETE /.ds-my-data-stream-2099.03.08-000003/_doc/bfspvnIBr7VVZlfp2lqX
  291. ----
  292. // TEST[setup:my_index]
  293. // TEST[s/.ds-my-data-stream-2099.03.08-000003/my-index-000001/]
  294. // TEST[s/bfspvnIBr7VVZlfp2lqX/1/]
  295. To delete or update multiple documents with a single request, use the
  296. <<docs-bulk,bulk API>>'s `delete`, `index`, and `update` actions. For `index`
  297. actions, include valid <<bulk-optimistic-concurrency-control,`if_seq_no` and
  298. `if_primary_term`>> arguments.
  299. [source,console]
  300. ----
  301. PUT /_bulk?refresh
  302. { "index": { "_index": ".ds-my-data-stream-2099.03.08-000003", "_id": "bfspvnIBr7VVZlfp2lqX", "if_seq_no": 0, "if_primary_term": 1 } }
  303. { "@timestamp": "2099-03-08T11:06:07.000Z", "user": { "id": "8a4f500d" }, "message": "Login successful" }
  304. ----
  305. // TEST[setup:my_index]
  306. // TEST[s/.ds-my-data-stream-2099.03.08-000003/my-index-000001/]
  307. // TEST[s/bfspvnIBr7VVZlfp2lqX/1/]