ml-configuring-transform.asciidoc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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. "datafeed_config":{
  100. "datafeed_id": "datafeed-test1",
  101. "indices": ["my-index-000001"],
  102. "runtime_mappings": {
  103. "total_error_count": { <2>
  104. "type": "long",
  105. "script": {
  106. "source": "emit(doc['error_count'].value + doc['aborted_count'].value)"
  107. }
  108. }
  109. }
  110. }
  111. }
  112. ----------------------------------
  113. // TEST[skip:needs-licence]
  114. <1> A runtime field named `total_error_count` is referenced in the detector
  115. within the job.
  116. <2> The runtime field is defined in the {dfeed}.
  117. This `test1` {anomaly-job} contains a detector that uses a runtime field in a
  118. mean analysis function. The `datafeed-test1` {dfeed} defines the runtime field.
  119. It contains a script that adds two fields in the document to produce a "total"
  120. error count.
  121. The syntax for the `runtime_mappings` property is identical to that used by
  122. {es}. For more information, see {ref}/runtime.html[Runtime fields].
  123. You can preview the contents of the {dfeed} by using the following API:
  124. [source,console]
  125. ----------------------------------
  126. GET _ml/datafeeds/datafeed-test1/_preview
  127. ----------------------------------
  128. // TEST[skip:continued]
  129. In this example, the API returns the following results, which contain a sum of
  130. the `error_count` and `aborted_count` values:
  131. [source,js]
  132. ----------------------------------
  133. [
  134. {
  135. "@timestamp": 1490274000000,
  136. "total_error_count": 40476
  137. }
  138. ]
  139. ----------------------------------
  140. NOTE: This example demonstrates how to use runtime fields, but it contains
  141. insufficient data to generate meaningful results.
  142. //For a full demonstration of
  143. //how to create jobs with sample data, see <<ml-getting-started>>.
  144. You can alternatively use {kib} to create an advanced {anomaly-job} that uses
  145. runtime fields. To add the `runtime_mappings` property to your {dfeed}, you must
  146. use the **Edit JSON** tab. For example:
  147. [role="screenshot"]
  148. image::images/ml-runtimefields.jpg[Using runtime_mappings in {dfeed} config via {kib}]
  149. [[ml-configuring-transform2]]
  150. .Example 2: Concatenating strings
  151. [source,console]
  152. --------------------------------------------------
  153. PUT _ml/anomaly_detectors/test2
  154. {
  155. "analysis_config":{
  156. "bucket_span": "10m",
  157. "detectors":[
  158. {
  159. "function":"low_info_content",
  160. "field_name":"my_runtime_field" <1>
  161. }
  162. ]
  163. },
  164. "data_description": {
  165. "time_field":"@timestamp"
  166. },
  167. "datafeed_config":{
  168. "datafeed_id": "datafeed-test2",
  169. "indices": ["my-index-000001"],
  170. "runtime_mappings": {
  171. "my_runtime_field": {
  172. "type": "keyword",
  173. "script": {
  174. "source": "emit(doc['some_field'].value + '_' + doc['another_field'].value)" <2>
  175. }
  176. }
  177. }
  178. }
  179. }
  180. GET _ml/datafeeds/datafeed-test2/_preview
  181. --------------------------------------------------
  182. // TEST[skip:needs-licence]
  183. <1> The runtime field has a generic name in this case, since it is used for
  184. various tests in the examples.
  185. <2> The runtime field uses the plus (+) operator to concatenate strings.
  186. The preview {dfeed} API returns the following results, which show that "JOE"
  187. and "SMITH " have been concatenated and an underscore was added:
  188. [source,js]
  189. ----------------------------------
  190. [
  191. {
  192. "@timestamp": 1490274000000,
  193. "my_runtime_field": "JOE_SMITH "
  194. }
  195. ]
  196. ----------------------------------
  197. [[ml-configuring-transform3]]
  198. .Example 3: Trimming strings
  199. [source,console]
  200. --------------------------------------------------
  201. POST _ml/datafeeds/datafeed-test2/_update
  202. {
  203. "runtime_mappings": {
  204. "my_runtime_field": {
  205. "type": "keyword",
  206. "script": {
  207. "source": "emit(doc['another_field'].value.trim())" <1>
  208. }
  209. }
  210. }
  211. }
  212. GET _ml/datafeeds/datafeed-test2/_preview
  213. --------------------------------------------------
  214. // TEST[skip:continued]
  215. <1> This runtime field uses the `trim()` function to trim extra white space from
  216. a string.
  217. The preview {dfeed} API returns the following results, which show that "SMITH "
  218. has been trimmed to "SMITH":
  219. [source,js]
  220. ----------------------------------
  221. [
  222. {
  223. "@timestamp": 1490274000000,
  224. "my_script_field": "SMITH"
  225. }
  226. ]
  227. ----------------------------------
  228. [[ml-configuring-transform4]]
  229. .Example 4: Converting strings to lowercase
  230. [source,console]
  231. --------------------------------------------------
  232. POST _ml/datafeeds/datafeed-test2/_update
  233. {
  234. "runtime_mappings": {
  235. "my_runtime_field": {
  236. "type": "keyword",
  237. "script": {
  238. "source": "emit(doc['some_field'].value.toLowerCase())" <1>
  239. }
  240. }
  241. }
  242. }
  243. GET _ml/datafeeds/datafeed-test2/_preview
  244. --------------------------------------------------
  245. // TEST[skip:continued]
  246. <1> This runtime field uses the `toLowerCase` function to convert a string to
  247. all lowercase letters. Likewise, you can use the `toUpperCase{}` function to
  248. convert a string to uppercase letters.
  249. The preview {dfeed} API returns the following results, which show that "JOE"
  250. has been converted to "joe":
  251. [source,js]
  252. ----------------------------------
  253. [
  254. {
  255. "@timestamp": 1490274000000,
  256. "my_script_field": "joe"
  257. }
  258. ]
  259. ----------------------------------
  260. [[ml-configuring-transform5]]
  261. .Example 5: Converting strings to mixed case formats
  262. [source,console]
  263. --------------------------------------------------
  264. POST _ml/datafeeds/datafeed-test2/_update
  265. {
  266. "runtime_mappings": {
  267. "my_runtime_field": {
  268. "type": "keyword",
  269. "script": {
  270. "source": "emit(doc['some_field'].value.substring(0, 1).toUpperCase() + doc['some_field'].value.substring(1).toLowerCase())" <1>
  271. }
  272. }
  273. }
  274. }
  275. GET _ml/datafeeds/datafeed-test2/_preview
  276. --------------------------------------------------
  277. // TEST[skip:continued]
  278. <1> This runtime field is a more complicated example of case manipulation. It
  279. uses the `subString()` function to capitalize the first letter of a string and
  280. converts the remaining characters to lowercase.
  281. The preview {dfeed} API returns the following results, which show that "JOE" has
  282. been converted to "Joe":
  283. [source,js]
  284. ----------------------------------
  285. [
  286. {
  287. "@timestamp": 1490274000000,
  288. "my_script_field": "Joe"
  289. }
  290. ]
  291. ----------------------------------
  292. [[ml-configuring-transform6]]
  293. .Example 6: Replacing tokens
  294. [source,console]
  295. --------------------------------------------------
  296. POST _ml/datafeeds/datafeed-test2/_update
  297. {
  298. "runtime_mappings": {
  299. "my_runtime_field": {
  300. "type": "keyword",
  301. "script": {
  302. "source": "emit(/\\s/.matcher(doc['tokenstring2'].value).replaceAll('_'))" <1>
  303. }
  304. }
  305. }
  306. }
  307. GET _ml/datafeeds/datafeed-test2/_preview
  308. --------------------------------------------------
  309. // TEST[skip:continued]
  310. <1> This script uses regular expressions to replace white space with
  311. underscores.
  312. The preview {dfeed} API returns the following results, which show that "foo bar
  313. baz" has been converted to "foo_bar_baz":
  314. [source,js]
  315. ----------------------------------
  316. [
  317. {
  318. "@timestamp": 1490274000000,
  319. "my_script_field": "foo_bar_baz"
  320. }
  321. ]
  322. ----------------------------------
  323. [[ml-configuring-transform7]]
  324. .Example 7: Regular expression matching and concatenation
  325. [source,console]
  326. --------------------------------------------------
  327. POST _ml/datafeeds/datafeed-test2/_update
  328. {
  329. "runtime_mappings": {
  330. "my_runtime_field": {
  331. "type": "keyword",
  332. "script": {
  333. "source": "def m = /(.*)-bar-([0-9][0-9])/.matcher(doc['tokenstring3'].value); emit(m.find() ? m.group(1) + '_' + m.group(2) : '');" <1>
  334. }
  335. }
  336. }
  337. }
  338. GET _ml/datafeeds/datafeed-test2/_preview
  339. --------------------------------------------------
  340. // TEST[skip:continued]
  341. <1> This script looks for a specific regular expression pattern and emits the
  342. matched groups as a concatenated string. If no match is found, it emits an empty
  343. string.
  344. The preview {dfeed} API returns the following results, which show that
  345. "foo-bar-19" has been converted to "foo_19":
  346. [source,js]
  347. ----------------------------------
  348. [
  349. {
  350. "@timestamp": 1490274000000,
  351. "my_script_field": "foo_19"
  352. }
  353. ]
  354. ----------------------------------
  355. [[ml-configuring-transform8]]
  356. .Example 8: Transforming geopoint data
  357. [source,console]
  358. --------------------------------------------------
  359. PUT _ml/anomaly_detectors/test3
  360. {
  361. "analysis_config":{
  362. "bucket_span": "10m",
  363. "detectors":[
  364. {
  365. "function":"lat_long",
  366. "field_name": "my_coordinates"
  367. }
  368. ]
  369. },
  370. "data_description": {
  371. "time_field":"@timestamp"
  372. },
  373. "datafeed_config":{
  374. "datafeed_id": "datafeed-test3",
  375. "indices": ["my-index-000001"],
  376. "runtime_mappings": {
  377. "my_coordinates": {
  378. "type": "keyword",
  379. "script": {
  380. "source": "emit(doc['coords.lat'].value + ',' + doc['coords.lon'].value)"
  381. }
  382. }
  383. }
  384. }
  385. }
  386. GET _ml/datafeeds/datafeed-test3/_preview
  387. --------------------------------------------------
  388. // TEST[skip:needs-licence]
  389. In {es}, location data can be stored in `geo_point` fields but this data type is
  390. not supported natively in {ml} analytics. This example of a runtime field
  391. transforms the data into an appropriate format. For more information,
  392. see <<ml-geo-functions>>.
  393. The preview {dfeed} API returns the following results, which show that
  394. `41.44` and `90.5` have been combined into "41.44,90.5":
  395. [source,js]
  396. ----------------------------------
  397. [
  398. {
  399. "@timestamp": 1490274000000,
  400. "my_coordinates": "41.44,90.5"
  401. }
  402. ]
  403. ----------------------------------
  404. ////
  405. [[ml-configuring-transform9]]
  406. .Example 9: Splitting strings by domain name
  407. [source,console]
  408. --------------------------------------------------
  409. PUT _ml/anomaly_detectors/test4
  410. {
  411. "description":"DNS tunneling",
  412. "analysis_config":{
  413. "bucket_span": "30m",
  414. "influencers": ["clientip","hrd"],
  415. "detectors":[
  416. {
  417. "function":"high_info_content",
  418. "field_name": "sub",
  419. "over_field_name": "hrd",
  420. "exclude_frequent":"all"
  421. }
  422. ]
  423. },
  424. "data_description": {
  425. "time_field":"@timestamp"
  426. },
  427. "datafeed_config":{
  428. "datafeed_id": "datafeed-test4",
  429. "indices": ["my-index-000001"],
  430. "script_fields":{
  431. "sub":{
  432. "script":"return domainSplit(doc['query'].value).get(0);"
  433. },
  434. "hrd":{
  435. "script":"return domainSplit(doc['query'].value).get(1);"
  436. }
  437. }
  438. }
  439. }
  440. GET _ml/datafeeds/datafeed-test4/_preview
  441. --------------------------------------------------
  442. // TEST[skip:needs-licence]
  443. If you have a single field that contains a well-formed DNS domain name, you can
  444. use the `domainSplit()` function to split the string into its highest registered
  445. domain and the sub-domain, which is everything to the left of the highest
  446. registered domain. For example, the highest registered domain of
  447. `www.ml.elastic.co` is `elastic.co` and the sub-domain is `www.ml`. The
  448. `domainSplit()` function returns an array of two values: the first value is the
  449. subdomain; the second value is the highest registered domain.
  450. The preview {dfeed} API returns the following results, which show that
  451. "www.ml.elastic.co" has been split into "elastic.co" and "www.ml":
  452. [source,js]
  453. ----------------------------------
  454. [
  455. {
  456. "@timestamp": 1490274000000,
  457. "clientip.keyword": "123.456.78.900",
  458. "hrd": "elastic.co",
  459. "sub": "www.ml"
  460. }
  461. ]
  462. ----------------------------------
  463. ////