ecommerce-tutorial.asciidoc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. [role="xpack"]
  2. [testenv="basic"]
  3. [[ecommerce-transforms]]
  4. === Tutorial: Transforming the eCommerce sample data
  5. beta[]
  6. <<transforms,{transforms-cap}>> enable you to retrieve information
  7. from an {es} index, transform it, and store it in another index. Let's use the
  8. {kibana-ref}/add-sample-data.html[{kib} sample data] to demonstrate how you can
  9. pivot and summarize your data with {transforms}.
  10. . If the {es} {security-features} are enabled, obtain a user ID with sufficient
  11. privileges to complete these steps.
  12. +
  13. --
  14. You need `manage_data_frame_transforms` cluster privileges to preview and create
  15. {transforms}. Members of the built-in `data_frame_transforms_admin`
  16. role have these privileges.
  17. You also need `read` and `view_index_metadata` index privileges on the source
  18. index and `read`, `create_index`, and `index` privileges on the destination
  19. index.
  20. For more information, see
  21. {stack-ov}/security-privileges.html[Security privileges] and
  22. {stack-ov}/built-in-roles.html[Built-in roles].
  23. --
  24. . Choose your _source index_.
  25. +
  26. --
  27. In this example, we'll use the eCommerce orders sample data. If you're not
  28. already familiar with the `kibana_sample_data_ecommerce` index, use the
  29. *Revenue* dashboard in {kib} to explore the data. Consider what insights you
  30. might want to derive from this eCommerce data.
  31. --
  32. . Play with various options for grouping and aggregating the data.
  33. +
  34. --
  35. _Pivoting_ your data involves using at least one field to group it and applying
  36. at least one aggregation. You can preview what the transformed data will look
  37. like, so go ahead and play with it!
  38. For example, you might want to group the data by product ID and calculate the
  39. total number of sales for each product and its average price. Alternatively, you
  40. might want to look at the behavior of individual customers and calculate how
  41. much each customer spent in total and how many different categories of products
  42. they purchased. Or you might want to take the currencies or geographies into
  43. consideration. What are the most interesting ways you can transform and
  44. interpret this data?
  45. Go to *Machine Learning* > *Data Frames* in {kib} and use the
  46. wizard to create a {transform}:
  47. [role="screenshot"]
  48. image::images/ecommerce-pivot1.jpg["Creating a simple {transform} in {kib}"]
  49. In this case, we grouped the data by customer ID and calculated the sum of
  50. products each customer purchased.
  51. Let's add some more aggregations to learn more about our customers' orders. For
  52. example, let's calculate the total sum of their purchases, the maximum number of
  53. products that they purchased in a single order, and their total number of orders.
  54. We'll accomplish this by using the
  55. {ref}/search-aggregations-metrics-sum-aggregation.html[`sum` aggregation] on the
  56. `taxless_total_price` field, the
  57. {ref}/search-aggregations-metrics-max-aggregation.html[`max` aggregation] on the
  58. `total_quantity` field, and the
  59. {ref}/search-aggregations-metrics-cardinality-aggregation.html[`cardinality` aggregation]
  60. on the `order_id` field:
  61. [role="screenshot"]
  62. image::images/ecommerce-pivot2.jpg["Adding multiple aggregations to a {transform} in {kib}"]
  63. TIP: If you're interested in a subset of the data, you can optionally include a
  64. {ref}/search-request-body.html#request-body-search-query[query] element. In this
  65. example, we've filtered the data so that we're only looking at orders with a
  66. `currency` of `EUR`. Alternatively, we could group the data by that field too.
  67. If you want to use more complex queries, you can create your {dataframe} from a
  68. {kibana-ref}/save-open-search.html[saved search].
  69. If you prefer, you can use the
  70. {ref}/preview-transform.html[preview {transforms} API]:
  71. [source,console]
  72. --------------------------------------------------
  73. POST _data_frame/transforms/_preview
  74. {
  75. "source": {
  76. "index": "kibana_sample_data_ecommerce",
  77. "query": {
  78. "bool": {
  79. "filter": {
  80. "term": {"currency": "EUR"}
  81. }
  82. }
  83. }
  84. },
  85. "pivot": {
  86. "group_by": {
  87. "customer_id": {
  88. "terms": {
  89. "field": "customer_id"
  90. }
  91. }
  92. },
  93. "aggregations": {
  94. "total_quantity.sum": {
  95. "sum": {
  96. "field": "total_quantity"
  97. }
  98. },
  99. "taxless_total_price.sum": {
  100. "sum": {
  101. "field": "taxless_total_price"
  102. }
  103. },
  104. "total_quantity.max": {
  105. "max": {
  106. "field": "total_quantity"
  107. }
  108. },
  109. "order_id.cardinality": {
  110. "cardinality": {
  111. "field": "order_id"
  112. }
  113. }
  114. }
  115. }
  116. }
  117. --------------------------------------------------
  118. // TEST[skip:set up sample data]
  119. --
  120. . When you are satisfied with what you see in the preview, create the
  121. {transform}.
  122. +
  123. --
  124. .. Supply a job ID and the name of the target (or _destination_) index. If the
  125. target index does not exist, it will be created automatically.
  126. .. Decide whether you want the {transform} to run once or continuously.
  127. --
  128. +
  129. --
  130. Since this sample data index is unchanging, let's use the default behavior and
  131. just run the {transform} once.
  132. [role="screenshot"]
  133. image::images/ecommerce-batch.jpg["Specifying the {transform} options in {kib}"]
  134. If you want to try it out, however, go ahead and click on *Continuous mode*.
  135. You must choose a field that the {transform} can use to check which
  136. entities have changed. In general, it's a good idea to use the ingest timestamp
  137. field. In this example, however, you can use the `order_date` field.
  138. If you prefer, you can use the
  139. {ref}/put-transform.html[create {transforms} API]. For
  140. example:
  141. [source,console]
  142. --------------------------------------------------
  143. PUT _data_frame/transforms/ecommerce-customer-transform
  144. {
  145. "source": {
  146. "index": [
  147. "kibana_sample_data_ecommerce"
  148. ],
  149. "query": {
  150. "bool": {
  151. "filter": {
  152. "term": {
  153. "currency": "EUR"
  154. }
  155. }
  156. }
  157. }
  158. },
  159. "pivot": {
  160. "group_by": {
  161. "customer_id": {
  162. "terms": {
  163. "field": "customer_id"
  164. }
  165. }
  166. },
  167. "aggregations": {
  168. "total_quantity.sum": {
  169. "sum": {
  170. "field": "total_quantity"
  171. }
  172. },
  173. "taxless_total_price.sum": {
  174. "sum": {
  175. "field": "taxless_total_price"
  176. }
  177. },
  178. "total_quantity.max": {
  179. "max": {
  180. "field": "total_quantity"
  181. }
  182. },
  183. "order_id.cardinality": {
  184. "cardinality": {
  185. "field": "order_id"
  186. }
  187. }
  188. }
  189. },
  190. "dest": {
  191. "index": "ecommerce-customers"
  192. }
  193. }
  194. --------------------------------------------------
  195. // TEST[skip:setup kibana sample data]
  196. --
  197. . Start the {transform}.
  198. +
  199. --
  200. TIP: Even though resource utilization is automatically adjusted based on the
  201. cluster load, a {transform} increases search and indexing load on your
  202. cluster while it runs. If you're experiencing an excessive load, however, you
  203. can stop it.
  204. You can start, stop, and manage {transforms} in {kib}:
  205. [role="screenshot"]
  206. image::images/dataframe-transforms.jpg["Managing {transforms} in {kib}"]
  207. Alternatively, you can use the
  208. {ref}/start-transform.html[start {transforms}] and
  209. {ref}/stop-transform.html[stop {transforms}] APIs. For
  210. example:
  211. [source,console]
  212. --------------------------------------------------
  213. POST _data_frame/transforms/ecommerce-customer-transform/_start
  214. --------------------------------------------------
  215. // TEST[skip:setup kibana sample data]
  216. --
  217. . Explore the data in your new index.
  218. +
  219. --
  220. For example, use the *Discover* application in {kib}:
  221. [role="screenshot"]
  222. image::images/ecommerce-results.jpg["Exploring the new index in {kib}"]
  223. --
  224. TIP: If you do not want to keep the {transform}, you can delete it in
  225. {kib} or use the
  226. {ref}/delete-transform.html[delete {transform} API]. When
  227. you delete a {transform}, its destination index and {kib} index
  228. patterns remain.