ecommerce-tutorial.asciidoc 7.5 KB

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