ecommerce-tutorial.asciidoc 7.5 KB

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