examples.asciidoc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. [role="xpack"]
  2. [testenv="basic"]
  3. [[transform-examples]]
  4. = {transform-cap} examples
  5. ++++
  6. <titleabbrev>Examples</titleabbrev>
  7. ++++
  8. These examples demonstrate how to use {transforms} to derive useful insights
  9. from your data. All the examples use one of the
  10. {kibana-ref}/add-sample-data.html[{kib} sample datasets]. For a more detailed,
  11. step-by-step example, see <<ecommerce-transforms>>.
  12. * <<example-best-customers>>
  13. * <<example-airline>>
  14. * <<example-clientips>>
  15. * <<example-last-log>>
  16. [[example-best-customers]]
  17. == Finding your best customers
  18. This example uses the eCommerce orders sample data set to find the customers who
  19. spent the most in a hypothetical webshop. Let's transform the data such that the
  20. destination index contains the number of orders, the total price of the orders,
  21. the amount of unique products and the average price per order, and the total
  22. amount of ordered products for each customer.
  23. [source,console]
  24. ----------------------------------
  25. POST _transform/_preview
  26. {
  27. "source": {
  28. "index": "kibana_sample_data_ecommerce"
  29. },
  30. "dest" : { <1>
  31. "index" : "sample_ecommerce_orders_by_customer"
  32. },
  33. "pivot": {
  34. "group_by": { <2>
  35. "user": { "terms": { "field": "user" }},
  36. "customer_id": { "terms": { "field": "customer_id" }}
  37. },
  38. "aggregations": {
  39. "order_count": { "value_count": { "field": "order_id" }},
  40. "total_order_amt": { "sum": { "field": "taxful_total_price" }},
  41. "avg_amt_per_order": { "avg": { "field": "taxful_total_price" }},
  42. "avg_unique_products_per_order": { "avg": { "field": "total_unique_products" }},
  43. "total_unique_products": { "cardinality": { "field": "products.product_id" }}
  44. }
  45. }
  46. }
  47. ----------------------------------
  48. // TEST[skip:setup kibana sample data]
  49. <1> The destination index for the {transform}. It is ignored by `_preview`.
  50. <2> Two `group_by` fields is selected. This means the {transform} contains a
  51. unique row per `user` and `customer_id` combination. Within this data set, both
  52. these fields are unique. By including both in the {transform}, it gives more
  53. context to the final results.
  54. NOTE: In the example above, condensed JSON formatting is used for easier
  55. readability of the pivot object.
  56. The preview {transforms} API enables you to see the layout of the {transform} in
  57. advance, populated with some sample values. For example:
  58. [source,js]
  59. ----------------------------------
  60. {
  61. "preview" : [
  62. {
  63. "total_order_amt" : 3946.9765625,
  64. "order_count" : 59.0,
  65. "total_unique_products" : 116.0,
  66. "avg_unique_products_per_order" : 2.0,
  67. "customer_id" : "10",
  68. "user" : "recip",
  69. "avg_amt_per_order" : 66.89790783898304
  70. },
  71. ...
  72. ]
  73. }
  74. ----------------------------------
  75. // NOTCONSOLE
  76. This {transform} makes it easier to answer questions such as:
  77. * Which customers spend the most?
  78. * Which customers spend the most per order?
  79. * Which customers order most often?
  80. * Which customers ordered the least number of different products?
  81. It's possible to answer these questions using aggregations alone, however
  82. {transforms} allow us to persist this data as a customer centric index. This
  83. enables us to analyze data at scale and gives more flexibility to explore and
  84. navigate data from a customer centric perspective. In some cases, it can even
  85. make creating visualizations much simpler.
  86. [[example-airline]]
  87. == Finding air carriers with the most delays
  88. This example uses the Flights sample data set to find out which air carrier
  89. had the most delays. First, filter the source data such that it excludes all
  90. the cancelled flights by using a query filter. Then transform the data to
  91. contain the distinct number of flights, the sum of delayed minutes, and the sum
  92. of the flight minutes by air carrier. Finally, use a
  93. <<search-aggregations-pipeline-bucket-script-aggregation,`bucket_script`>>
  94. to determine what percentage of the flight time was actually delay.
  95. [source,console]
  96. ----------------------------------
  97. POST _transform/_preview
  98. {
  99. "source": {
  100. "index": "kibana_sample_data_flights",
  101. "query": { <1>
  102. "bool": {
  103. "filter": [
  104. { "term": { "Cancelled": false } }
  105. ]
  106. }
  107. }
  108. },
  109. "dest" : { <2>
  110. "index" : "sample_flight_delays_by_carrier"
  111. },
  112. "pivot": {
  113. "group_by": { <3>
  114. "carrier": { "terms": { "field": "Carrier" }}
  115. },
  116. "aggregations": {
  117. "flights_count": { "value_count": { "field": "FlightNum" }},
  118. "delay_mins_total": { "sum": { "field": "FlightDelayMin" }},
  119. "flight_mins_total": { "sum": { "field": "FlightTimeMin" }},
  120. "delay_time_percentage": { <4>
  121. "bucket_script": {
  122. "buckets_path": {
  123. "delay_time": "delay_mins_total.value",
  124. "flight_time": "flight_mins_total.value"
  125. },
  126. "script": "(params.delay_time / params.flight_time) * 100"
  127. }
  128. }
  129. }
  130. }
  131. }
  132. ----------------------------------
  133. // TEST[skip:setup kibana sample data]
  134. <1> Filter the source data to select only flights that are not cancelled.
  135. <2> The destination index for the {transform}. It is ignored by `_preview`.
  136. <3> The data is grouped by the `Carrier` field which contains the airline name.
  137. <4> This `bucket_script` performs calculations on the results that are returned
  138. by the aggregation. In this particular example, it calculates what percentage of
  139. travel time was taken up by delays.
  140. The preview shows you that the new index would contain data like this for each
  141. carrier:
  142. [source,js]
  143. ----------------------------------
  144. {
  145. "preview" : [
  146. {
  147. "carrier" : "ES-Air",
  148. "flights_count" : 2802.0,
  149. "flight_mins_total" : 1436927.5130677223,
  150. "delay_time_percentage" : 9.335543983955839,
  151. "delay_mins_total" : 134145.0
  152. },
  153. ...
  154. ]
  155. }
  156. ----------------------------------
  157. // NOTCONSOLE
  158. This {transform} makes it easier to answer questions such as:
  159. * Which air carrier has the most delays as a percentage of flight time?
  160. NOTE: This data is fictional and does not reflect actual delays or flight stats
  161. for any of the featured destination or origin airports.
  162. [[example-clientips]]
  163. == Finding suspicious client IPs
  164. This example uses the web log sample data set to identify suspicious client IPs.
  165. It transform the data such that the new index contains the sum of bytes and the
  166. number of distinct URLs, agents, incoming requests by location, and geographic
  167. destinations for each client IP. It also uses filter aggregations to count the
  168. specific types of HTTP responses that each client IP receives. Ultimately, the
  169. example below transforms web log data into an entity centric index where the
  170. entity is `clientip`.
  171. [source,console]
  172. ----------------------------------
  173. PUT _transform/suspicious_client_ips
  174. {
  175. "source": {
  176. "index": "kibana_sample_data_logs"
  177. },
  178. "dest" : { <1>
  179. "index" : "sample_weblogs_by_clientip"
  180. },
  181. "sync" : { <2>
  182. "time": {
  183. "field": "timestamp",
  184. "delay": "60s"
  185. }
  186. },
  187. "pivot": {
  188. "group_by": { <3>
  189. "clientip": { "terms": { "field": "clientip" } }
  190. },
  191. "aggregations": {
  192. "url_dc": { "cardinality": { "field": "url.keyword" }},
  193. "bytes_sum": { "sum": { "field": "bytes" }},
  194. "geo.src_dc": { "cardinality": { "field": "geo.src" }},
  195. "agent_dc": { "cardinality": { "field": "agent.keyword" }},
  196. "geo.dest_dc": { "cardinality": { "field": "geo.dest" }},
  197. "responses.total": { "value_count": { "field": "timestamp" }},
  198. "success" : { <4>
  199. "filter": {
  200. "term": { "response" : "200"}}
  201. },
  202. "error404" : {
  203. "filter": {
  204. "term": { "response" : "404"}}
  205. },
  206. "error503" : {
  207. "filter": {
  208. "term": { "response" : "503"}}
  209. },
  210. "timestamp.min": { "min": { "field": "timestamp" }},
  211. "timestamp.max": { "max": { "field": "timestamp" }},
  212. "timestamp.duration_ms": { <5>
  213. "bucket_script": {
  214. "buckets_path": {
  215. "min_time": "timestamp.min.value",
  216. "max_time": "timestamp.max.value"
  217. },
  218. "script": "(params.max_time - params.min_time)"
  219. }
  220. }
  221. }
  222. }
  223. }
  224. ----------------------------------
  225. // TEST[skip:setup kibana sample data]
  226. <1> The destination index for the {transform}.
  227. <2> Configures the {transform} to run continuously. It uses the `timestamp`
  228. field to synchronize the source and destination indices. The worst case
  229. ingestion delay is 60 seconds.
  230. <3> The data is grouped by the `clientip` field.
  231. <4> Filter aggregation that counts the occurrences of successful (`200`)
  232. responses in the `response` field. The following two aggregations (`error404`
  233. and `error503`) count the error responses by error codes.
  234. <5> This `bucket_script` calculates the duration of the `clientip` access based
  235. on the results of the aggregation.
  236. After you create the {transform}, you must start it:
  237. [source,console]
  238. ----------------------------------
  239. POST _transform/suspicious_client_ips/_start
  240. ----------------------------------
  241. // TEST[skip:setup kibana sample data]
  242. Shortly thereafter, the first results should be available in the destination
  243. index:
  244. [source,console]
  245. ----------------------------------
  246. GET sample_weblogs_by_clientip/_search
  247. ----------------------------------
  248. // TEST[skip:setup kibana sample data]
  249. The search result shows you data like this for each client IP:
  250. [source,js]
  251. ----------------------------------
  252. "hits" : [
  253. {
  254. "_index" : "sample_weblogs_by_clientip",
  255. "_id" : "MOeHH_cUL5urmartKj-b5UQAAAAAAAAA",
  256. "_score" : 1.0,
  257. "_source" : {
  258. "geo" : {
  259. "src_dc" : 2.0,
  260. "dest_dc" : 2.0
  261. },
  262. "success" : 2,
  263. "error404" : 0,
  264. "error503" : 0,
  265. "clientip" : "0.72.176.46",
  266. "agent_dc" : 2.0,
  267. "bytes_sum" : 4422.0,
  268. "responses" : {
  269. "total" : 2.0
  270. },
  271. "url_dc" : 2.0,
  272. "timestamp" : {
  273. "duration_ms" : 5.2191698E8,
  274. "min" : "2020-03-16T07:51:57.333Z",
  275. "max" : "2020-03-22T08:50:34.313Z"
  276. }
  277. }
  278. }
  279. ]
  280. ----------------------------------
  281. // NOTCONSOLE
  282. NOTE: Like other Kibana sample data sets, the web log sample dataset contains
  283. timestamps relative to when you installed it, including timestamps in the
  284. future. The {ctransform} will pick up the data points once they are in the past.
  285. If you installed the web log sample dataset some time ago, you can uninstall and
  286. reinstall it and the timestamps will change.
  287. This {transform} makes it easier to answer questions such as:
  288. * Which client IPs are transferring the most amounts of data?
  289. * Which client IPs are interacting with a high number of different URLs?
  290. * Which client IPs have high error rates?
  291. * Which client IPs are interacting with a high number of destination countries?
  292. [[example-last-log]]
  293. == Finding the last log event for each IP address
  294. This example uses the web log sample data set to find the last log from an IP
  295. address. Let's use the `latest` type of {transform} in continuous mode. It
  296. copies the most recent document for each unique key from the source index to the destination index
  297. and updates the destination index as new data comes into the source index.
  298. Pick the `clientip` field as the unique key; the data is grouped by this field.
  299. Select `timestamp` as the date field that sorts the data chronologically. For
  300. continuous mode, specify a date field that is used to identify new documents,
  301. and an interval between checks for changes in the source index.
  302. Let's assume that we're interested in retaining documents only for IP addresses that appeared recently in the log. You can define a retention policy and specify a date field that is used to calculate
  303. the age of a document. This example uses the same date field that is used to
  304. sort the data. Then set the maximum age of a document; documents that are older
  305. than the value you set will be removed from the destination index.
  306. This {transform} creates the destination index that contains the latest login
  307. date for each client IP. As the {transform} runs in continuous mode, the
  308. destination index will be updated as new data that comes into the source index.
  309. Finally, every document that is older than 30 days will be removed from the
  310. destination index due to the applied retention policy.
  311. [source,console]
  312. ----------------------------------
  313. PUT _transform/last-log-from-clientip
  314. {
  315. "source": {
  316. "index": [
  317. "kibana_sample_data_logs"
  318. ]
  319. },
  320. "latest": {
  321. "unique_key": [ <1>
  322. "clientip"
  323. ],
  324. "sort": "timestamp" <2>
  325. },
  326. "frequency": "1m", <3>
  327. "dest": {
  328. "index": "last-log-from-clientip"
  329. },
  330. "sync": { <4>
  331. "time": {
  332. "field": "timestamp",
  333. "delay": "60s"
  334. }
  335. },
  336. "retention_policy": { <5>
  337. "time": {
  338. "field": "timestamp",
  339. "max_age": "30d"
  340. }
  341. },
  342. "settings": {
  343. "max_page_search_size": 500
  344. }
  345. }
  346. ----------------------------------
  347. // TEST[skip:setup kibana sample data]
  348. <1> Specifies the field for grouping the data.
  349. <2> Specifies the date field that is used for sorting the data.
  350. <3> Sets the interval for the {transform} to check for changes in the source
  351. index.
  352. <4> Contains the time field and delay settings used to synchronize the source
  353. and destination indices.
  354. <5> Specifies the retention policy for the transform. Documents that are older
  355. than the configured value will be removed from the destination index.
  356. After you create the {transform}, start it:
  357. [source,console]
  358. ----------------------------------
  359. POST _transform/last-log-from-clientip/_start
  360. ----------------------------------
  361. // TEST[skip:setup kibana sample data]
  362. After the {transform} processes the data, search the destination index:
  363. [source,console]
  364. ----------------------------------
  365. GET last-log-from-clientip/_search
  366. ----------------------------------
  367. // TEST[skip:setup kibana sample data]
  368. The search result shows you data like this for each client IP:
  369. [source,js]
  370. ----------------------------------
  371. {
  372. "_index" : "last-log-from-clientip",
  373. "_id" : "MOeHH_cUL5urmartKj-b5UQAAAAAAAAA",
  374. "_score" : 1.0,
  375. "_source" : {
  376. "referer" : "http://twitter.com/error/don-lind",
  377. "request" : "/elasticsearch",
  378. "agent" : "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)",
  379. "extension" : "",
  380. "memory" : null,
  381. "ip" : "0.72.176.46",
  382. "index" : "kibana_sample_data_logs",
  383. "message" : "0.72.176.46 - - [2018-09-18T06:31:00.572Z] \"GET /elasticsearch HTTP/1.1\" 200 7065 \"-\" \"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)\"",
  384. "url" : "https://www.elastic.co/downloads/elasticsearch",
  385. "tags" : [
  386. "success",
  387. "info"
  388. ],
  389. "geo" : {
  390. "srcdest" : "IN:PH",
  391. "src" : "IN",
  392. "coordinates" : {
  393. "lon" : -124.1127917,
  394. "lat" : 40.80338889
  395. },
  396. "dest" : "PH"
  397. },
  398. "utc_time" : "2021-05-04T06:31:00.572Z",
  399. "bytes" : 7065,
  400. "machine" : {
  401. "os" : "ios",
  402. "ram" : 12884901888
  403. },
  404. "response" : 200,
  405. "clientip" : "0.72.176.46",
  406. "host" : "www.elastic.co",
  407. "event" : {
  408. "dataset" : "sample_web_logs"
  409. },
  410. "phpmemory" : null,
  411. "timestamp" : "2021-05-04T06:31:00.572Z"
  412. }
  413. }
  414. ----------------------------------
  415. // NOTCONSOLE
  416. This {transform} makes it easier to answer questions such as:
  417. * What was the most recent log event associated with a specific IP address?