ecommerce-tutorial.asciidoc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. . Choose the `pivot` type of {transform} and play with various options for
  23. grouping and aggregating the data.
  24. +
  25. --
  26. There are two types of {transforms}, but first we'll try out _pivoting_ your
  27. data, which involves using at least one field to group it and applying at least
  28. one aggregation. You can preview what the transformed data will look
  29. like, so go ahead and play with it! You can also enable histogram charts to get
  30. a better understanding of the distribution of values in your data.
  31. For example, you might want to group the data by product ID and calculate the
  32. total number of sales for each product and its average price. Alternatively, you
  33. might want to look at the behavior of individual customers and calculate how
  34. much each customer spent in total and how many different categories of products
  35. they purchased. Or you might want to take the currencies or geographies into
  36. consideration. What are the most interesting ways you can transform and
  37. interpret this data?
  38. Go to *Management* > *Stack Management* > *Data* > *Transforms* in {kib} and use
  39. the wizard to create a {transform}:
  40. [role="screenshot"]
  41. image::images/ecommerce-pivot1.png["Creating a simple {transform} in {kib}"]
  42. Group the data by customer ID and add one or more aggregations to learn more
  43. about each customer's orders. For example, let's calculate the sum of products
  44. they purchased, the total price of their purchases, the maximum number of
  45. products that they purchased in a single order, and their total number of orders. We'll accomplish this by using the
  46. <<search-aggregations-metrics-sum-aggregation,`sum` aggregation>> on the
  47. `total_quantity` and `taxless_total_price` fields, the
  48. <<search-aggregations-metrics-max-aggregation,`max` aggregation>> on the
  49. `total_quantity` field, and the
  50. <<search-aggregations-metrics-cardinality-aggregation,`cardinality` aggregation>>
  51. on the `order_id` field:
  52. [role="screenshot"]
  53. image::images/ecommerce-pivot2.png["Adding multiple aggregations to a {transform} in {kib}"]
  54. TIP: If you're interested in a subset of the data, you can optionally include a
  55. <<request-body-search-query,query>> element. In this
  56. example, we've filtered the data so that we're only looking at orders with a
  57. `currency` of `EUR`. Alternatively, we could group the data by that field too.
  58. If you want to use more complex queries, you can create your {dataframe} from a
  59. {kibana-ref}/save-open-search.html[saved search].
  60. If you prefer, you can use the
  61. <<preview-transform,preview {transforms} API>>.
  62. .API example
  63. [%collapsible]
  64. ====
  65. [source,console]
  66. --------------------------------------------------
  67. POST _transform/_preview
  68. {
  69. "source": {
  70. "index": "kibana_sample_data_ecommerce",
  71. "query": {
  72. "bool": {
  73. "filter": {
  74. "term": {"currency": "EUR"}
  75. }
  76. }
  77. }
  78. },
  79. "pivot": {
  80. "group_by": {
  81. "customer_id": {
  82. "terms": {
  83. "field": "customer_id"
  84. }
  85. }
  86. },
  87. "aggregations": {
  88. "total_quantity.sum": {
  89. "sum": {
  90. "field": "total_quantity"
  91. }
  92. },
  93. "taxless_total_price.sum": {
  94. "sum": {
  95. "field": "taxless_total_price"
  96. }
  97. },
  98. "total_quantity.max": {
  99. "max": {
  100. "field": "total_quantity"
  101. }
  102. },
  103. "order_id.cardinality": {
  104. "cardinality": {
  105. "field": "order_id"
  106. }
  107. }
  108. }
  109. }
  110. }
  111. --------------------------------------------------
  112. // TEST[skip:set up sample data]
  113. ====
  114. --
  115. . When you are satisfied with what you see in the preview, create the
  116. {transform}.
  117. +
  118. --
  119. .. Supply a {transform} ID, the name of the target (or _destination_) index and
  120. optionally a description. If the target index does not exist, it will be created
  121. automatically.
  122. .. Decide whether you want the {transform} to run once or continuously. Since
  123. this sample data index is unchanging, let's use the default behavior and just
  124. run the {transform} once. If you want to try it out, however, go ahead and click
  125. on *Continuous mode*. You must choose a field that the {transform} can use to
  126. check which entities have changed. In general, it's a good idea to use the
  127. ingest timestamp field. In this example, however, you can use the `order_date`
  128. field.
  129. .. Optionally, you can configure a retention policy that applies to your
  130. {transform}. Select a date field that is used to identify old documents
  131. in the destination index and provide a maximum age. Documents that are older
  132. than the configured value are removed from the destination index.
  133. [role="screenshot"]
  134. image::images/ecommerce-pivot3.png["Adding transfrom ID and retention policy to a {transform} in {kib}"]
  135. If you prefer, you can use the
  136. <<put-transform,create {transforms} API>>.
  137. .API example
  138. [%collapsible]
  139. ====
  140. [source,console]
  141. --------------------------------------------------
  142. PUT _transform/ecommerce-customer-transform
  143. {
  144. "source": {
  145. "index": [
  146. "kibana_sample_data_ecommerce"
  147. ],
  148. "query": {
  149. "bool": {
  150. "filter": {
  151. "term": {
  152. "currency": "EUR"
  153. }
  154. }
  155. }
  156. }
  157. },
  158. "pivot": {
  159. "group_by": {
  160. "customer_id": {
  161. "terms": {
  162. "field": "customer_id"
  163. }
  164. }
  165. },
  166. "aggregations": {
  167. "total_quantity.sum": {
  168. "sum": {
  169. "field": "total_quantity"
  170. }
  171. },
  172. "taxless_total_price.sum": {
  173. "sum": {
  174. "field": "taxless_total_price"
  175. }
  176. },
  177. "total_quantity.max": {
  178. "max": {
  179. "field": "total_quantity"
  180. }
  181. },
  182. "order_id.cardinality": {
  183. "cardinality": {
  184. "field": "order_id"
  185. }
  186. }
  187. }
  188. },
  189. "dest": {
  190. "index": "ecommerce-customers"
  191. },
  192. "retention_policy": {
  193. "time": {
  194. "field": "order_date",
  195. "max_age": "60d"
  196. }
  197. }
  198. }
  199. --------------------------------------------------
  200. // TEST[skip:setup kibana sample data]
  201. ====
  202. --
  203. . Start the {transform}.
  204. +
  205. --
  206. TIP: Even though resource utilization is automatically adjusted based on the
  207. cluster load, a {transform} increases search and indexing load on your
  208. cluster while it runs. If you're experiencing an excessive load, however, you
  209. can stop it.
  210. You can start, stop, and manage {transforms} in {kib}:
  211. [role="screenshot"]
  212. image::images/manage-transforms.png["Managing {transforms} in {kib}"]
  213. Alternatively, you can use the
  214. <<start-transform,start {transforms}>> and
  215. <<stop-transform,stop {transforms}>> APIs.
  216. .API example
  217. [%collapsible]
  218. ====
  219. [source,console]
  220. --------------------------------------------------
  221. POST _transform/ecommerce-customer-transform/_start
  222. --------------------------------------------------
  223. // TEST[skip:setup kibana sample data]
  224. ====
  225. TIP: If you chose a batch {transform}, it is a single operation that has a
  226. single checkpoint. You cannot restart it when it's complete. {ctransforms-cap}
  227. differ in that they continually increment and process checkpoints as new source
  228. data is ingested.
  229. --
  230. . Explore the data in your new index.
  231. +
  232. --
  233. For example, use the *Discover* application in {kib}:
  234. [role="screenshot"]
  235. image::images/ecommerce-results.png["Exploring the new index in {kib}"]
  236. --
  237. . Optional: Create another {transform}, this time using the `latest` method.
  238. +
  239. --
  240. This method populates the destination index with the latest documents for each
  241. unique key value. For example, you might want to find the latest orders (sorted
  242. by the `order_date` field) for each customer or for each country and region.
  243. [role="screenshot"]
  244. image::images/ecommerce-latest1.png["Creating a latest {transform} in {kib}"]
  245. .API example
  246. [%collapsible]
  247. ====
  248. [source,console]
  249. --------------------------------------------------
  250. POST _transform/_preview
  251. {
  252. "source": {
  253. "index": "kibana_sample_data_ecommerce",
  254. "query": {
  255. "bool": {
  256. "filter": {
  257. "term": {"currency": "EUR"}
  258. }
  259. }
  260. }
  261. },
  262. "latest": {
  263. "unique_key": ["geoip.country_iso_code", "geoip.region_name"],
  264. "sort": "order_date"
  265. }
  266. }
  267. --------------------------------------------------
  268. // TEST[skip:set up sample data]
  269. ====
  270. --
  271. . If you do not want to keep a {transform}, you can delete it in
  272. {kib} or use the <<delete-transform,delete {transform} API>>. By default, when
  273. you delete a {transform}, its destination index and {kib} index patterns remain.
  274. Now that you've created simple {transforms} for {kib} sample data, consider
  275. possible use cases for your own data. For more ideas, see
  276. <<transform-usage>> and <<transform-examples>>.