use-a-data-stream.asciidoc 8.3 KB

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