1
0

use-a-data-stream.asciidoc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. [[use-a-data-stream]]
  2. == Use a data stream
  3. After you <<set-up-a-data-stream,set up a data stream set up>>, 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. ////
  9. [source,console]
  10. ----
  11. PUT /_index_template/logs_data_stream
  12. {
  13. "index_patterns": [ "logs*" ],
  14. "data_stream": {
  15. "timestamp_field": "@timestamp"
  16. },
  17. "template": {
  18. "mappings": {
  19. "properties": {
  20. "@timestamp": {
  21. "type": "date"
  22. }
  23. }
  24. }
  25. }
  26. }
  27. PUT /_data_stream/logs
  28. ----
  29. ////
  30. [discrete]
  31. [[add-documents-to-a-data-stream]]
  32. === Add documents to a data stream
  33. You can add documents to a data stream using the following requests:
  34. * An <<docs-index_,index API>> request with an
  35. <<docs-index-api-op_type,`op_type`>> set to `create`. Specify the data
  36. stream's name in place of an index name.
  37. +
  38. --
  39. NOTE: The `op_type` parameter defaults to `create` when adding new documents.
  40. .*Example: Index API request*
  41. [%collapsible]
  42. ====
  43. The following <<docs-index_,index API>> adds a new document to the `logs` data
  44. stream.
  45. [source,console]
  46. ----
  47. POST /logs/_doc/
  48. {
  49. "@timestamp": "2020-12-07T11:06:07.000Z",
  50. "user": {
  51. "id": "8a4f500d"
  52. },
  53. "message": "Login successful"
  54. }
  55. ----
  56. // TEST[continued]
  57. ====
  58. --
  59. * A <<docs-bulk,bulk API>> request using the `create` action. Specify the data
  60. stream's name in place of an index name.
  61. +
  62. --
  63. NOTE: Data streams do not support other bulk actions, such as `index`.
  64. .*Example: Bulk API request*
  65. [%collapsible]
  66. ====
  67. The following <<docs-bulk,bulk API>> index request adds several new documents to
  68. the `logs` data stream. Note that only the `create` action is used.
  69. [source,console]
  70. ----
  71. PUT /logs/_bulk?refresh
  72. {"create":{"_index" : "logs"}}
  73. { "@timestamp": "2020-12-08T11:04:05.000Z", "user": { "id": "vlb44hny" }, "message": "Login attempt failed" }
  74. {"create":{"_index" : "logs"}}
  75. { "@timestamp": "2020-12-08T11:06:07.000Z", "user": { "id": "8a4f500d" }, "message": "Login successful" }
  76. {"create":{"_index" : "logs"}}
  77. { "@timestamp": "2020-12-09T11:07:08.000Z", "user": { "id": "l7gk7f82" }, "message": "Logout successful" }
  78. ----
  79. // TEST[continued]
  80. ====
  81. --
  82. [discrete]
  83. [[search-a-data-stream]]
  84. === Search a data stream
  85. The following search APIs support data streams:
  86. * <<search-search, Search>>
  87. * <<async-search, Async search>>
  88. * <<search-multi-search, Multi search>>
  89. * <<search-field-caps, Field capabilities>>
  90. ////
  91. * <<eql-search-api, EQL search>>
  92. ////
  93. .*Example*
  94. [%collapsible]
  95. ====
  96. The following <<search-search,search API>> request searches the `logs` data
  97. stream for documents with a timestamp between today and yesterday that also have
  98. `message` value of `login successful`.
  99. [source,console]
  100. ----
  101. GET /logs/_search
  102. {
  103. "query": {
  104. "bool": {
  105. "must": {
  106. "range": {
  107. "@timestamp": {
  108. "gte": "now-1d/d",
  109. "lt": "now/d"
  110. }
  111. }
  112. },
  113. "should": {
  114. "match": {
  115. "message": "login successful"
  116. }
  117. }
  118. }
  119. }
  120. }
  121. ----
  122. // TEST[continued]
  123. ====
  124. [discrete]
  125. [[manually-roll-over-a-data-stream]]
  126. === Manually roll over a data stream
  127. A rollover creates a new backing index for a data stream. This new backing index
  128. becomes the stream's <<data-stream-write-index,write index>> and increments
  129. the stream's <<data-streams-generation,generation>>.
  130. In most cases, we recommend using <<index-lifecycle-management,{ilm-init}>> to
  131. automate rollovers for data streams. This lets you automatically roll over the
  132. current write index when it meets specified criteria, such as a maximum age or
  133. size.
  134. However, you can also use the <<indices-rollover-index,rollover API>> to
  135. manually perform a rollover. This can be useful if you want to apply mapping or
  136. setting changes to the stream's write index after updating a data stream's
  137. template.
  138. .*Example*
  139. [%collapsible]
  140. ====
  141. The following <<indices-rollover-index,rollover API>> request submits a manual
  142. rollover request for the `logs` data stream.
  143. [source,console]
  144. ----
  145. POST /logs/_rollover/
  146. {
  147. "conditions": {
  148. "max_docs": "1"
  149. }
  150. }
  151. ----
  152. // TEST[continued]
  153. ====
  154. ////
  155. [source,console]
  156. ----
  157. DELETE /_data_stream/logs
  158. DELETE /_index_template/logs_data_stream
  159. ----
  160. // TEST[continued]
  161. ////