ml-configuring-transform.asciidoc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. [role="xpack"]
  2. [[ml-configuring-transform]]
  3. = Altering data in your {dfeed} with runtime fields
  4. If you use {dfeeds}, you can use runtime fields to alter your data before it
  5. is analyzed. You can add an optional `runtime_mappings` property to your
  6. {dfeeds}, where you can specify field types and scripts that evaluate custom
  7. expressions without affecting the indices that you're retrieving the data from.
  8. If your {dfeed} defines runtime fields, you can use those fields in your
  9. {anomaly-job}. For example, you can use the runtime fields in the analysis
  10. functions in one or more detectors. Runtime fields can impact search performance
  11. based on the computation defined in the runtime script.
  12. [NOTE]
  13. ===============================
  14. Some of these examples use regular expressions. By default, regular
  15. expressions are disabled because they circumvent the protection that Painless
  16. provides against long running and memory hungry scripts. For more information,
  17. see {ref}/modules-scripting-painless.html[Painless scripting language].
  18. {ml-cap} analysis is case sensitive. For example, "John" is considered to be
  19. different than "john". This is one reason you might consider using scripts that
  20. convert your strings to upper or lowercase letters.
  21. ===============================
  22. * <<ml-configuring-transform1>>
  23. * <<ml-configuring-transform2>>
  24. * <<ml-configuring-transform3>>
  25. * <<ml-configuring-transform4>>
  26. * <<ml-configuring-transform5>>
  27. * <<ml-configuring-transform6>>
  28. * <<ml-configuring-transform7>>
  29. * <<ml-configuring-transform8>>
  30. // * <<ml-configuring-transform9>>
  31. The following index APIs create and add content to an index that is used in
  32. subsequent examples:
  33. [source,console]
  34. ----------------------------------
  35. PUT /my-index-000001
  36. {
  37. "mappings":{
  38. "properties": {
  39. "@timestamp": { "type": "date" },
  40. "aborted_count": { "type": "long" },
  41. "another_field": { "type": "keyword" }, <1>
  42. "clientip": { "type": "keyword" },
  43. "coords": {
  44. "properties": {
  45. "lat": { "type": "keyword" },
  46. "lon": { "type": "keyword" }
  47. }
  48. },
  49. "error_count": { "type": "long" },
  50. "query": { "type": "keyword" },
  51. "some_field": { "type": "keyword" },
  52. "tokenstring1":{ "type":"keyword" },
  53. "tokenstring2":{ "type":"keyword" },
  54. "tokenstring3":{ "type":"keyword" }
  55. }
  56. }
  57. }
  58. PUT /my-index-000001/_doc/1
  59. {
  60. "@timestamp":"2017-03-23T13:00:00",
  61. "error_count":36320,
  62. "aborted_count":4156,
  63. "some_field":"JOE",
  64. "another_field":"SMITH ",
  65. "tokenstring1":"foo-bar-baz",
  66. "tokenstring2":"foo bar baz",
  67. "tokenstring3":"foo-bar-19",
  68. "query":"www.ml.elastic.co",
  69. "clientip":"123.456.78.900",
  70. "coords": {
  71. "lat" : 41.44,
  72. "lon":90.5
  73. }
  74. }
  75. ----------------------------------
  76. // TEST[skip:SETUP]
  77. <1> In this example, string fields are mapped as `keyword` fields to support
  78. aggregation. If you want both a full text (`text`) and a keyword (`keyword`)
  79. version of the same field, use multi-fields. For more information, see
  80. {ref}/multi-fields.html[fields].
  81. [[ml-configuring-transform1]]
  82. .Example 1: Adding two numerical fields
  83. [source,console]
  84. ----------------------------------
  85. PUT _ml/anomaly_detectors/test1
  86. {
  87. "analysis_config":{
  88. "bucket_span": "10m",
  89. "detectors":[
  90. {
  91. "function":"mean",
  92. "field_name": "total_error_count" <1>
  93. }
  94. ]
  95. },
  96. "data_description": {
  97. "time_field":"@timestamp"
  98. }
  99. }
  100. PUT _ml/datafeeds/datafeed-test1
  101. {
  102. "job_id": "test1",
  103. "indices": [
  104. "my-index-000001"
  105. ],
  106. "runtime_mappings": {
  107. "total_error_count": { <2>
  108. "type": "long",
  109. "script": {
  110. "source": "emit(doc['error_count'].value + doc['aborted_count'].value)"
  111. }
  112. }
  113. }
  114. }
  115. ----------------------------------
  116. // TEST[skip:needs-licence]
  117. <1> A runtime field named `total_error_count` is referenced in the detector
  118. within the job.
  119. <2> The runtime field is defined in the {dfeed}.
  120. This `test1` {anomaly-job} contains a detector that uses a runtime field in a
  121. mean analysis function. The `datafeed-test1` {dfeed} defines the runtime field.
  122. It contains a script that adds two fields in the document to produce a "total"
  123. error count.
  124. The syntax for the `runtime_mappings` property is identical to that used by
  125. {es}. For more information, see {ref}/runtime.html[Runtime fields].
  126. You can preview the contents of the {dfeed} by using the following API:
  127. [source,console]
  128. ----------------------------------
  129. GET _ml/datafeeds/datafeed-test1/_preview
  130. ----------------------------------
  131. // TEST[skip:continued]
  132. In this example, the API returns the following results, which contain a sum of
  133. the `error_count` and `aborted_count` values:
  134. [source,js]
  135. ----------------------------------
  136. [
  137. {
  138. "@timestamp": 1490274000000,
  139. "total_error_count": 40476
  140. }
  141. ]
  142. ----------------------------------
  143. NOTE: This example demonstrates how to use runtime fields, but it contains
  144. insufficient data to generate meaningful results.
  145. //For a full demonstration of
  146. //how to create jobs with sample data, see <<ml-getting-started>>.
  147. You can alternatively use {kib} to create an advanced {anomaly-job} that uses
  148. runtime fields. To add the `runtime_mappings` property to your {dfeed}, you must
  149. use the **Edit JSON** tab. For example:
  150. [role="screenshot"]
  151. image::images/ml-runtimefields.jpg[Using runtime_mappings in {dfeed} config via {kib}]
  152. [[ml-configuring-transform2]]
  153. .Example 2: Concatenating strings
  154. [source,console]
  155. --------------------------------------------------
  156. PUT _ml/anomaly_detectors/test2
  157. {
  158. "analysis_config":{
  159. "bucket_span": "10m",
  160. "detectors":[
  161. {
  162. "function":"low_info_content",
  163. "field_name":"my_runtime_field" <1>
  164. }
  165. ]
  166. },
  167. "data_description": {
  168. "time_field":"@timestamp"
  169. }
  170. }
  171. PUT _ml/datafeeds/datafeed-test2
  172. {
  173. "job_id": "test2",
  174. "indices": ["my-index-000001"],
  175. "runtime_mappings": {
  176. "my_runtime_field": {
  177. "type": "keyword",
  178. "script": {
  179. "source": "emit(doc['some_field'].value + '_' + doc['another_field'].value)" <2>
  180. }
  181. }
  182. }
  183. }
  184. GET _ml/datafeeds/datafeed-test2/_preview
  185. --------------------------------------------------
  186. // TEST[skip:needs-licence]
  187. <1> The runtime field has a generic name in this case, since it is used for
  188. various tests in the examples.
  189. <2> The runtime field uses the plus (+) operator to concatenate strings.
  190. The preview {dfeed} API returns the following results, which show that "JOE"
  191. and "SMITH " have been concatenated and an underscore was added:
  192. [source,js]
  193. ----------------------------------
  194. [
  195. {
  196. "@timestamp": 1490274000000,
  197. "my_runtime_field": "JOE_SMITH "
  198. }
  199. ]
  200. ----------------------------------
  201. [[ml-configuring-transform3]]
  202. .Example 3: Trimming strings
  203. [source,console]
  204. --------------------------------------------------
  205. POST _ml/datafeeds/datafeed-test2/_update
  206. {
  207. "runtime_mappings": {
  208. "my_runtime_field": {
  209. "type": "keyword",
  210. "script": {
  211. "source": "emit(doc['another_field'].value.trim())" <1>
  212. }
  213. }
  214. }
  215. }
  216. GET _ml/datafeeds/datafeed-test2/_preview
  217. --------------------------------------------------
  218. // TEST[skip:continued]
  219. <1> This runtime field uses the `trim()` function to trim extra white space from
  220. a string.
  221. The preview {dfeed} API returns the following results, which show that "SMITH "
  222. has been trimmed to "SMITH":
  223. [source,js]
  224. ----------------------------------
  225. [
  226. {
  227. "@timestamp": 1490274000000,
  228. "my_script_field": "SMITH"
  229. }
  230. ]
  231. ----------------------------------
  232. [[ml-configuring-transform4]]
  233. .Example 4: Converting strings to lowercase
  234. [source,console]
  235. --------------------------------------------------
  236. POST _ml/datafeeds/datafeed-test2/_update
  237. {
  238. "runtime_mappings": {
  239. "my_runtime_field": {
  240. "type": "keyword",
  241. "script": {
  242. "source": "emit(doc['some_field'].value.toLowerCase())" <1>
  243. }
  244. }
  245. }
  246. }
  247. GET _ml/datafeeds/datafeed-test2/_preview
  248. --------------------------------------------------
  249. // TEST[skip:continued]
  250. <1> This runtime field uses the `toLowerCase` function to convert a string to
  251. all lowercase letters. Likewise, you can use the `toUpperCase{}` function to
  252. convert a string to uppercase letters.
  253. The preview {dfeed} API returns the following results, which show that "JOE"
  254. has been converted to "joe":
  255. [source,js]
  256. ----------------------------------
  257. [
  258. {
  259. "@timestamp": 1490274000000,
  260. "my_script_field": "joe"
  261. }
  262. ]
  263. ----------------------------------
  264. [[ml-configuring-transform5]]
  265. .Example 5: Converting strings to mixed case formats
  266. [source,console]
  267. --------------------------------------------------
  268. POST _ml/datafeeds/datafeed-test2/_update
  269. {
  270. "runtime_mappings": {
  271. "my_runtime_field": {
  272. "type": "keyword",
  273. "script": {
  274. "source": "emit(doc['some_field'].value.substring(0, 1).toUpperCase() + doc['some_field'].value.substring(1).toLowerCase())" <1>
  275. }
  276. }
  277. }
  278. }
  279. GET _ml/datafeeds/datafeed-test2/_preview
  280. --------------------------------------------------
  281. // TEST[skip:continued]
  282. <1> This runtime field is a more complicated example of case manipulation. It
  283. uses the `subString()` function to capitalize the first letter of a string and
  284. converts the remaining characters to lowercase.
  285. The preview {dfeed} API returns the following results, which show that "JOE" has
  286. been converted to "Joe":
  287. [source,js]
  288. ----------------------------------
  289. [
  290. {
  291. "@timestamp": 1490274000000,
  292. "my_script_field": "Joe"
  293. }
  294. ]
  295. ----------------------------------
  296. [[ml-configuring-transform6]]
  297. .Example 6: Replacing tokens
  298. [source,console]
  299. --------------------------------------------------
  300. POST _ml/datafeeds/datafeed-test2/_update
  301. {
  302. "runtime_mappings": {
  303. "my_runtime_field": {
  304. "type": "keyword",
  305. "script": {
  306. "source": "emit(/\\s/.matcher(doc['tokenstring2'].value).replaceAll('_'))" <1>
  307. }
  308. }
  309. }
  310. }
  311. GET _ml/datafeeds/datafeed-test2/_preview
  312. --------------------------------------------------
  313. // TEST[skip:continued]
  314. <1> This script uses regular expressions to replace white space with
  315. underscores.
  316. The preview {dfeed} API returns the following results, which show that "foo bar
  317. baz" has been converted to "foo_bar_baz":
  318. [source,js]
  319. ----------------------------------
  320. [
  321. {
  322. "@timestamp": 1490274000000,
  323. "my_script_field": "foo_bar_baz"
  324. }
  325. ]
  326. ----------------------------------
  327. [[ml-configuring-transform7]]
  328. .Example 7: Regular expression matching and concatenation
  329. [source,console]
  330. --------------------------------------------------
  331. POST _ml/datafeeds/datafeed-test2/_update
  332. {
  333. "runtime_mappings": {
  334. "my_runtime_field": {
  335. "type": "keyword",
  336. "script": {
  337. "source": "emit(def m = /(.*)-bar-([0-9][0-9])/.matcher(doc['tokenstring3'].value); return m.find() ? m.group(1) + '_' + m.group(2) : '';)" <1>
  338. }
  339. }
  340. }
  341. }
  342. GET _ml/datafeeds/datafeed-test2/_preview
  343. --------------------------------------------------
  344. // TEST[skip:continued]
  345. <1> This script looks for a specific regular expression pattern and emits the
  346. matched groups as a concatenated string. If no match is found, it emits an empty
  347. string.
  348. The preview {dfeed} API returns the following results, which show that
  349. "foo-bar-19" has been converted to "foo_19":
  350. [source,js]
  351. ----------------------------------
  352. [
  353. {
  354. "@timestamp": 1490274000000,
  355. "my_script_field": "foo_19"
  356. }
  357. ]
  358. ----------------------------------
  359. [[ml-configuring-transform8]]
  360. .Example 8: Transforming geopoint data
  361. [source,console]
  362. --------------------------------------------------
  363. PUT _ml/anomaly_detectors/test3
  364. {
  365. "analysis_config":{
  366. "bucket_span": "10m",
  367. "detectors":[
  368. {
  369. "function":"lat_long",
  370. "field_name": "my_coordinates"
  371. }
  372. ]
  373. },
  374. "data_description": {
  375. "time_field":"@timestamp"
  376. }
  377. }
  378. PUT _ml/datafeeds/datafeed-test3
  379. {
  380. "job_id": "test3",
  381. "indices": ["my-index-000001"],
  382. "runtime_mappings": {
  383. "my_coordinates": {
  384. "type": "keyword",
  385. "script": {
  386. "source": "emit(doc['coords.lat'].value + ',' + doc['coords.lon'].value)"
  387. }
  388. }
  389. }
  390. }
  391. GET _ml/datafeeds/datafeed-test3/_preview
  392. --------------------------------------------------
  393. // TEST[skip:needs-licence]
  394. In {es}, location data can be stored in `geo_point` fields but this data type is
  395. not supported natively in {ml} analytics. This example of a runtime field
  396. transforms the data into an appropriate format. For more information,
  397. see <<ml-geo-functions>>.
  398. The preview {dfeed} API returns the following results, which show that
  399. `41.44` and `90.5` have been combined into "41.44,90.5":
  400. [source,js]
  401. ----------------------------------
  402. [
  403. {
  404. "@timestamp": 1490274000000,
  405. "my_coordinates": "41.44,90.5"
  406. }
  407. ]
  408. ----------------------------------
  409. ////
  410. [[ml-configuring-transform9]]
  411. .Example 9: Splitting strings by domain name
  412. [source,console]
  413. --------------------------------------------------
  414. PUT _ml/anomaly_detectors/test4
  415. {
  416. "description":"DNS tunneling",
  417. "analysis_config":{
  418. "bucket_span": "30m",
  419. "influencers": ["clientip","hrd"],
  420. "detectors":[
  421. {
  422. "function":"high_info_content",
  423. "field_name": "sub",
  424. "over_field_name": "hrd",
  425. "exclude_frequent":"all"
  426. }
  427. ]
  428. },
  429. "data_description": {
  430. "time_field":"@timestamp"
  431. }
  432. }
  433. PUT _ml/datafeeds/datafeed-test4
  434. {
  435. "job_id": "test4",
  436. "indices": ["my-index-000001"],
  437. "script_fields":{
  438. "sub":{
  439. "script":"return domainSplit(doc['query'].value).get(0);"
  440. },
  441. "hrd":{
  442. "script":"return domainSplit(doc['query'].value).get(1);"
  443. }
  444. }
  445. }
  446. GET _ml/datafeeds/datafeed-test4/_preview
  447. --------------------------------------------------
  448. // TEST[skip:needs-licence]
  449. If you have a single field that contains a well-formed DNS domain name, you can
  450. use the `domainSplit()` function to split the string into its highest registered
  451. domain and the sub-domain, which is everything to the left of the highest
  452. registered domain. For example, the highest registered domain of
  453. `www.ml.elastic.co` is `elastic.co` and the sub-domain is `www.ml`. The
  454. `domainSplit()` function returns an array of two values: the first value is the
  455. subdomain; the second value is the highest registered domain.
  456. The preview {dfeed} API returns the following results, which show that
  457. "www.ml.elastic.co" has been split into "elastic.co" and "www.ml":
  458. [source,js]
  459. ----------------------------------
  460. [
  461. {
  462. "@timestamp": 1490274000000,
  463. "clientip.keyword": "123.456.78.900",
  464. "hrd": "elastic.co",
  465. "sub": "www.ml"
  466. }
  467. ]
  468. ----------------------------------
  469. ////