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_transform` cluster privileges to preview and create
  15. {transforms}. Members of the built-in `transform_admin` role have these
  16. 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 *Management* > *Elasticsearch* > *Transforms* 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. <<search-aggregations-metrics-sum-aggregation,`sum` aggregation>> on the
  54. `taxless_total_price` field, the
  55. <<search-aggregations-metrics-max-aggregation,`max` aggregation>> on the
  56. `total_quantity` field, and the
  57. <<search-aggregations-metrics-cardinality-aggregation,`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. <<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. <<preview-transform,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. If you want to try it out, however, go ahead and click on *Continuous mode*.
  131. You must choose a field that the {transform} can use to check which
  132. entities have changed. In general, it's a good idea to use the ingest timestamp
  133. field. In this example, however, you can use the `order_date` field.
  134. If you prefer, you can use the
  135. <<put-transform,create {transforms} API>>. For
  136. example:
  137. [source,console]
  138. --------------------------------------------------
  139. PUT _transform/ecommerce-customer-transform
  140. {
  141. "source": {
  142. "index": [
  143. "kibana_sample_data_ecommerce"
  144. ],
  145. "query": {
  146. "bool": {
  147. "filter": {
  148. "term": {
  149. "currency": "EUR"
  150. }
  151. }
  152. }
  153. }
  154. },
  155. "pivot": {
  156. "group_by": {
  157. "customer_id": {
  158. "terms": {
  159. "field": "customer_id"
  160. }
  161. }
  162. },
  163. "aggregations": {
  164. "total_quantity.sum": {
  165. "sum": {
  166. "field": "total_quantity"
  167. }
  168. },
  169. "taxless_total_price.sum": {
  170. "sum": {
  171. "field": "taxless_total_price"
  172. }
  173. },
  174. "total_quantity.max": {
  175. "max": {
  176. "field": "total_quantity"
  177. }
  178. },
  179. "order_id.cardinality": {
  180. "cardinality": {
  181. "field": "order_id"
  182. }
  183. }
  184. }
  185. },
  186. "dest": {
  187. "index": "ecommerce-customers"
  188. }
  189. }
  190. --------------------------------------------------
  191. // TEST[skip:setup kibana sample data]
  192. --
  193. . Start the {transform}.
  194. +
  195. --
  196. TIP: Even though resource utilization is automatically adjusted based on the
  197. cluster load, a {transform} increases search and indexing load on your
  198. cluster while it runs. If you're experiencing an excessive load, however, you
  199. can stop it.
  200. You can start, stop, and manage {transforms} in {kib}:
  201. [role="screenshot"]
  202. image::images/manage-transforms.jpg["Managing {transforms} in {kib}"]
  203. Alternatively, you can use the
  204. <<start-transform,start {transforms}>> and
  205. <<stop-transform,stop {transforms}>> APIs. For
  206. example:
  207. [source,console]
  208. --------------------------------------------------
  209. POST _transform/ecommerce-customer-transform/_start
  210. --------------------------------------------------
  211. // TEST[skip:setup kibana sample data]
  212. TIP: If you chose a batch {transform}, it is a single operation that has a
  213. single checkpoint. You cannot restart it when it's complete. {ctransforms-cap}
  214. differ in that they continually increment and process checkpoints as new source
  215. data is ingested.
  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. <<delete-transform,delete {transform} API>>. When
  227. you delete a {transform}, its destination index and {kib} index
  228. patterns remain.