put-dfanalytics.asciidoc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. [role="xpack"]
  2. [testenv="platinum"]
  3. [[put-dfanalytics]]
  4. === Create {dfanalytics-jobs} API
  5. [subs="attributes"]
  6. ++++
  7. <titleabbrev>Create {dfanalytics-jobs}</titleabbrev>
  8. ++++
  9. Instantiates a {dfanalytics-job}.
  10. experimental[]
  11. [[ml-put-dfanalytics-request]]
  12. ==== {api-request-title}
  13. `PUT _ml/data_frame/analytics/<data_frame_analytics_id>`
  14. [[ml-put-dfanalytics-prereq]]
  15. ==== {api-prereq-title}
  16. * You must have `machine_learning_admin` built-in role to use this API. You must
  17. also have `read` and `view_index_metadata` privileges on the source index and
  18. `read`, `create_index`, and `index` privileges on the destination index. For
  19. more information, see <<security-privileges>> and <<built-in-roles>>.
  20. [[ml-put-dfanalytics-desc]]
  21. ==== {api-description-title}
  22. This API creates a {dfanalytics-job} that performs an analysis on the source
  23. index and stores the outcome in a destination index.
  24. The destination index will be automatically created if it does not exist. The
  25. `index.number_of_shards` and `index.number_of_replicas` settings of the source
  26. index will be copied over the destination index. When the source index matches
  27. multiple indices, these settings will be set to the maximum values found in the
  28. source indices.
  29. The mappings of the source indices are also attempted to be copied over
  30. to the destination index, however, if the mappings of any of the fields don't
  31. match among the source indices, the attempt will fail with an error message.
  32. If the destination index already exists, then it will be use as is. This makes
  33. it possible to set up the destination index in advance with custom settings
  34. and mappings.
  35. [[ml-put-dfanalytics-supported-fields]]
  36. ===== Supported fields
  37. ====== {oldetection-cap}
  38. {oldetection-cap} requires numeric or boolean data to analyze. The algorithms
  39. don't support missing values therefore fields that have data types other than
  40. numeric or boolean are ignored. Documents where included fields contain missing
  41. values, null values, or an array are also ignored. Therefore the `dest` index
  42. may contain documents that don't have an {olscore}.
  43. ====== {regression-cap}
  44. {regression-cap} supports fields that are numeric, `boolean`, `text`, `keyword`,
  45. and `ip`. It is also tolerant of missing values. Fields that are supported are
  46. included in the analysis, other fields are ignored. Documents where included
  47. fields contain an array with two or more values are also ignored. Documents in
  48. the `dest` index that don’t contain a results field are not included in the
  49. {reganalysis}.
  50. ====== {classification-cap}
  51. {classification-cap} supports fields that are numeric, `boolean`, `text`,
  52. `keyword`, and `ip`. It is also tolerant of missing values. Fields that are
  53. supported are included in the analysis, other fields are ignored. Documents
  54. where included fields contain an array with two or more values are also ignored.
  55. Documents in the `dest` index that don’t contain a results field are not
  56. included in the {classanalysis}.
  57. {classanalysis-cap} can be improved by mapping ordinal variable values to a
  58. single number. For example, in case of age ranges, you can model the values as
  59. "0-14" = 0, "15-24" = 1, "25-34" = 2, and so on.
  60. [[ml-put-dfanalytics-path-params]]
  61. ==== {api-path-parms-title}
  62. `<data_frame_analytics_id>`::
  63. (Required, string) A numerical character string that uniquely identifies the
  64. {dfanalytics-job}. This identifier can contain lowercase alphanumeric
  65. characters (a-z and 0-9), hyphens, and underscores. It must start and end with
  66. alphanumeric characters.
  67. [[ml-put-dfanalytics-request-body]]
  68. ==== {api-request-body-title}
  69. `analysis`::
  70. (Required, object) Defines the type of {dfanalytics} you want to perform on
  71. your source index. For example: `outlier_detection`. See
  72. <<dfanalytics-types>>.
  73. `analyzed_fields`::
  74. (Optional, object) Specify `includes` and/or `excludes` patterns to select
  75. which fields will be included in the analysis. If `analyzed_fields` is not
  76. set, only the relevant fields will be included. For example, all the numeric
  77. fields for {oldetection}. For the supported field types, see
  78. <<ml-put-dfanalytics-supported-fields>>. Also see the <<explain-dfanalytics>>
  79. which helps understand field selection.
  80. `includes`:::
  81. (Optional, array) An array of strings that defines the fields that will be
  82. included in the analysis.
  83. `excludes`:::
  84. (Optional, array) An array of strings that defines the fields that will be
  85. excluded from the analysis. You do not need to add fields with unsupported
  86. data types to `excludes`, these fields are excluded from the analysis
  87. automatically.
  88. `description`::
  89. (Optional, string) A description of the job.
  90. `dest`::
  91. (Required, object) The destination configuration, consisting of `index` and
  92. optionally `results_field` (`ml` by default).
  93. `index`:::
  94. (Required, string) Defines the _destination index_ to store the results of
  95. the {dfanalytics-job}.
  96. `results_field`:::
  97. (Optional, string) Defines the name of the field in which to store the
  98. results of the analysis. Default to `ml`.
  99. `model_memory_limit`::
  100. (Optional, string) The approximate maximum amount of memory resources that are
  101. permitted for analytical processing. The default value for {dfanalytics-jobs}
  102. is `1gb`. If your `elasticsearch.yml` file contains an
  103. `xpack.ml.max_model_memory_limit` setting, an error occurs when you try to
  104. create {dfanalytics-jobs} that have `model_memory_limit` values greater than
  105. that setting. For more information, see <<ml-settings>>.
  106. `source`::
  107. (object) The configuration of how to source the analysis data. It requires an
  108. `index`. Optionally, `query` and `_source` may be specified.
  109. `index`:::
  110. (Required, string or array) Index or indices on which to perform the
  111. analysis. It can be a single index or index pattern as well as an array of
  112. indices or patterns.
  113. `query`:::
  114. (Optional, object) The {es} query domain-specific language
  115. (<<query-dsl,DSL>>). This value corresponds to the query object in an {es}
  116. search POST body. All the options that are supported by {es} can be used,
  117. as this object is passed verbatim to {es}. By default, this property has
  118. the following value: `{"match_all": {}}`.
  119. `_source`:::
  120. (Optional, object) Specify `includes` and/or `excludes` patterns to select
  121. which fields will be present in the destination. Fields that are excluded
  122. cannot be included in the analysis.
  123. `includes`::::
  124. (array) An array of strings that defines the fields that will be
  125. included in the destination.
  126. `excludes`::::
  127. (array) An array of strings that defines the fields that will be
  128. excluded from the destination.
  129. `allow_lazy_start`::
  130. (Optional, boolean) Whether this job should be allowed to start when there
  131. is insufficient {ml} node capacity for it to be immediately assigned to a node.
  132. The default is `false`, which means that the <<start-dfanalytics>>
  133. will return an error if a {ml} node with capacity to run the
  134. job cannot immediately be found. (However, this is also subject to
  135. the cluster-wide `xpack.ml.max_lazy_ml_nodes` setting - see
  136. <<advanced-ml-settings>>.) If this option is set to `true` then
  137. the <<start-dfanalytics>> will not return an error, and the job will
  138. wait in the `starting` state until sufficient {ml} node capacity
  139. is available.
  140. [[ml-put-dfanalytics-example]]
  141. ==== {api-examples-title}
  142. [[ml-put-dfanalytics-example-preprocess]]
  143. ===== Preprocessing actions example
  144. The following example shows how to limit the scope of the analysis to certain
  145. fields, specify excluded fields in the destination index, and use a query to
  146. filter your data before analysis.
  147. [source,console]
  148. --------------------------------------------------
  149. PUT _ml/data_frame/analytics/model-flight-delays-pre
  150. {
  151. "source": {
  152. "index": [
  153. "kibana_sample_data_flights" <1>
  154. ],
  155. "query": { <2>
  156. "range": {
  157. "DistanceKilometers": {
  158. "gt": 0
  159. }
  160. }
  161. },
  162. "_source": { <3>
  163. "includes": [],
  164. "excludes": [
  165. "FlightDelay",
  166. "FlightDelayType"
  167. ]
  168. }
  169. },
  170. "dest": { <4>
  171. "index": "df-flight-delays",
  172. "results_field": "ml-results"
  173. },
  174. "analysis": {
  175. "regression": {
  176. "dependent_variable": "FlightDelayMin",
  177. "training_percent": 90
  178. }
  179. },
  180. "analyzed_fields": { <5>
  181. "includes": [],
  182. "excludes": [
  183. "FlightNum"
  184. ]
  185. },
  186. "model_memory_limit": "100mb"
  187. }
  188. --------------------------------------------------
  189. // TEST[skip:setup kibana sample data]
  190. <1> The source index to analyze.
  191. <2> This query filters out entire documents that will not be present in the
  192. destination index.
  193. <3> The `_source` object defines fields in the dataset that will be included or
  194. excluded in the destination index. In this case, `includes` does not specify any
  195. fields, so the default behavior takes place: all the fields of the source index
  196. will included except the ones that are explicitly specified in `excludes`.
  197. <4> Defines the destination index that contains the results of the analysis and
  198. the fields of the source index specified in the `_source` object. Also defines
  199. the name of the `results_field`.
  200. <5> Specifies fields to be included in or excluded from the analysis. This does
  201. not affect whether the fields will be present in the destination index, only
  202. affects whether they are used in the analysis.
  203. In this example, we can see that all the fields of the source index are included
  204. in the destination index except `FlightDelay` and `FlightDelayType` because
  205. these are defined as excluded fields by the `excludes` parameter of the
  206. `_source` object. The `FlightNum` field is included in the destination index,
  207. however it is not included in the analysis because it is explicitly specified as
  208. excluded field by the `excludes` parameter of the `analyzed_fields` object.
  209. [[ml-put-dfanalytics-example-od]]
  210. ===== {oldetection-cap} example
  211. The following example creates the `loganalytics` {dfanalytics-job}, the analysis
  212. type is `outlier_detection`:
  213. [source,console]
  214. --------------------------------------------------
  215. PUT _ml/data_frame/analytics/loganalytics
  216. {
  217. "description": "Outlier detection on log data",
  218. "source": {
  219. "index": "logdata"
  220. },
  221. "dest": {
  222. "index": "logdata_out"
  223. },
  224. "analysis": {
  225. "outlier_detection": {
  226. "compute_feature_influence": true,
  227. "outlier_fraction": 0.05,
  228. "standardization_enabled": true
  229. }
  230. }
  231. }
  232. --------------------------------------------------
  233. // TEST[setup:setup_logdata]
  234. The API returns the following result:
  235. [source,console-result]
  236. ----
  237. {
  238. "id": "loganalytics",
  239. "description": "Outlier detection on log data",
  240. "source": {
  241. "index": ["logdata"],
  242. "query": {
  243. "match_all": {}
  244. }
  245. },
  246. "dest": {
  247. "index": "logdata_out",
  248. "results_field": "ml"
  249. },
  250. "analysis": {
  251. "outlier_detection": {
  252. "compute_feature_influence": true,
  253. "outlier_fraction": 0.05,
  254. "standardization_enabled": true
  255. }
  256. },
  257. "model_memory_limit": "1gb",
  258. "create_time" : 1562265491319,
  259. "version" : "8.0.0",
  260. "allow_lazy_start" : false
  261. }
  262. ----
  263. // TESTRESPONSE[s/1562265491319/$body.$_path/]
  264. // TESTRESPONSE[s/"version": "8.0.0"/"version": $body.version/]
  265. [[ml-put-dfanalytics-example-r]]
  266. ===== {regression-cap} examples
  267. The following example creates the `house_price_regression_analysis`
  268. {dfanalytics-job}, the analysis type is `regression`:
  269. [source,console]
  270. --------------------------------------------------
  271. PUT _ml/data_frame/analytics/house_price_regression_analysis
  272. {
  273. "source": {
  274. "index": "houses_sold_last_10_yrs"
  275. },
  276. "dest": {
  277. "index": "house_price_predictions"
  278. },
  279. "analysis":
  280. {
  281. "regression": {
  282. "dependent_variable": "price"
  283. }
  284. }
  285. }
  286. --------------------------------------------------
  287. // TEST[skip:TBD]
  288. The API returns the following result:
  289. [source,console-result]
  290. ----
  291. {
  292. "id" : "house_price_regression_analysis",
  293. "source" : {
  294. "index" : [
  295. "houses_sold_last_10_yrs"
  296. ],
  297. "query" : {
  298. "match_all" : { }
  299. }
  300. },
  301. "dest" : {
  302. "index" : "house_price_predictions",
  303. "results_field" : "ml"
  304. },
  305. "analysis" : {
  306. "regression" : {
  307. "dependent_variable" : "price",
  308. "training_percent" : 100
  309. }
  310. },
  311. "model_memory_limit" : "1gb",
  312. "create_time" : 1567168659127,
  313. "version" : "8.0.0",
  314. "allow_lazy_start" : false
  315. }
  316. ----
  317. // TESTRESPONSE[s/1567168659127/$body.$_path/]
  318. // TESTRESPONSE[s/"version": "8.0.0"/"version": $body.version/]
  319. The following example creates a job and specifies a training percent:
  320. [source,console]
  321. --------------------------------------------------
  322. PUT _ml/data_frame/analytics/student_performance_mathematics_0.3
  323. {
  324. "source": {
  325. "index": "student_performance_mathematics"
  326. },
  327. "dest": {
  328. "index":"student_performance_mathematics_reg"
  329. },
  330. "analysis":
  331. {
  332. "regression": {
  333. "dependent_variable": "G3",
  334. "training_percent": 70 <1>
  335. }
  336. }
  337. }
  338. --------------------------------------------------
  339. // TEST[skip:TBD]
  340. <1> The `training_percent` defines the percentage of the data set that will be used
  341. for training the model.
  342. [[ml-put-dfanalytics-example-c]]
  343. ===== {classification-cap} example
  344. The following example creates the `loan_classification` {dfanalytics-job}, the
  345. analysis type is `classification`:
  346. [source,console]
  347. --------------------------------------------------
  348. PUT _ml/data_frame/analytics/loan_classification
  349. {
  350. "source" : {
  351. "index": "loan-applicants"
  352. },
  353. "dest" : {
  354. "index": "loan-applicants-classified"
  355. },
  356. "analysis" : {
  357. "classification": {
  358. "dependent_variable": "label",
  359. "training_percent": 75,
  360. "num_top_classes": 2
  361. }
  362. }
  363. }
  364. --------------------------------------------------
  365. // TEST[skip:TBD]