examples.asciidoc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  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-bytes>>
  17. * <<example-customer-names>>
  18. [[example-best-customers]]
  19. == Finding your best customers
  20. This example uses the eCommerce orders sample data set to find the customers who
  21. spent the most in a hypothetical webshop. Let's transform the data such that the
  22. destination index contains the number of orders, the total price of the orders,
  23. the amount of unique products and the average price per order, and the total
  24. amount of ordered products for each customer.
  25. [source,console]
  26. ----------------------------------
  27. POST _transform/_preview
  28. {
  29. "source": {
  30. "index": "kibana_sample_data_ecommerce"
  31. },
  32. "dest" : { <1>
  33. "index" : "sample_ecommerce_orders_by_customer"
  34. },
  35. "pivot": {
  36. "group_by": { <2>
  37. "user": { "terms": { "field": "user" }},
  38. "customer_id": { "terms": { "field": "customer_id" }}
  39. },
  40. "aggregations": {
  41. "order_count": { "value_count": { "field": "order_id" }},
  42. "total_order_amt": { "sum": { "field": "taxful_total_price" }},
  43. "avg_amt_per_order": { "avg": { "field": "taxful_total_price" }},
  44. "avg_unique_products_per_order": { "avg": { "field": "total_unique_products" }},
  45. "total_unique_products": { "cardinality": { "field": "products.product_id" }}
  46. }
  47. }
  48. }
  49. ----------------------------------
  50. // TEST[skip:setup kibana sample data]
  51. <1> The destination index for the {transform}. It is ignored by `_preview`.
  52. <2> Two `group_by` fields is selected. This means the {transform} contains a
  53. unique row per `user` and `customer_id` combination. Within this data set, both
  54. these fields are unique. By including both in the {transform}, it gives more
  55. context to the final results.
  56. NOTE: In the example above, condensed JSON formatting is used for easier
  57. readability of the pivot object.
  58. The preview {transforms} API enables you to see the layout of the {transform} in
  59. advance, populated with some sample values. For example:
  60. [source,js]
  61. ----------------------------------
  62. {
  63. "preview" : [
  64. {
  65. "total_order_amt" : 3946.9765625,
  66. "order_count" : 59.0,
  67. "total_unique_products" : 116.0,
  68. "avg_unique_products_per_order" : 2.0,
  69. "customer_id" : "10",
  70. "user" : "recip",
  71. "avg_amt_per_order" : 66.89790783898304
  72. },
  73. ...
  74. ]
  75. }
  76. ----------------------------------
  77. // NOTCONSOLE
  78. This {transform} makes it easier to answer questions such as:
  79. * Which customers spend the most?
  80. * Which customers spend the most per order?
  81. * Which customers order most often?
  82. * Which customers ordered the least number of different products?
  83. It's possible to answer these questions using aggregations alone, however
  84. {transforms} allow us to persist this data as a customer centric index. This
  85. enables us to analyze data at scale and gives more flexibility to explore and
  86. navigate data from a customer centric perspective. In some cases, it can even
  87. make creating visualizations much simpler.
  88. [[example-airline]]
  89. == Finding air carriers with the most delays
  90. This example uses the Flights sample data set to find out which air carrier
  91. had the most delays. First, filter the source data such that it excludes all
  92. the cancelled flights by using a query filter. Then transform the data to
  93. contain the distinct number of flights, the sum of delayed minutes, and the sum
  94. of the flight minutes by air carrier. Finally, use a
  95. <<search-aggregations-pipeline-bucket-script-aggregation,`bucket_script`>>
  96. to determine what percentage of the flight time was actually delay.
  97. [source,console]
  98. ----------------------------------
  99. POST _transform/_preview
  100. {
  101. "source": {
  102. "index": "kibana_sample_data_flights",
  103. "query": { <1>
  104. "bool": {
  105. "filter": [
  106. { "term": { "Cancelled": false } }
  107. ]
  108. }
  109. }
  110. },
  111. "dest" : { <2>
  112. "index" : "sample_flight_delays_by_carrier"
  113. },
  114. "pivot": {
  115. "group_by": { <3>
  116. "carrier": { "terms": { "field": "Carrier" }}
  117. },
  118. "aggregations": {
  119. "flights_count": { "value_count": { "field": "FlightNum" }},
  120. "delay_mins_total": { "sum": { "field": "FlightDelayMin" }},
  121. "flight_mins_total": { "sum": { "field": "FlightTimeMin" }},
  122. "delay_time_percentage": { <4>
  123. "bucket_script": {
  124. "buckets_path": {
  125. "delay_time": "delay_mins_total.value",
  126. "flight_time": "flight_mins_total.value"
  127. },
  128. "script": "(params.delay_time / params.flight_time) * 100"
  129. }
  130. }
  131. }
  132. }
  133. }
  134. ----------------------------------
  135. // TEST[skip:setup kibana sample data]
  136. <1> Filter the source data to select only flights that are not cancelled.
  137. <2> The destination index for the {transform}. It is ignored by `_preview`.
  138. <3> The data is grouped by the `Carrier` field which contains the airline name.
  139. <4> This `bucket_script` performs calculations on the results that are returned
  140. by the aggregation. In this particular example, it calculates what percentage of
  141. travel time was taken up by delays.
  142. The preview shows you that the new index would contain data like this for each
  143. carrier:
  144. [source,js]
  145. ----------------------------------
  146. {
  147. "preview" : [
  148. {
  149. "carrier" : "ES-Air",
  150. "flights_count" : 2802.0,
  151. "flight_mins_total" : 1436927.5130677223,
  152. "delay_time_percentage" : 9.335543983955839,
  153. "delay_mins_total" : 134145.0
  154. },
  155. ...
  156. ]
  157. }
  158. ----------------------------------
  159. // NOTCONSOLE
  160. This {transform} makes it easier to answer questions such as:
  161. * Which air carrier has the most delays as a percentage of flight time?
  162. NOTE: This data is fictional and does not reflect actual delays or flight stats
  163. for any of the featured destination or origin airports.
  164. [[example-clientips]]
  165. == Finding suspicious client IPs
  166. This example uses the web log sample data set to identify suspicious client IPs.
  167. It transform the data such that the new index contains the sum of bytes and the
  168. number of distinct URLs, agents, incoming requests by location, and geographic
  169. destinations for each client IP. It also uses filter aggregations to count the
  170. specific types of HTTP responses that each client IP receives. Ultimately, the
  171. example below transforms web log data into an entity centric index where the
  172. entity is `clientip`.
  173. [source,console]
  174. ----------------------------------
  175. PUT _transform/suspicious_client_ips
  176. {
  177. "source": {
  178. "index": "kibana_sample_data_logs"
  179. },
  180. "dest" : { <1>
  181. "index" : "sample_weblogs_by_clientip"
  182. },
  183. "sync" : { <2>
  184. "time": {
  185. "field": "timestamp",
  186. "delay": "60s"
  187. }
  188. },
  189. "pivot": {
  190. "group_by": { <3>
  191. "clientip": { "terms": { "field": "clientip" } }
  192. },
  193. "aggregations": {
  194. "url_dc": { "cardinality": { "field": "url.keyword" }},
  195. "bytes_sum": { "sum": { "field": "bytes" }},
  196. "geo.src_dc": { "cardinality": { "field": "geo.src" }},
  197. "agent_dc": { "cardinality": { "field": "agent.keyword" }},
  198. "geo.dest_dc": { "cardinality": { "field": "geo.dest" }},
  199. "responses.total": { "value_count": { "field": "timestamp" }},
  200. "success" : { <4>
  201. "filter": {
  202. "term": { "response" : "200"}}
  203. },
  204. "error404" : {
  205. "filter": {
  206. "term": { "response" : "404"}}
  207. },
  208. "error503" : {
  209. "filter": {
  210. "term": { "response" : "503"}}
  211. },
  212. "timestamp.min": { "min": { "field": "timestamp" }},
  213. "timestamp.max": { "max": { "field": "timestamp" }},
  214. "timestamp.duration_ms": { <5>
  215. "bucket_script": {
  216. "buckets_path": {
  217. "min_time": "timestamp.min.value",
  218. "max_time": "timestamp.max.value"
  219. },
  220. "script": "(params.max_time - params.min_time)"
  221. }
  222. }
  223. }
  224. }
  225. }
  226. ----------------------------------
  227. // TEST[skip:setup kibana sample data]
  228. <1> The destination index for the {transform}.
  229. <2> Configures the {transform} to run continuously. It uses the `timestamp`
  230. field to synchronize the source and destination indices. The worst case
  231. ingestion delay is 60 seconds.
  232. <3> The data is grouped by the `clientip` field.
  233. <4> Filter aggregation that counts the occurrences of successful (`200`)
  234. responses in the `response` field. The following two aggregations (`error404`
  235. and `error503`) count the error responses by error codes.
  236. <5> This `bucket_script` calculates the duration of the `clientip` access based
  237. on the results of the aggregation.
  238. After you create the {transform}, you must start it:
  239. [source,console]
  240. ----------------------------------
  241. POST _transform/suspicious_client_ips/_start
  242. ----------------------------------
  243. // TEST[skip:setup kibana sample data]
  244. Shortly thereafter, the first results should be available in the destination
  245. index:
  246. [source,console]
  247. ----------------------------------
  248. GET sample_weblogs_by_clientip/_search
  249. ----------------------------------
  250. // TEST[skip:setup kibana sample data]
  251. The search result shows you data like this for each client IP:
  252. [source,js]
  253. ----------------------------------
  254. "hits" : [
  255. {
  256. "_index" : "sample_weblogs_by_clientip",
  257. "_id" : "MOeHH_cUL5urmartKj-b5UQAAAAAAAAA",
  258. "_score" : 1.0,
  259. "_source" : {
  260. "geo" : {
  261. "src_dc" : 2.0,
  262. "dest_dc" : 2.0
  263. },
  264. "success" : 2,
  265. "error404" : 0,
  266. "error503" : 0,
  267. "clientip" : "0.72.176.46",
  268. "agent_dc" : 2.0,
  269. "bytes_sum" : 4422.0,
  270. "responses" : {
  271. "total" : 2.0
  272. },
  273. "url_dc" : 2.0,
  274. "timestamp" : {
  275. "duration_ms" : 5.2191698E8,
  276. "min" : "2020-03-16T07:51:57.333Z",
  277. "max" : "2020-03-22T08:50:34.313Z"
  278. }
  279. }
  280. }
  281. ]
  282. ----------------------------------
  283. // NOTCONSOLE
  284. NOTE: Like other Kibana sample data sets, the web log sample dataset contains
  285. timestamps relative to when you installed it, including timestamps in the
  286. future. The {ctransform} will pick up the data points once they are in the past.
  287. If you installed the web log sample dataset some time ago, you can uninstall and
  288. reinstall it and the timestamps will change.
  289. This {transform} makes it easier to answer questions such as:
  290. * Which client IPs are transferring the most amounts of data?
  291. * Which client IPs are interacting with a high number of different URLs?
  292. * Which client IPs have high error rates?
  293. * Which client IPs are interacting with a high number of destination countries?
  294. [[example-last-log]]
  295. == Finding the last log event for each IP address
  296. This example uses the web log sample data set to find the last log from an IP
  297. address. Let's use the `latest` type of {transform} in continuous mode. It
  298. copies the most recent document for each unique key from the source index to the
  299. destination index and updates the destination index as new data comes into the
  300. source index.
  301. Pick the `clientip` field as the unique key; the data is grouped by this field.
  302. Select `timestamp` as the date field that sorts the data chronologically. For
  303. continuous mode, specify a date field that is used to identify new documents,
  304. and an interval between checks for changes in the source index.
  305. Let's assume that we're interested in retaining documents only for IP addresses
  306. that appeared recently in the log. You can define a retention policy and specify
  307. a date field that is used to calculate the age of a document. This example uses
  308. the same date field that is used to sort the data. Then set the maximum age of a
  309. document; documents that are older than the value you set will be removed from
  310. the destination index.
  311. This {transform} creates the destination index that contains the latest login
  312. date for each client IP. As the {transform} runs in continuous mode, the
  313. destination index will be updated as new data that comes into the source index.
  314. Finally, every document that is older than 30 days will be removed from the
  315. destination index due to the applied retention policy.
  316. [source,console]
  317. ----------------------------------
  318. PUT _transform/last-log-from-clientip
  319. {
  320. "source": {
  321. "index": [
  322. "kibana_sample_data_logs"
  323. ]
  324. },
  325. "latest": {
  326. "unique_key": [ <1>
  327. "clientip"
  328. ],
  329. "sort": "timestamp" <2>
  330. },
  331. "frequency": "1m", <3>
  332. "dest": {
  333. "index": "last-log-from-clientip"
  334. },
  335. "sync": { <4>
  336. "time": {
  337. "field": "timestamp",
  338. "delay": "60s"
  339. }
  340. },
  341. "retention_policy": { <5>
  342. "time": {
  343. "field": "timestamp",
  344. "max_age": "30d"
  345. }
  346. },
  347. "settings": {
  348. "max_page_search_size": 500
  349. }
  350. }
  351. ----------------------------------
  352. // TEST[skip:setup kibana sample data]
  353. <1> Specifies the field for grouping the data.
  354. <2> Specifies the date field that is used for sorting the data.
  355. <3> Sets the interval for the {transform} to check for changes in the source
  356. index.
  357. <4> Contains the time field and delay settings used to synchronize the source
  358. and destination indices.
  359. <5> Specifies the retention policy for the transform. Documents that are older
  360. than the configured value will be removed from the destination index.
  361. After you create the {transform}, start it:
  362. [source,console]
  363. ----------------------------------
  364. POST _transform/last-log-from-clientip/_start
  365. ----------------------------------
  366. // TEST[skip:setup kibana sample data]
  367. After the {transform} processes the data, search the destination index:
  368. [source,console]
  369. ----------------------------------
  370. GET last-log-from-clientip/_search
  371. ----------------------------------
  372. // TEST[skip:setup kibana sample data]
  373. The search result shows you data like this for each client IP:
  374. [source,js]
  375. ----------------------------------
  376. {
  377. "_index" : "last-log-from-clientip",
  378. "_id" : "MOeHH_cUL5urmartKj-b5UQAAAAAAAAA",
  379. "_score" : 1.0,
  380. "_source" : {
  381. "referer" : "http://twitter.com/error/don-lind",
  382. "request" : "/elasticsearch",
  383. "agent" : "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)",
  384. "extension" : "",
  385. "memory" : null,
  386. "ip" : "0.72.176.46",
  387. "index" : "kibana_sample_data_logs",
  388. "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)\"",
  389. "url" : "https://www.elastic.co/downloads/elasticsearch",
  390. "tags" : [
  391. "success",
  392. "info"
  393. ],
  394. "geo" : {
  395. "srcdest" : "IN:PH",
  396. "src" : "IN",
  397. "coordinates" : {
  398. "lon" : -124.1127917,
  399. "lat" : 40.80338889
  400. },
  401. "dest" : "PH"
  402. },
  403. "utc_time" : "2021-05-04T06:31:00.572Z",
  404. "bytes" : 7065,
  405. "machine" : {
  406. "os" : "ios",
  407. "ram" : 12884901888
  408. },
  409. "response" : 200,
  410. "clientip" : "0.72.176.46",
  411. "host" : "www.elastic.co",
  412. "event" : {
  413. "dataset" : "sample_web_logs"
  414. },
  415. "phpmemory" : null,
  416. "timestamp" : "2021-05-04T06:31:00.572Z"
  417. }
  418. }
  419. ----------------------------------
  420. // NOTCONSOLE
  421. This {transform} makes it easier to answer questions such as:
  422. * What was the most recent log event associated with a specific IP address?
  423. [[example-bytes]]
  424. == Finding client IPs that sent the most bytes to the server
  425. This example uses the web log sample data set to find the client IP that sent
  426. the most bytes to the server in every hour. The example uses a `pivot`
  427. {transform} with a <<search-aggregations-metrics-top-metrics,`top_metrics`>>
  428. aggregation.
  429. Group the data by a <<_date_histogram,date histogram>> on the time field with an
  430. interval of one hour. Use a
  431. <<search-aggregations-metrics-max-aggregation,max aggregation>> on the `bytes`
  432. field to get the maximum amount of data that is sent to the server. Without
  433. the `max` aggregation, the API call still returns the client IP that sent the
  434. most bytes, however, the amount of bytes that it sent is not returned. In the
  435. `top_metrics` property, specify `clientip` and `geo.src`, then sort them by the
  436. `bytes` field in descending order. The {transform} returns the client IP that
  437. sent the biggest amount of data and the 2-letter ISO code of the corresponding
  438. location.
  439. [source,console]
  440. ----------------------------------
  441. POST _transform/_preview
  442. {
  443. "source": {
  444. "index": "kibana_sample_data_logs"
  445. },
  446. "pivot": {
  447. "group_by": { <1>
  448. "timestamp": {
  449. "date_histogram": {
  450. "field": "timestamp",
  451. "fixed_interval": "1h"
  452. }
  453. }
  454. },
  455. "aggregations": {
  456. "bytes.max": { <2>
  457. "max": {
  458. "field": "bytes"
  459. }
  460. },
  461. "top": {
  462. "top_metrics": { <3>
  463. "metrics": [
  464. {
  465. "field": "clientip"
  466. },
  467. {
  468. "field": "geo.src"
  469. }
  470. ],
  471. "sort": {
  472. "bytes": "desc"
  473. }
  474. }
  475. }
  476. }
  477. }
  478. }
  479. ----------------------------------
  480. // TEST[skip:setup kibana sample data]
  481. <1> The data is grouped by a date histogram of the time field with a one hour
  482. interval.
  483. <2> Calculates the maximum value of the `bytes` field.
  484. <3> Specifies the fields (`clientip` and `geo.src`) of the top document to
  485. return and the sorting method (document with the highest `bytes` value).
  486. The API call above returns a response similar to this:
  487. [source,js]
  488. ----------------------------------
  489. {
  490. "preview" : [
  491. {
  492. "top" : {
  493. "clientip" : "223.87.60.27",
  494. "geo.src" : "IN"
  495. },
  496. "bytes" : {
  497. "max" : 6219
  498. },
  499. "timestamp" : "2021-04-25T00:00:00.000Z"
  500. },
  501. {
  502. "top" : {
  503. "clientip" : "99.74.118.237",
  504. "geo.src" : "LK"
  505. },
  506. "bytes" : {
  507. "max" : 14113
  508. },
  509. "timestamp" : "2021-04-25T03:00:00.000Z"
  510. },
  511. {
  512. "top" : {
  513. "clientip" : "218.148.135.12",
  514. "geo.src" : "BR"
  515. },
  516. "bytes" : {
  517. "max" : 4531
  518. },
  519. "timestamp" : "2021-04-25T04:00:00.000Z"
  520. },
  521. ...
  522. ]
  523. }
  524. ----------------------------------
  525. // NOTCONSOLE
  526. [[example-customer-names]]
  527. == Getting customer name and email address by customer ID
  528. This example uses the ecommerce sample data set to create an entity-centric
  529. index based on customer ID, and to get the customer name and email address by
  530. using the `top_metrics` aggregation.
  531. Group the data by `customer_id`, then add a `top_metrics` aggregation where the
  532. `metrics` are the `email`, the `customer_first_name.keyword`, and the
  533. `customer_last_name.keyword` fields. Sort the `top_metrics` by `order_date` in
  534. descending order. The API call looks like this:
  535. [source,console]
  536. ----------------------------------
  537. POST _transform/_preview
  538. {
  539. "source": {
  540. "index": "kibana_sample_data_ecommerce"
  541. },
  542. "pivot": {
  543. "group_by": { <1>
  544. "customer_id": {
  545. "terms": {
  546. "field": "customer_id"
  547. }
  548. }
  549. },
  550. "aggregations": {
  551. "last": {
  552. "top_metrics": { <2>
  553. "metrics": [
  554. {
  555. "field": "email"
  556. },
  557. {
  558. "field": "customer_first_name.keyword"
  559. },
  560. {
  561. "field": "customer_last_name.keyword"
  562. }
  563. ],
  564. "sort": {
  565. "order_date": "desc"
  566. }
  567. }
  568. }
  569. }
  570. }
  571. }
  572. ----------------------------------
  573. // TEST[skip:setup kibana sample data]
  574. <1> The data is grouped by a `terms` aggregation on the `customer_id` field.
  575. <2> Specifies the fields to return (email and name fields) in a descending order
  576. by the order date.
  577. The API returns a response that is similar to this:
  578. [source,js]
  579. ----------------------------------
  580. {
  581. "preview" : [
  582. {
  583. "last" : {
  584. "customer_last_name.keyword" : "Long",
  585. "customer_first_name.keyword" : "Recip",
  586. "email" : "recip@long-family.zzz"
  587. },
  588. "customer_id" : "10"
  589. },
  590. {
  591. "last" : {
  592. "customer_last_name.keyword" : "Jackson",
  593. "customer_first_name.keyword" : "Fitzgerald",
  594. "email" : "fitzgerald@jackson-family.zzz"
  595. },
  596. "customer_id" : "11"
  597. },
  598. {
  599. "last" : {
  600. "customer_last_name.keyword" : "Cross",
  601. "customer_first_name.keyword" : "Brigitte",
  602. "email" : "brigitte@cross-family.zzz"
  603. },
  604. "customer_id" : "12"
  605. },
  606. ...
  607. ]
  608. }
  609. ----------------------------------
  610. // NOTCONSOLE