use-a-data-stream.asciidoc 8.3 KB

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