put-dfanalytics.asciidoc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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)
  64. include::{docdir}/ml/ml-shared.asciidoc[tag=job-id-data-frame-analytics-define]
  65. [[ml-put-dfanalytics-request-body]]
  66. ==== {api-request-body-title}
  67. `analysis`::
  68. (Required, object) Defines the type of {dfanalytics} you want to perform on
  69. your source index. For example: `outlier_detection`. See
  70. <<dfanalytics-types>>.
  71. `analyzed_fields`::
  72. (Optional, object) Specify `includes` and/or `excludes` patterns to select
  73. which fields will be included in the analysis. If `analyzed_fields` is not
  74. set, only the relevant fields will be included. For example, all the numeric
  75. fields for {oldetection}. For the supported field types, see
  76. <<ml-put-dfanalytics-supported-fields>>. Also see the <<explain-dfanalytics>>
  77. which helps understand field selection.
  78. `includes`:::
  79. (Optional, array) An array of strings that defines the fields that will be
  80. included in the analysis.
  81. `excludes`:::
  82. (Optional, array) An array of strings that defines the fields that will be
  83. excluded from the analysis. You do not need to add fields with unsupported
  84. data types to `excludes`, these fields are excluded from the analysis
  85. automatically.
  86. `description`::
  87. (Optional, string) A description of the job.
  88. `dest`::
  89. (Required, object) The destination configuration, consisting of `index` and
  90. optionally `results_field` (`ml` by default).
  91. `index`:::
  92. (Required, string) Defines the _destination index_ to store the results of
  93. the {dfanalytics-job}.
  94. `results_field`:::
  95. (Optional, string) Defines the name of the field in which to store the
  96. results of the analysis. Default to `ml`.
  97. `model_memory_limit`::
  98. (Optional, string) The approximate maximum amount of memory resources that are
  99. permitted for analytical processing. The default value for {dfanalytics-jobs}
  100. is `1gb`. If your `elasticsearch.yml` file contains an
  101. `xpack.ml.max_model_memory_limit` setting, an error occurs when you try to
  102. create {dfanalytics-jobs} that have `model_memory_limit` values greater than
  103. that setting. For more information, see <<ml-settings>>.
  104. `source`::
  105. (object) The configuration of how to source the analysis data. It requires an
  106. `index`. Optionally, `query` and `_source` may be specified.
  107. `index`:::
  108. (Required, string or array) Index or indices on which to perform the
  109. analysis. It can be a single index or index pattern as well as an array of
  110. indices or patterns.
  111. `query`:::
  112. (Optional, object) The {es} query domain-specific language
  113. (<<query-dsl,DSL>>). This value corresponds to the query object in an {es}
  114. search POST body. All the options that are supported by {es} can be used,
  115. as this object is passed verbatim to {es}. By default, this property has
  116. the following value: `{"match_all": {}}`.
  117. `_source`:::
  118. (Optional, object) Specify `includes` and/or `excludes` patterns to select
  119. which fields will be present in the destination. Fields that are excluded
  120. cannot be included in the analysis.
  121. `includes`::::
  122. (array) An array of strings that defines the fields that will be
  123. included in the destination.
  124. `excludes`::::
  125. (array) An array of strings that defines the fields that will be
  126. excluded from the destination.
  127. `allow_lazy_start`::
  128. (Optional, boolean) Whether this job should be allowed to start when there
  129. is insufficient {ml} node capacity for it to be immediately assigned to a node.
  130. The default is `false`, which means that the <<start-dfanalytics>>
  131. will return an error if a {ml} node with capacity to run the
  132. job cannot immediately be found. (However, this is also subject to
  133. the cluster-wide `xpack.ml.max_lazy_ml_nodes` setting - see
  134. <<advanced-ml-settings>>.) If this option is set to `true` then
  135. the <<start-dfanalytics>> will not return an error, and the job will
  136. wait in the `starting` state until sufficient {ml} node capacity
  137. is available.
  138. [[ml-put-dfanalytics-example]]
  139. ==== {api-examples-title}
  140. [[ml-put-dfanalytics-example-preprocess]]
  141. ===== Preprocessing actions example
  142. The following example shows how to limit the scope of the analysis to certain
  143. fields, specify excluded fields in the destination index, and use a query to
  144. filter your data before analysis.
  145. [source,console]
  146. --------------------------------------------------
  147. PUT _ml/data_frame/analytics/model-flight-delays-pre
  148. {
  149. "source": {
  150. "index": [
  151. "kibana_sample_data_flights" <1>
  152. ],
  153. "query": { <2>
  154. "range": {
  155. "DistanceKilometers": {
  156. "gt": 0
  157. }
  158. }
  159. },
  160. "_source": { <3>
  161. "includes": [],
  162. "excludes": [
  163. "FlightDelay",
  164. "FlightDelayType"
  165. ]
  166. }
  167. },
  168. "dest": { <4>
  169. "index": "df-flight-delays",
  170. "results_field": "ml-results"
  171. },
  172. "analysis": {
  173. "regression": {
  174. "dependent_variable": "FlightDelayMin",
  175. "training_percent": 90
  176. }
  177. },
  178. "analyzed_fields": { <5>
  179. "includes": [],
  180. "excludes": [
  181. "FlightNum"
  182. ]
  183. },
  184. "model_memory_limit": "100mb"
  185. }
  186. --------------------------------------------------
  187. // TEST[skip:setup kibana sample data]
  188. <1> The source index to analyze.
  189. <2> This query filters out entire documents that will not be present in the
  190. destination index.
  191. <3> The `_source` object defines fields in the dataset that will be included or
  192. excluded in the destination index. In this case, `includes` does not specify any
  193. fields, so the default behavior takes place: all the fields of the source index
  194. will included except the ones that are explicitly specified in `excludes`.
  195. <4> Defines the destination index that contains the results of the analysis and
  196. the fields of the source index specified in the `_source` object. Also defines
  197. the name of the `results_field`.
  198. <5> Specifies fields to be included in or excluded from the analysis. This does
  199. not affect whether the fields will be present in the destination index, only
  200. affects whether they are used in the analysis.
  201. In this example, we can see that all the fields of the source index are included
  202. in the destination index except `FlightDelay` and `FlightDelayType` because
  203. these are defined as excluded fields by the `excludes` parameter of the
  204. `_source` object. The `FlightNum` field is included in the destination index,
  205. however it is not included in the analysis because it is explicitly specified as
  206. excluded field by the `excludes` parameter of the `analyzed_fields` object.
  207. [[ml-put-dfanalytics-example-od]]
  208. ===== {oldetection-cap} example
  209. The following example creates the `loganalytics` {dfanalytics-job}, the analysis
  210. type is `outlier_detection`:
  211. [source,console]
  212. --------------------------------------------------
  213. PUT _ml/data_frame/analytics/loganalytics
  214. {
  215. "description": "Outlier detection on log data",
  216. "source": {
  217. "index": "logdata"
  218. },
  219. "dest": {
  220. "index": "logdata_out"
  221. },
  222. "analysis": {
  223. "outlier_detection": {
  224. "compute_feature_influence": true,
  225. "outlier_fraction": 0.05,
  226. "standardization_enabled": true
  227. }
  228. }
  229. }
  230. --------------------------------------------------
  231. // TEST[setup:setup_logdata]
  232. The API returns the following result:
  233. [source,console-result]
  234. ----
  235. {
  236. "id": "loganalytics",
  237. "description": "Outlier detection on log data",
  238. "source": {
  239. "index": ["logdata"],
  240. "query": {
  241. "match_all": {}
  242. }
  243. },
  244. "dest": {
  245. "index": "logdata_out",
  246. "results_field": "ml"
  247. },
  248. "analysis": {
  249. "outlier_detection": {
  250. "compute_feature_influence": true,
  251. "outlier_fraction": 0.05,
  252. "standardization_enabled": true
  253. }
  254. },
  255. "model_memory_limit": "1gb",
  256. "create_time" : 1562265491319,
  257. "version" : "8.0.0",
  258. "allow_lazy_start" : false
  259. }
  260. ----
  261. // TESTRESPONSE[s/1562265491319/$body.$_path/]
  262. // TESTRESPONSE[s/"version": "8.0.0"/"version": $body.version/]
  263. [[ml-put-dfanalytics-example-r]]
  264. ===== {regression-cap} examples
  265. The following example creates the `house_price_regression_analysis`
  266. {dfanalytics-job}, the analysis type is `regression`:
  267. [source,console]
  268. --------------------------------------------------
  269. PUT _ml/data_frame/analytics/house_price_regression_analysis
  270. {
  271. "source": {
  272. "index": "houses_sold_last_10_yrs"
  273. },
  274. "dest": {
  275. "index": "house_price_predictions"
  276. },
  277. "analysis":
  278. {
  279. "regression": {
  280. "dependent_variable": "price"
  281. }
  282. }
  283. }
  284. --------------------------------------------------
  285. // TEST[skip:TBD]
  286. The API returns the following result:
  287. [source,console-result]
  288. ----
  289. {
  290. "id" : "house_price_regression_analysis",
  291. "source" : {
  292. "index" : [
  293. "houses_sold_last_10_yrs"
  294. ],
  295. "query" : {
  296. "match_all" : { }
  297. }
  298. },
  299. "dest" : {
  300. "index" : "house_price_predictions",
  301. "results_field" : "ml"
  302. },
  303. "analysis" : {
  304. "regression" : {
  305. "dependent_variable" : "price",
  306. "training_percent" : 100
  307. }
  308. },
  309. "model_memory_limit" : "1gb",
  310. "create_time" : 1567168659127,
  311. "version" : "8.0.0",
  312. "allow_lazy_start" : false
  313. }
  314. ----
  315. // TESTRESPONSE[s/1567168659127/$body.$_path/]
  316. // TESTRESPONSE[s/"version": "8.0.0"/"version": $body.version/]
  317. The following example creates a job and specifies a training percent:
  318. [source,console]
  319. --------------------------------------------------
  320. PUT _ml/data_frame/analytics/student_performance_mathematics_0.3
  321. {
  322. "source": {
  323. "index": "student_performance_mathematics"
  324. },
  325. "dest": {
  326. "index":"student_performance_mathematics_reg"
  327. },
  328. "analysis":
  329. {
  330. "regression": {
  331. "dependent_variable": "G3",
  332. "training_percent": 70 <1>
  333. }
  334. }
  335. }
  336. --------------------------------------------------
  337. // TEST[skip:TBD]
  338. <1> The `training_percent` defines the percentage of the data set that will be used
  339. for training the model.
  340. [[ml-put-dfanalytics-example-c]]
  341. ===== {classification-cap} example
  342. The following example creates the `loan_classification` {dfanalytics-job}, the
  343. analysis type is `classification`:
  344. [source,console]
  345. --------------------------------------------------
  346. PUT _ml/data_frame/analytics/loan_classification
  347. {
  348. "source" : {
  349. "index": "loan-applicants"
  350. },
  351. "dest" : {
  352. "index": "loan-applicants-classified"
  353. },
  354. "analysis" : {
  355. "classification": {
  356. "dependent_variable": "label",
  357. "training_percent": 75,
  358. "num_top_classes": 2
  359. }
  360. }
  361. }
  362. --------------------------------------------------
  363. // TEST[skip:TBD]