find-file-structure.asciidoc 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945
  1. [role="xpack"]
  2. [testenv="basic"]
  3. [[ml-find-file-structure]]
  4. = Find file structure API
  5. ++++
  6. <titleabbrev>Find file structure</titleabbrev>
  7. ++++
  8. experimental[]
  9. Finds the structure of a text file. The text file must contain data that is
  10. suitable to be ingested into {es}.
  11. [[ml-find-file-structure-request]]
  12. == {api-request-title}
  13. `POST _ml/find_file_structure`
  14. [[ml-find-file-structure-prereqs]]
  15. == {api-prereq-title}
  16. * If the {es} {security-features} are enabled, you must have `monitor_ml` or
  17. `monitor` cluster privileges to use this API. See
  18. <<security-privileges>>.
  19. [[ml-find-file-structure-desc]]
  20. == {api-description-title}
  21. This API provides a starting point for ingesting data into {es} in a format that
  22. is suitable for subsequent use with other {ml} functionality.
  23. Unlike other {es} endpoints, the data that is posted to this endpoint does not
  24. need to be UTF-8 encoded and in JSON format. It must, however, be text; binary
  25. file formats are not currently supported.
  26. The response from the API contains:
  27. * A couple of messages from the beginning of the file.
  28. * Statistics that reveal the most common values for all fields detected within
  29. the file and basic numeric statistics for numeric fields.
  30. * Information about the structure of the file, which is useful when you write
  31. ingest configurations to index the file contents.
  32. * Appropriate mappings for an {es} index, which you could use to ingest the file
  33. contents.
  34. All this information can be calculated by the structure finder with no guidance.
  35. However, you can optionally override some of the decisions about the file
  36. structure by specifying one or more query parameters.
  37. Details of the output can be seen in the
  38. <<ml-find-file-structure-examples,examples>>.
  39. If the structure finder produces unexpected results for a particular file,
  40. specify the `explain` query parameter. It causes an `explanation` to appear in
  41. the response, which should help in determining why the returned structure was
  42. chosen.
  43. [[ml-find-file-structure-query-parms]]
  44. == {api-query-parms-title}
  45. `charset`::
  46. (Optional, string) The file's character set. It must be a character set that
  47. is supported by the JVM that {es} uses. For example, `UTF-8`, `UTF-16LE`,
  48. `windows-1252`, or `EUC-JP`. If this parameter is not specified, the structure
  49. finder chooses an appropriate character set.
  50. `column_names`::
  51. (Optional, string) If you have set `format` to `delimited`, you can specify
  52. the column names in a comma-separated list. If this parameter is not specified,
  53. the structure finder uses the column names from the header row of the file. If
  54. the file does not have a header role, columns are named "column1", "column2",
  55. "column3", etc.
  56. `delimiter`::
  57. (Optional, string) If you have set `format` to `delimited`, you can specify
  58. the character used to delimit the values in each row. Only a single character
  59. is supported; the delimiter cannot have multiple characters. By default, the
  60. API considers the following possibilities: comma, tab, semi-colon, and pipe
  61. (`|`). In this default scenario, all rows must have the same number of fields
  62. for the delimited format to be detected. If you specify a delimiter, up to 10%
  63. of the rows can have a different number of columns than the first row.
  64. `explain`::
  65. (Optional, boolean) If this parameter is set to `true`, the response includes
  66. a field named `explanation`, which is an array of strings that indicate how
  67. the structure finder produced its result. The default value is `false`.
  68. `format`::
  69. (Optional, string) The high level structure of the file. Valid values are
  70. `ndjson`, `xml`, `delimited`, and `semi_structured_text`. By default, the
  71. API chooses the format. In this default scenario, all rows must
  72. have the same number of fields for a delimited format to be detected. If the
  73. `format` is set to `delimited` and the `delimiter` is not set, however, the
  74. API tolerates up to 5% of rows that have a different number of
  75. columns than the first row.
  76. `grok_pattern`::
  77. (Optional, string) If you have set `format` to `semi_structured_text`, you can
  78. specify a Grok pattern that is used to extract fields from every message in
  79. the file. The name of the timestamp field in the Grok pattern must match what
  80. is specified in the `timestamp_field` parameter. If that parameter is not
  81. specified, the name of the timestamp field in the Grok pattern must match
  82. "timestamp". If `grok_pattern` is not specified, the structure finder creates
  83. a Grok pattern.
  84. `has_header_row`::
  85. (Optional, boolean) If you have set `format` to `delimited`, you can use this
  86. parameter to indicate whether the column names are in the first row of the
  87. file. If this parameter is not specified, the structure finder guesses based
  88. on the similarity of the first row of the file to other rows.
  89. `line_merge_size_limit`::
  90. (Optional, unsigned integer) The maximum number of characters in a message
  91. when lines are merged to form messages while analyzing semi-structured files.
  92. The default is `10000`. If you have extremely long messages you may need to
  93. increase this, but be aware that this may lead to very long processing times
  94. if the way to group lines into messages is misdetected.
  95. `lines_to_sample`::
  96. (Optional, unsigned integer) The number of lines to include in the structural
  97. analysis, starting from the beginning of the file. The minimum is 2; the
  98. default is `1000`. If the value of this parameter is greater than the number
  99. of lines in the file, the analysis proceeds (as long as there are at least two
  100. lines in the file) for all of the lines. +
  101. +
  102. --
  103. NOTE: The number of lines and the variation of the lines affects the speed of
  104. the analysis. For example, if you upload a log file where the first 1000 lines
  105. are all variations on the same message, the analysis will find more commonality
  106. than would be seen with a bigger sample. If possible, however, it is more
  107. efficient to upload a sample file with more variety in the first 1000 lines than
  108. to request analysis of 100000 lines to achieve some variety.
  109. --
  110. `quote`::
  111. (Optional, string) If you have set `format` to `delimited`, you can specify
  112. the character used to quote the values in each row if they contain newlines or
  113. the delimiter character. Only a single character is supported. If this
  114. parameter is not specified, the default value is a double quote (`"`). If your
  115. delimited file format does not use quoting, a workaround is to set this
  116. argument to a character that does not appear anywhere in the sample.
  117. `should_trim_fields`::
  118. (Optional, boolean) If you have set `format` to `delimited`, you can specify
  119. whether values between delimiters should have whitespace trimmed from them. If
  120. this parameter is not specified and the delimiter is pipe (`|`), the default
  121. value is `true`. Otherwise, the default value is `false`.
  122. `timeout`::
  123. (Optional, <<time-units,time units>>) Sets the maximum amount of time that the
  124. structure analysis make take. If the analysis is still running when the
  125. timeout expires then it will be aborted. The default value is 25 seconds.
  126. `timestamp_field`::
  127. (Optional, string) The name of the field that contains the primary timestamp
  128. of each record in the file. In particular, if the file were ingested into an
  129. index, this is the field that would be used to populate the `@timestamp` field.
  130. +
  131. --
  132. If the `format` is `semi_structured_text`, this field must match the name of the
  133. appropriate extraction in the `grok_pattern`. Therefore, for semi-structured
  134. file formats, it is best not to specify this parameter unless `grok_pattern` is
  135. also specified.
  136. For structured file formats, if you specify this parameter, the field must exist
  137. within the file.
  138. If this parameter is not specified, the structure finder makes a decision about
  139. which field (if any) is the primary timestamp field. For structured file
  140. formats, it is not compulsory to have a timestamp in the file.
  141. --
  142. `timestamp_format`::
  143. (Optional, string) The Java time format of the timestamp field in the file.
  144. +
  145. --
  146. Only a subset of Java time format letter groups are supported:
  147. * `a`
  148. * `d`
  149. * `dd`
  150. * `EEE`
  151. * `EEEE`
  152. * `H`
  153. * `HH`
  154. * `h`
  155. * `M`
  156. * `MM`
  157. * `MMM`
  158. * `MMMM`
  159. * `mm`
  160. * `ss`
  161. * `XX`
  162. * `XXX`
  163. * `yy`
  164. * `yyyy`
  165. * `zzz`
  166. Additionally `S` letter groups (fractional seconds) of length one to nine are
  167. supported providing they occur after `ss` and separated from the `ss` by a `.`,
  168. `,` or `:`. Spacing and punctuation is also permitted with the exception of `?`,
  169. newline and carriage return, together with literal text enclosed in single
  170. quotes. For example, `MM/dd HH.mm.ss,SSSSSS 'in' yyyy` is a valid override
  171. format.
  172. One valuable use case for this parameter is when the format is semi-structured
  173. text, there are multiple timestamp formats in the file, and you know which
  174. format corresponds to the primary timestamp, but you do not want to specify the
  175. full `grok_pattern`. Another is when the timestamp format is one that the
  176. structure finder does not consider by default.
  177. If this parameter is not specified, the structure finder chooses the best
  178. format from a built-in set.
  179. The following table provides the appropriate `timeformat` values for some example timestamps:
  180. |===
  181. | Timeformat | Presentation
  182. | yyyy-MM-dd HH:mm:ssZ | 2019-04-20 13:15:22+0000
  183. | EEE, d MMM yyyy HH:mm:ss Z | Sat, 20 Apr 2019 13:15:22 +0000
  184. | dd.MM.yy HH:mm:ss.SSS | 20.04.19 13:15:22.285
  185. |===
  186. See
  187. https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html[the Java date/time format documentation]
  188. for more information about date and time format syntax.
  189. --
  190. [[ml-find-file-structure-request-body]]
  191. == {api-request-body-title}
  192. The text file that you want to analyze. It must contain data that is suitable to
  193. be ingested into {es}. It does not need to be in JSON format and it does not
  194. need to be UTF-8 encoded. The size is limited to the {es} HTTP receive buffer
  195. size, which defaults to 100 Mb.
  196. [[ml-find-file-structure-examples]]
  197. == {api-examples-title}
  198. [[ml-find-file-structure-example-nld-json]]
  199. === Ingesting newline-delimited JSON
  200. Suppose you have a newline-delimited JSON file that contains information about
  201. some books. You can send the contents to the `find_file_structure` endpoint:
  202. [source,console]
  203. ----
  204. POST _ml/find_file_structure
  205. {"name": "Leviathan Wakes", "author": "James S.A. Corey", "release_date": "2011-06-02", "page_count": 561}
  206. {"name": "Hyperion", "author": "Dan Simmons", "release_date": "1989-05-26", "page_count": 482}
  207. {"name": "Dune", "author": "Frank Herbert", "release_date": "1965-06-01", "page_count": 604}
  208. {"name": "Dune Messiah", "author": "Frank Herbert", "release_date": "1969-10-15", "page_count": 331}
  209. {"name": "Children of Dune", "author": "Frank Herbert", "release_date": "1976-04-21", "page_count": 408}
  210. {"name": "God Emperor of Dune", "author": "Frank Herbert", "release_date": "1981-05-28", "page_count": 454}
  211. {"name": "Consider Phlebas", "author": "Iain M. Banks", "release_date": "1987-04-23", "page_count": 471}
  212. {"name": "Pandora's Star", "author": "Peter F. Hamilton", "release_date": "2004-03-02", "page_count": 768}
  213. {"name": "Revelation Space", "author": "Alastair Reynolds", "release_date": "2000-03-15", "page_count": 585}
  214. {"name": "A Fire Upon the Deep", "author": "Vernor Vinge", "release_date": "1992-06-01", "page_count": 613}
  215. {"name": "Ender's Game", "author": "Orson Scott Card", "release_date": "1985-06-01", "page_count": 324}
  216. {"name": "1984", "author": "George Orwell", "release_date": "1985-06-01", "page_count": 328}
  217. {"name": "Fahrenheit 451", "author": "Ray Bradbury", "release_date": "1953-10-15", "page_count": 227}
  218. {"name": "Brave New World", "author": "Aldous Huxley", "release_date": "1932-06-01", "page_count": 268}
  219. {"name": "Foundation", "author": "Isaac Asimov", "release_date": "1951-06-01", "page_count": 224}
  220. {"name": "The Giver", "author": "Lois Lowry", "release_date": "1993-04-26", "page_count": 208}
  221. {"name": "Slaughterhouse-Five", "author": "Kurt Vonnegut", "release_date": "1969-06-01", "page_count": 275}
  222. {"name": "The Hitchhiker's Guide to the Galaxy", "author": "Douglas Adams", "release_date": "1979-10-12", "page_count": 180}
  223. {"name": "Snow Crash", "author": "Neal Stephenson", "release_date": "1992-06-01", "page_count": 470}
  224. {"name": "Neuromancer", "author": "William Gibson", "release_date": "1984-07-01", "page_count": 271}
  225. {"name": "The Handmaid's Tale", "author": "Margaret Atwood", "release_date": "1985-06-01", "page_count": 311}
  226. {"name": "Starship Troopers", "author": "Robert A. Heinlein", "release_date": "1959-12-01", "page_count": 335}
  227. {"name": "The Left Hand of Darkness", "author": "Ursula K. Le Guin", "release_date": "1969-06-01", "page_count": 304}
  228. {"name": "The Moon is a Harsh Mistress", "author": "Robert A. Heinlein", "release_date": "1966-04-01", "page_count": 288}
  229. ----
  230. If the request does not encounter errors, you receive the following result:
  231. [source,console-result]
  232. ----
  233. {
  234. "num_lines_analyzed" : 24, <1>
  235. "num_messages_analyzed" : 24, <2>
  236. "sample_start" : "{\"name\": \"Leviathan Wakes\", \"author\": \"James S.A. Corey\", \"release_date\": \"2011-06-02\", \"page_count\": 561}\n{\"name\": \"Hyperion\", \"author\": \"Dan Simmons\", \"release_date\": \"1989-05-26\", \"page_count\": 482}\n", <3>
  237. "charset" : "UTF-8", <4>
  238. "has_byte_order_marker" : false, <5>
  239. "format" : "ndjson", <6>
  240. "timestamp_field" : "release_date", <7>
  241. "joda_timestamp_formats" : [ <8>
  242. "ISO8601"
  243. ],
  244. "java_timestamp_formats" : [ <9>
  245. "ISO8601"
  246. ],
  247. "need_client_timezone" : true, <10>
  248. "mappings" : { <11>
  249. "@timestamp" : {
  250. "type" : "date"
  251. },
  252. "author" : {
  253. "type" : "keyword"
  254. },
  255. "name" : {
  256. "type" : "keyword"
  257. },
  258. "page_count" : {
  259. "type" : "long"
  260. },
  261. "release_date" : {
  262. "type" : "date",
  263. "format" : "iso8601"
  264. }
  265. },
  266. "ingest_pipeline" : {
  267. "description" : "Ingest pipeline created by file structure finder",
  268. "processors" : [
  269. {
  270. "date" : {
  271. "field" : "release_date",
  272. "timezone" : "{{ event.timezone }}",
  273. "formats" : [
  274. "ISO8601"
  275. ]
  276. }
  277. }
  278. ]
  279. },
  280. "field_stats" : { <12>
  281. "author" : {
  282. "count" : 24,
  283. "cardinality" : 20,
  284. "top_hits" : [
  285. {
  286. "value" : "Frank Herbert",
  287. "count" : 4
  288. },
  289. {
  290. "value" : "Robert A. Heinlein",
  291. "count" : 2
  292. },
  293. {
  294. "value" : "Alastair Reynolds",
  295. "count" : 1
  296. },
  297. {
  298. "value" : "Aldous Huxley",
  299. "count" : 1
  300. },
  301. {
  302. "value" : "Dan Simmons",
  303. "count" : 1
  304. },
  305. {
  306. "value" : "Douglas Adams",
  307. "count" : 1
  308. },
  309. {
  310. "value" : "George Orwell",
  311. "count" : 1
  312. },
  313. {
  314. "value" : "Iain M. Banks",
  315. "count" : 1
  316. },
  317. {
  318. "value" : "Isaac Asimov",
  319. "count" : 1
  320. },
  321. {
  322. "value" : "James S.A. Corey",
  323. "count" : 1
  324. }
  325. ]
  326. },
  327. "name" : {
  328. "count" : 24,
  329. "cardinality" : 24,
  330. "top_hits" : [
  331. {
  332. "value" : "1984",
  333. "count" : 1
  334. },
  335. {
  336. "value" : "A Fire Upon the Deep",
  337. "count" : 1
  338. },
  339. {
  340. "value" : "Brave New World",
  341. "count" : 1
  342. },
  343. {
  344. "value" : "Children of Dune",
  345. "count" : 1
  346. },
  347. {
  348. "value" : "Consider Phlebas",
  349. "count" : 1
  350. },
  351. {
  352. "value" : "Dune",
  353. "count" : 1
  354. },
  355. {
  356. "value" : "Dune Messiah",
  357. "count" : 1
  358. },
  359. {
  360. "value" : "Ender's Game",
  361. "count" : 1
  362. },
  363. {
  364. "value" : "Fahrenheit 451",
  365. "count" : 1
  366. },
  367. {
  368. "value" : "Foundation",
  369. "count" : 1
  370. }
  371. ]
  372. },
  373. "page_count" : {
  374. "count" : 24,
  375. "cardinality" : 24,
  376. "min_value" : 180,
  377. "max_value" : 768,
  378. "mean_value" : 387.0833333333333,
  379. "median_value" : 329.5,
  380. "top_hits" : [
  381. {
  382. "value" : 180,
  383. "count" : 1
  384. },
  385. {
  386. "value" : 208,
  387. "count" : 1
  388. },
  389. {
  390. "value" : 224,
  391. "count" : 1
  392. },
  393. {
  394. "value" : 227,
  395. "count" : 1
  396. },
  397. {
  398. "value" : 268,
  399. "count" : 1
  400. },
  401. {
  402. "value" : 271,
  403. "count" : 1
  404. },
  405. {
  406. "value" : 275,
  407. "count" : 1
  408. },
  409. {
  410. "value" : 288,
  411. "count" : 1
  412. },
  413. {
  414. "value" : 304,
  415. "count" : 1
  416. },
  417. {
  418. "value" : 311,
  419. "count" : 1
  420. }
  421. ]
  422. },
  423. "release_date" : {
  424. "count" : 24,
  425. "cardinality" : 20,
  426. "earliest" : "1932-06-01",
  427. "latest" : "2011-06-02",
  428. "top_hits" : [
  429. {
  430. "value" : "1985-06-01",
  431. "count" : 3
  432. },
  433. {
  434. "value" : "1969-06-01",
  435. "count" : 2
  436. },
  437. {
  438. "value" : "1992-06-01",
  439. "count" : 2
  440. },
  441. {
  442. "value" : "1932-06-01",
  443. "count" : 1
  444. },
  445. {
  446. "value" : "1951-06-01",
  447. "count" : 1
  448. },
  449. {
  450. "value" : "1953-10-15",
  451. "count" : 1
  452. },
  453. {
  454. "value" : "1959-12-01",
  455. "count" : 1
  456. },
  457. {
  458. "value" : "1965-06-01",
  459. "count" : 1
  460. },
  461. {
  462. "value" : "1966-04-01",
  463. "count" : 1
  464. },
  465. {
  466. "value" : "1969-10-15",
  467. "count" : 1
  468. }
  469. ]
  470. }
  471. }
  472. }
  473. ----
  474. // TESTRESPONSE[s/"sample_start" : ".*",/"sample_start" : "$body.sample_start",/]
  475. // The substitution is because the "file" is pre-processed by the test harness,
  476. // so the fields may get reordered in the JSON the endpoint sees
  477. <1> `num_lines_analyzed` indicates how many lines of the file were analyzed.
  478. <2> `num_messages_analyzed` indicates how many distinct messages the lines contained.
  479. For NDJSON, this value is the same as `num_lines_analyzed`. For other file
  480. formats, messages can span several lines.
  481. <3> `sample_start` reproduces the first two messages in the file verbatim. This
  482. may help to diagnose parse errors or accidental uploads of the wrong file.
  483. <4> `charset` indicates the character encoding used to parse the file.
  484. <5> For UTF character encodings, `has_byte_order_marker` indicates whether the
  485. file begins with a byte order marker.
  486. <6> `format` is one of `ndjson`, `xml`, `delimited` or `semi_structured_text`.
  487. <7> The `timestamp_field` names the field considered most likely to be the
  488. primary timestamp of each document.
  489. <8> `joda_timestamp_formats` are used to tell Logstash how to parse timestamps.
  490. <9> `java_timestamp_formats` are the Java time formats recognized in the time
  491. fields. Elasticsearch mappings and Ingest pipeline use this format.
  492. <10> If a timestamp format is detected that does not include a timezone,
  493. `need_client_timezone` will be `true`. The server that parses the file must
  494. therefore be told the correct timezone by the client.
  495. <11> `mappings` contains some suitable mappings for an index into which the data
  496. could be ingested. In this case, the `release_date` field has been given a
  497. `keyword` type as it is not considered specific enough to convert to the
  498. `date` type.
  499. <12> `field_stats` contains the most common values of each field, plus basic
  500. numeric statistics for the numeric `page_count` field. This information
  501. may provide clues that the data needs to be cleaned or transformed prior
  502. to use by other {ml} functionality.
  503. [[ml-find-file-structure-example-nyc]]
  504. === Finding the structure of NYC yellow cab example data
  505. The next example shows how it's possible to find the structure of some New York
  506. City yellow cab trip data. The first `curl` command downloads the data, the
  507. first 20000 lines of which are then piped into the `find_file_structure`
  508. endpoint. The `lines_to_sample` query parameter of the endpoint is set to 20000
  509. to match what is specified in the `head` command.
  510. [source,js]
  511. ----
  512. curl -s "s3.amazonaws.com/nyc-tlc/trip+data/yellow_tripdata_2018-06.csv" | head -20000 | curl -s -H "Content-Type: application/json" -XPOST "localhost:9200/_ml/find_file_structure?pretty&lines_to_sample=20000" -T -
  513. ----
  514. // NOTCONSOLE
  515. // Not converting to console because this shows how curl can be used
  516. --
  517. NOTE: The `Content-Type: application/json` header must be set even though in
  518. this case the data is not JSON. (Alternatively the `Content-Type` can be set
  519. to any other supported by {es}, but it must be set.)
  520. --
  521. If the request does not encounter errors, you receive the following result:
  522. [source,js]
  523. ----
  524. {
  525. "num_lines_analyzed" : 20000,
  526. "num_messages_analyzed" : 19998, <1>
  527. "sample_start" : "VendorID,tpep_pickup_datetime,tpep_dropoff_datetime,passenger_count,trip_distance,RatecodeID,store_and_fwd_flag,PULocationID,DOLocationID,payment_type,fare_amount,extra,mta_tax,tip_amount,tolls_amount,improvement_surcharge,total_amount\n\n1,2018-06-01 00:15:40,2018-06-01 00:16:46,1,.00,1,N,145,145,2,3,0.5,0.5,0,0,0.3,4.3\n",
  528. "charset" : "UTF-8",
  529. "has_byte_order_marker" : false,
  530. "format" : "delimited", <2>
  531. "multiline_start_pattern" : "^.*?,\"?\\d{4}-\\d{2}-\\d{2}[T ]\\d{2}:\\d{2}",
  532. "exclude_lines_pattern" : "^\"?VendorID\"?,\"?tpep_pickup_datetime\"?,\"?tpep_dropoff_datetime\"?,\"?passenger_count\"?,\"?trip_distance\"?,\"?RatecodeID\"?,\"?store_and_fwd_flag\"?,\"?PULocationID\"?,\"?DOLocationID\"?,\"?payment_type\"?,\"?fare_amount\"?,\"?extra\"?,\"?mta_tax\"?,\"?tip_amount\"?,\"?tolls_amount\"?,\"?improvement_surcharge\"?,\"?total_amount\"?",
  533. "column_names" : [ <3>
  534. "VendorID",
  535. "tpep_pickup_datetime",
  536. "tpep_dropoff_datetime",
  537. "passenger_count",
  538. "trip_distance",
  539. "RatecodeID",
  540. "store_and_fwd_flag",
  541. "PULocationID",
  542. "DOLocationID",
  543. "payment_type",
  544. "fare_amount",
  545. "extra",
  546. "mta_tax",
  547. "tip_amount",
  548. "tolls_amount",
  549. "improvement_surcharge",
  550. "total_amount"
  551. ],
  552. "has_header_row" : true, <4>
  553. "delimiter" : ",", <5>
  554. "quote" : "\"", <6>
  555. "timestamp_field" : "tpep_pickup_datetime", <7>
  556. "joda_timestamp_formats" : [ <8>
  557. "YYYY-MM-dd HH:mm:ss"
  558. ],
  559. "java_timestamp_formats" : [ <9>
  560. "yyyy-MM-dd HH:mm:ss"
  561. ],
  562. "need_client_timezone" : true, <10>
  563. "mappings" : {
  564. "@timestamp" : {
  565. "type" : "date"
  566. },
  567. "DOLocationID" : {
  568. "type" : "long"
  569. },
  570. "PULocationID" : {
  571. "type" : "long"
  572. },
  573. "RatecodeID" : {
  574. "type" : "long"
  575. },
  576. "VendorID" : {
  577. "type" : "long"
  578. },
  579. "extra" : {
  580. "type" : "double"
  581. },
  582. "fare_amount" : {
  583. "type" : "double"
  584. },
  585. "improvement_surcharge" : {
  586. "type" : "double"
  587. },
  588. "mta_tax" : {
  589. "type" : "double"
  590. },
  591. "passenger_count" : {
  592. "type" : "long"
  593. },
  594. "payment_type" : {
  595. "type" : "long"
  596. },
  597. "store_and_fwd_flag" : {
  598. "type" : "keyword"
  599. },
  600. "tip_amount" : {
  601. "type" : "double"
  602. },
  603. "tolls_amount" : {
  604. "type" : "double"
  605. },
  606. "total_amount" : {
  607. "type" : "double"
  608. },
  609. "tpep_dropoff_datetime" : {
  610. "type" : "date",
  611. "format" : "yyyy-MM-dd HH:mm:ss"
  612. },
  613. "tpep_pickup_datetime" : {
  614. "type" : "date",
  615. "format" : "yyyy-MM-dd HH:mm:ss"
  616. },
  617. "trip_distance" : {
  618. "type" : "double"
  619. }
  620. },
  621. "ingest_pipeline" : {
  622. "description" : "Ingest pipeline created by file structure finder",
  623. "processors" : [
  624. {
  625. "csv" : {
  626. "field" : "message",
  627. "target_fields" : [
  628. "VendorID",
  629. "tpep_pickup_datetime",
  630. "tpep_dropoff_datetime",
  631. "passenger_count",
  632. "trip_distance",
  633. "RatecodeID",
  634. "store_and_fwd_flag",
  635. "PULocationID",
  636. "DOLocationID",
  637. "payment_type",
  638. "fare_amount",
  639. "extra",
  640. "mta_tax",
  641. "tip_amount",
  642. "tolls_amount",
  643. "improvement_surcharge",
  644. "total_amount"
  645. ]
  646. }
  647. },
  648. {
  649. "date" : {
  650. "field" : "tpep_pickup_datetime",
  651. "timezone" : "{{ event.timezone }}",
  652. "formats" : [
  653. "yyyy-MM-dd HH:mm:ss"
  654. ]
  655. }
  656. },
  657. {
  658. "convert" : {
  659. "field" : "DOLocationID",
  660. "type" : "long"
  661. }
  662. },
  663. {
  664. "convert" : {
  665. "field" : "PULocationID",
  666. "type" : "long"
  667. }
  668. },
  669. {
  670. "convert" : {
  671. "field" : "RatecodeID",
  672. "type" : "long"
  673. }
  674. },
  675. {
  676. "convert" : {
  677. "field" : "VendorID",
  678. "type" : "long"
  679. }
  680. },
  681. {
  682. "convert" : {
  683. "field" : "extra",
  684. "type" : "double"
  685. }
  686. },
  687. {
  688. "convert" : {
  689. "field" : "fare_amount",
  690. "type" : "double"
  691. }
  692. },
  693. {
  694. "convert" : {
  695. "field" : "improvement_surcharge",
  696. "type" : "double"
  697. }
  698. },
  699. {
  700. "convert" : {
  701. "field" : "mta_tax",
  702. "type" : "double"
  703. }
  704. },
  705. {
  706. "convert" : {
  707. "field" : "passenger_count",
  708. "type" : "long"
  709. }
  710. },
  711. {
  712. "convert" : {
  713. "field" : "payment_type",
  714. "type" : "long"
  715. }
  716. },
  717. {
  718. "convert" : {
  719. "field" : "tip_amount",
  720. "type" : "double"
  721. }
  722. },
  723. {
  724. "convert" : {
  725. "field" : "tolls_amount",
  726. "type" : "double"
  727. }
  728. },
  729. {
  730. "convert" : {
  731. "field" : "total_amount",
  732. "type" : "double"
  733. }
  734. },
  735. {
  736. "convert" : {
  737. "field" : "trip_distance",
  738. "type" : "double"
  739. }
  740. },
  741. {
  742. "remove" : {
  743. "field" : "message"
  744. }
  745. }
  746. ]
  747. },
  748. "field_stats" : {
  749. "DOLocationID" : {
  750. "count" : 19998,
  751. "cardinality" : 240,
  752. "min_value" : 1,
  753. "max_value" : 265,
  754. "mean_value" : 150.26532653265312,
  755. "median_value" : 148,
  756. "top_hits" : [
  757. {
  758. "value" : 79,
  759. "count" : 760
  760. },
  761. {
  762. "value" : 48,
  763. "count" : 683
  764. },
  765. {
  766. "value" : 68,
  767. "count" : 529
  768. },
  769. {
  770. "value" : 170,
  771. "count" : 506
  772. },
  773. {
  774. "value" : 107,
  775. "count" : 468
  776. },
  777. {
  778. "value" : 249,
  779. "count" : 457
  780. },
  781. {
  782. "value" : 230,
  783. "count" : 441
  784. },
  785. {
  786. "value" : 186,
  787. "count" : 432
  788. },
  789. {
  790. "value" : 141,
  791. "count" : 409
  792. },
  793. {
  794. "value" : 263,
  795. "count" : 386
  796. }
  797. ]
  798. },
  799. "PULocationID" : {
  800. "count" : 19998,
  801. "cardinality" : 154,
  802. "min_value" : 1,
  803. "max_value" : 265,
  804. "mean_value" : 153.4042404240424,
  805. "median_value" : 148,
  806. "top_hits" : [
  807. {
  808. "value" : 79,
  809. "count" : 1067
  810. },
  811. {
  812. "value" : 230,
  813. "count" : 949
  814. },
  815. {
  816. "value" : 148,
  817. "count" : 940
  818. },
  819. {
  820. "value" : 132,
  821. "count" : 897
  822. },
  823. {
  824. "value" : 48,
  825. "count" : 853
  826. },
  827. {
  828. "value" : 161,
  829. "count" : 820
  830. },
  831. {
  832. "value" : 234,
  833. "count" : 750
  834. },
  835. {
  836. "value" : 249,
  837. "count" : 722
  838. },
  839. {
  840. "value" : 164,
  841. "count" : 663
  842. },
  843. {
  844. "value" : 114,
  845. "count" : 646
  846. }
  847. ]
  848. },
  849. "RatecodeID" : {
  850. "count" : 19998,
  851. "cardinality" : 5,
  852. "min_value" : 1,
  853. "max_value" : 5,
  854. "mean_value" : 1.0656565656565653,
  855. "median_value" : 1,
  856. "top_hits" : [
  857. {
  858. "value" : 1,
  859. "count" : 19311
  860. },
  861. {
  862. "value" : 2,
  863. "count" : 468
  864. },
  865. {
  866. "value" : 5,
  867. "count" : 195
  868. },
  869. {
  870. "value" : 4,
  871. "count" : 17
  872. },
  873. {
  874. "value" : 3,
  875. "count" : 7
  876. }
  877. ]
  878. },
  879. "VendorID" : {
  880. "count" : 19998,
  881. "cardinality" : 2,
  882. "min_value" : 1,
  883. "max_value" : 2,
  884. "mean_value" : 1.59005900590059,
  885. "median_value" : 2,
  886. "top_hits" : [
  887. {
  888. "value" : 2,
  889. "count" : 11800
  890. },
  891. {
  892. "value" : 1,
  893. "count" : 8198
  894. }
  895. ]
  896. },
  897. "extra" : {
  898. "count" : 19998,
  899. "cardinality" : 3,
  900. "min_value" : -0.5,
  901. "max_value" : 0.5,
  902. "mean_value" : 0.4815981598159816,
  903. "median_value" : 0.5,
  904. "top_hits" : [
  905. {
  906. "value" : 0.5,
  907. "count" : 19281
  908. },
  909. {
  910. "value" : 0,
  911. "count" : 698
  912. },
  913. {
  914. "value" : -0.5,
  915. "count" : 19
  916. }
  917. ]
  918. },
  919. "fare_amount" : {
  920. "count" : 19998,
  921. "cardinality" : 208,
  922. "min_value" : -100,
  923. "max_value" : 300,
  924. "mean_value" : 13.937719771977209,
  925. "median_value" : 9.5,
  926. "top_hits" : [
  927. {
  928. "value" : 6,
  929. "count" : 1004
  930. },
  931. {
  932. "value" : 6.5,
  933. "count" : 935
  934. },
  935. {
  936. "value" : 5.5,
  937. "count" : 909
  938. },
  939. {
  940. "value" : 7,
  941. "count" : 903
  942. },
  943. {
  944. "value" : 5,
  945. "count" : 889
  946. },
  947. {
  948. "value" : 7.5,
  949. "count" : 854
  950. },
  951. {
  952. "value" : 4.5,
  953. "count" : 802
  954. },
  955. {
  956. "value" : 8.5,
  957. "count" : 790
  958. },
  959. {
  960. "value" : 8,
  961. "count" : 789
  962. },
  963. {
  964. "value" : 9,
  965. "count" : 711
  966. }
  967. ]
  968. },
  969. "improvement_surcharge" : {
  970. "count" : 19998,
  971. "cardinality" : 3,
  972. "min_value" : -0.3,
  973. "max_value" : 0.3,
  974. "mean_value" : 0.29915991599159913,
  975. "median_value" : 0.3,
  976. "top_hits" : [
  977. {
  978. "value" : 0.3,
  979. "count" : 19964
  980. },
  981. {
  982. "value" : -0.3,
  983. "count" : 22
  984. },
  985. {
  986. "value" : 0,
  987. "count" : 12
  988. }
  989. ]
  990. },
  991. "mta_tax" : {
  992. "count" : 19998,
  993. "cardinality" : 3,
  994. "min_value" : -0.5,
  995. "max_value" : 0.5,
  996. "mean_value" : 0.4962246224622462,
  997. "median_value" : 0.5,
  998. "top_hits" : [
  999. {
  1000. "value" : 0.5,
  1001. "count" : 19868
  1002. },
  1003. {
  1004. "value" : 0,
  1005. "count" : 109
  1006. },
  1007. {
  1008. "value" : -0.5,
  1009. "count" : 21
  1010. }
  1011. ]
  1012. },
  1013. "passenger_count" : {
  1014. "count" : 19998,
  1015. "cardinality" : 7,
  1016. "min_value" : 0,
  1017. "max_value" : 6,
  1018. "mean_value" : 1.6201620162016201,
  1019. "median_value" : 1,
  1020. "top_hits" : [
  1021. {
  1022. "value" : 1,
  1023. "count" : 14219
  1024. },
  1025. {
  1026. "value" : 2,
  1027. "count" : 2886
  1028. },
  1029. {
  1030. "value" : 5,
  1031. "count" : 1047
  1032. },
  1033. {
  1034. "value" : 3,
  1035. "count" : 804
  1036. },
  1037. {
  1038. "value" : 6,
  1039. "count" : 523
  1040. },
  1041. {
  1042. "value" : 4,
  1043. "count" : 406
  1044. },
  1045. {
  1046. "value" : 0,
  1047. "count" : 113
  1048. }
  1049. ]
  1050. },
  1051. "payment_type" : {
  1052. "count" : 19998,
  1053. "cardinality" : 4,
  1054. "min_value" : 1,
  1055. "max_value" : 4,
  1056. "mean_value" : 1.315631563156316,
  1057. "median_value" : 1,
  1058. "top_hits" : [
  1059. {
  1060. "value" : 1,
  1061. "count" : 13936
  1062. },
  1063. {
  1064. "value" : 2,
  1065. "count" : 5857
  1066. },
  1067. {
  1068. "value" : 3,
  1069. "count" : 160
  1070. },
  1071. {
  1072. "value" : 4,
  1073. "count" : 45
  1074. }
  1075. ]
  1076. },
  1077. "store_and_fwd_flag" : {
  1078. "count" : 19998,
  1079. "cardinality" : 2,
  1080. "top_hits" : [
  1081. {
  1082. "value" : "N",
  1083. "count" : 19910
  1084. },
  1085. {
  1086. "value" : "Y",
  1087. "count" : 88
  1088. }
  1089. ]
  1090. },
  1091. "tip_amount" : {
  1092. "count" : 19998,
  1093. "cardinality" : 717,
  1094. "min_value" : 0,
  1095. "max_value" : 128,
  1096. "mean_value" : 2.010959095909593,
  1097. "median_value" : 1.45,
  1098. "top_hits" : [
  1099. {
  1100. "value" : 0,
  1101. "count" : 6917
  1102. },
  1103. {
  1104. "value" : 1,
  1105. "count" : 1178
  1106. },
  1107. {
  1108. "value" : 2,
  1109. "count" : 624
  1110. },
  1111. {
  1112. "value" : 3,
  1113. "count" : 248
  1114. },
  1115. {
  1116. "value" : 1.56,
  1117. "count" : 206
  1118. },
  1119. {
  1120. "value" : 1.46,
  1121. "count" : 205
  1122. },
  1123. {
  1124. "value" : 1.76,
  1125. "count" : 196
  1126. },
  1127. {
  1128. "value" : 1.45,
  1129. "count" : 195
  1130. },
  1131. {
  1132. "value" : 1.36,
  1133. "count" : 191
  1134. },
  1135. {
  1136. "value" : 1.5,
  1137. "count" : 187
  1138. }
  1139. ]
  1140. },
  1141. "tolls_amount" : {
  1142. "count" : 19998,
  1143. "cardinality" : 26,
  1144. "min_value" : 0,
  1145. "max_value" : 35,
  1146. "mean_value" : 0.2729697969796978,
  1147. "median_value" : 0,
  1148. "top_hits" : [
  1149. {
  1150. "value" : 0,
  1151. "count" : 19107
  1152. },
  1153. {
  1154. "value" : 5.76,
  1155. "count" : 791
  1156. },
  1157. {
  1158. "value" : 10.5,
  1159. "count" : 36
  1160. },
  1161. {
  1162. "value" : 2.64,
  1163. "count" : 21
  1164. },
  1165. {
  1166. "value" : 11.52,
  1167. "count" : 8
  1168. },
  1169. {
  1170. "value" : 5.54,
  1171. "count" : 4
  1172. },
  1173. {
  1174. "value" : 8.5,
  1175. "count" : 4
  1176. },
  1177. {
  1178. "value" : 17.28,
  1179. "count" : 4
  1180. },
  1181. {
  1182. "value" : 2,
  1183. "count" : 2
  1184. },
  1185. {
  1186. "value" : 2.16,
  1187. "count" : 2
  1188. }
  1189. ]
  1190. },
  1191. "total_amount" : {
  1192. "count" : 19998,
  1193. "cardinality" : 1267,
  1194. "min_value" : -100.3,
  1195. "max_value" : 389.12,
  1196. "mean_value" : 17.499898989898995,
  1197. "median_value" : 12.35,
  1198. "top_hits" : [
  1199. {
  1200. "value" : 7.3,
  1201. "count" : 478
  1202. },
  1203. {
  1204. "value" : 8.3,
  1205. "count" : 443
  1206. },
  1207. {
  1208. "value" : 8.8,
  1209. "count" : 420
  1210. },
  1211. {
  1212. "value" : 6.8,
  1213. "count" : 406
  1214. },
  1215. {
  1216. "value" : 7.8,
  1217. "count" : 405
  1218. },
  1219. {
  1220. "value" : 6.3,
  1221. "count" : 371
  1222. },
  1223. {
  1224. "value" : 9.8,
  1225. "count" : 368
  1226. },
  1227. {
  1228. "value" : 5.8,
  1229. "count" : 362
  1230. },
  1231. {
  1232. "value" : 9.3,
  1233. "count" : 332
  1234. },
  1235. {
  1236. "value" : 10.3,
  1237. "count" : 332
  1238. }
  1239. ]
  1240. },
  1241. "tpep_dropoff_datetime" : {
  1242. "count" : 19998,
  1243. "cardinality" : 9066,
  1244. "earliest" : "2018-05-31 06:18:15",
  1245. "latest" : "2018-06-02 02:25:44",
  1246. "top_hits" : [
  1247. {
  1248. "value" : "2018-06-01 01:12:12",
  1249. "count" : 10
  1250. },
  1251. {
  1252. "value" : "2018-06-01 00:32:15",
  1253. "count" : 9
  1254. },
  1255. {
  1256. "value" : "2018-06-01 00:44:27",
  1257. "count" : 9
  1258. },
  1259. {
  1260. "value" : "2018-06-01 00:46:42",
  1261. "count" : 9
  1262. },
  1263. {
  1264. "value" : "2018-06-01 01:03:22",
  1265. "count" : 9
  1266. },
  1267. {
  1268. "value" : "2018-06-01 01:05:13",
  1269. "count" : 9
  1270. },
  1271. {
  1272. "value" : "2018-06-01 00:11:20",
  1273. "count" : 8
  1274. },
  1275. {
  1276. "value" : "2018-06-01 00:16:03",
  1277. "count" : 8
  1278. },
  1279. {
  1280. "value" : "2018-06-01 00:19:47",
  1281. "count" : 8
  1282. },
  1283. {
  1284. "value" : "2018-06-01 00:25:17",
  1285. "count" : 8
  1286. }
  1287. ]
  1288. },
  1289. "tpep_pickup_datetime" : {
  1290. "count" : 19998,
  1291. "cardinality" : 8760,
  1292. "earliest" : "2018-05-31 06:08:31",
  1293. "latest" : "2018-06-02 01:21:21",
  1294. "top_hits" : [
  1295. {
  1296. "value" : "2018-06-01 00:01:23",
  1297. "count" : 12
  1298. },
  1299. {
  1300. "value" : "2018-06-01 00:04:31",
  1301. "count" : 10
  1302. },
  1303. {
  1304. "value" : "2018-06-01 00:05:38",
  1305. "count" : 10
  1306. },
  1307. {
  1308. "value" : "2018-06-01 00:09:50",
  1309. "count" : 10
  1310. },
  1311. {
  1312. "value" : "2018-06-01 00:12:01",
  1313. "count" : 10
  1314. },
  1315. {
  1316. "value" : "2018-06-01 00:14:17",
  1317. "count" : 10
  1318. },
  1319. {
  1320. "value" : "2018-06-01 00:00:34",
  1321. "count" : 9
  1322. },
  1323. {
  1324. "value" : "2018-06-01 00:00:40",
  1325. "count" : 9
  1326. },
  1327. {
  1328. "value" : "2018-06-01 00:02:53",
  1329. "count" : 9
  1330. },
  1331. {
  1332. "value" : "2018-06-01 00:05:40",
  1333. "count" : 9
  1334. }
  1335. ]
  1336. },
  1337. "trip_distance" : {
  1338. "count" : 19998,
  1339. "cardinality" : 1687,
  1340. "min_value" : 0,
  1341. "max_value" : 64.63,
  1342. "mean_value" : 3.6521062106210715,
  1343. "median_value" : 2.16,
  1344. "top_hits" : [
  1345. {
  1346. "value" : 0.9,
  1347. "count" : 335
  1348. },
  1349. {
  1350. "value" : 0.8,
  1351. "count" : 320
  1352. },
  1353. {
  1354. "value" : 1.1,
  1355. "count" : 316
  1356. },
  1357. {
  1358. "value" : 0.7,
  1359. "count" : 304
  1360. },
  1361. {
  1362. "value" : 1.2,
  1363. "count" : 303
  1364. },
  1365. {
  1366. "value" : 1,
  1367. "count" : 296
  1368. },
  1369. {
  1370. "value" : 1.3,
  1371. "count" : 280
  1372. },
  1373. {
  1374. "value" : 1.5,
  1375. "count" : 268
  1376. },
  1377. {
  1378. "value" : 1.6,
  1379. "count" : 268
  1380. },
  1381. {
  1382. "value" : 0.6,
  1383. "count" : 256
  1384. }
  1385. ]
  1386. }
  1387. }
  1388. }
  1389. ----
  1390. // NOTCONSOLE
  1391. <1> `num_messages_analyzed` is 2 lower than `num_lines_analyzed` because only
  1392. data records count as messages. The first line contains the column names
  1393. and in this sample the second line is blank.
  1394. <2> Unlike the first example, in this case the `format` has been identified as
  1395. `delimited`.
  1396. <3> Because the `format` is `delimited`, the `column_names` field in the output
  1397. lists the column names in the order they appear in the sample.
  1398. <4> `has_header_row` indicates that for this sample the column names were in
  1399. the first row of the sample. (If they hadn't been then it would have been
  1400. a good idea to specify them in the `column_names` query parameter.)
  1401. <5> The `delimiter` for this sample is a comma, as it's a CSV file.
  1402. <6> The `quote` character is the default double quote. (The structure finder
  1403. does not attempt to deduce any other quote character, so if you have a
  1404. delimited file that's quoted with some other character you must specify it
  1405. using the `quote` query parameter.)
  1406. <7> The `timestamp_field` has been chosen to be `tpep_pickup_datetime`.
  1407. `tpep_dropoff_datetime` would work just as well, but `tpep_pickup_datetime`
  1408. was chosen because it comes first in the column order. If you prefer
  1409. `tpep_dropoff_datetime` then force it to be chosen using the
  1410. `timestamp_field` query parameter.
  1411. <8> `joda_timestamp_formats` are used to tell Logstash how to parse timestamps.
  1412. <9> `java_timestamp_formats` are the Java time formats recognized in the time
  1413. fields. Elasticsearch mappings and Ingest pipeline use this format.
  1414. <10> The timestamp format in this sample doesn't specify a timezone, so to
  1415. accurately convert them to UTC timestamps to store in Elasticsearch it's
  1416. necessary to supply the timezone they relate to. `need_client_timezone`
  1417. will be `false` for timestamp formats that include the timezone.
  1418. [[ml-find-file-structure-example-timeout]]
  1419. === Setting the timeout parameter
  1420. If you try to analyze a lot of data then the analysis will take a long time.
  1421. If you want to limit the amount of processing your {es} cluster performs for
  1422. a request, use the `timeout` query parameter. The analysis will be aborted and
  1423. an error returned when the timeout expires. For example, you can replace 20000
  1424. lines in the previous example with 200000 and set a 1 second timeout on the
  1425. analysis:
  1426. [source,js]
  1427. ----
  1428. curl -s "s3.amazonaws.com/nyc-tlc/trip+data/yellow_tripdata_2018-06.csv" | head -200000 | curl -s -H "Content-Type: application/json" -XPOST "localhost:9200/_ml/find_file_structure?pretty&lines_to_sample=200000&timeout=1s" -T -
  1429. ----
  1430. // NOTCONSOLE
  1431. // Not converting to console because this shows how curl can be used
  1432. Unless you are using an incredibly fast computer you'll receive a timeout error:
  1433. [source,js]
  1434. ----
  1435. {
  1436. "error" : {
  1437. "root_cause" : [
  1438. {
  1439. "type" : "timeout_exception",
  1440. "reason" : "Aborting structure analysis during [delimited record parsing] as it has taken longer than the timeout of [1s]"
  1441. }
  1442. ],
  1443. "type" : "timeout_exception",
  1444. "reason" : "Aborting structure analysis during [delimited record parsing] as it has taken longer than the timeout of [1s]"
  1445. },
  1446. "status" : 500
  1447. }
  1448. ----
  1449. // NOTCONSOLE
  1450. --
  1451. NOTE: If you try the example above yourself you will note that the overall
  1452. running time of the `curl` commands is considerably longer than 1 second. This
  1453. is because it takes a while to download 200000 lines of CSV from the internet,
  1454. and the timeout is measured from the time this endpoint starts to process the
  1455. data.
  1456. --
  1457. [[ml-find-file-structure-example-eslog]]
  1458. === Analyzing {es} log files
  1459. This is an example of analyzing {es}'s own log file:
  1460. [source,js]
  1461. ----
  1462. curl -s -H "Content-Type: application/json" -XPOST "localhost:9200/_ml/find_file_structure?pretty" -T "$ES_HOME/logs/elasticsearch.log"
  1463. ----
  1464. // NOTCONSOLE
  1465. // Not converting to console because this shows how curl can be used
  1466. If the request does not encounter errors, the result will look something like
  1467. this:
  1468. [source,js]
  1469. ----
  1470. {
  1471. "num_lines_analyzed" : 53,
  1472. "num_messages_analyzed" : 53,
  1473. "sample_start" : "[2018-09-27T14:39:28,518][INFO ][o.e.e.NodeEnvironment ] [node-0] using [1] data paths, mounts [[/ (/dev/disk1)]], net usable_space [165.4gb], net total_space [464.7gb], types [hfs]\n[2018-09-27T14:39:28,521][INFO ][o.e.e.NodeEnvironment ] [node-0] heap size [494.9mb], compressed ordinary object pointers [true]\n",
  1474. "charset" : "UTF-8",
  1475. "has_byte_order_marker" : false,
  1476. "format" : "semi_structured_text", <1>
  1477. "multiline_start_pattern" : "^\\[\\b\\d{4}-\\d{2}-\\d{2}[T ]\\d{2}:\\d{2}", <2>
  1478. "grok_pattern" : "\\[%{TIMESTAMP_ISO8601:timestamp}\\]\\[%{LOGLEVEL:loglevel}.*", <3>
  1479. "timestamp_field" : "timestamp",
  1480. "joda_timestamp_formats" : [
  1481. "ISO8601"
  1482. ],
  1483. "java_timestamp_formats" : [
  1484. "ISO8601"
  1485. ],
  1486. "need_client_timezone" : true,
  1487. "mappings" : {
  1488. "@timestamp" : {
  1489. "type" : "date"
  1490. },
  1491. "loglevel" : {
  1492. "type" : "keyword"
  1493. },
  1494. "message" : {
  1495. "type" : "text"
  1496. }
  1497. },
  1498. "ingest_pipeline" : {
  1499. "description" : "Ingest pipeline created by file structure finder",
  1500. "processors" : [
  1501. {
  1502. "grok" : {
  1503. "field" : "message",
  1504. "patterns" : [
  1505. "\\[%{TIMESTAMP_ISO8601:timestamp}\\]\\[%{LOGLEVEL:loglevel}.*"
  1506. ]
  1507. }
  1508. },
  1509. {
  1510. "date" : {
  1511. "field" : "timestamp",
  1512. "timezone" : "{{ event.timezone }}",
  1513. "formats" : [
  1514. "ISO8601"
  1515. ]
  1516. }
  1517. },
  1518. {
  1519. "remove" : {
  1520. "field" : "timestamp"
  1521. }
  1522. }
  1523. ]
  1524. },
  1525. "field_stats" : {
  1526. "loglevel" : {
  1527. "count" : 53,
  1528. "cardinality" : 3,
  1529. "top_hits" : [
  1530. {
  1531. "value" : "INFO",
  1532. "count" : 51
  1533. },
  1534. {
  1535. "value" : "DEBUG",
  1536. "count" : 1
  1537. },
  1538. {
  1539. "value" : "WARN",
  1540. "count" : 1
  1541. }
  1542. ]
  1543. },
  1544. "timestamp" : {
  1545. "count" : 53,
  1546. "cardinality" : 28,
  1547. "earliest" : "2018-09-27T14:39:28,518",
  1548. "latest" : "2018-09-27T14:39:37,012",
  1549. "top_hits" : [
  1550. {
  1551. "value" : "2018-09-27T14:39:29,859",
  1552. "count" : 10
  1553. },
  1554. {
  1555. "value" : "2018-09-27T14:39:29,860",
  1556. "count" : 9
  1557. },
  1558. {
  1559. "value" : "2018-09-27T14:39:29,858",
  1560. "count" : 6
  1561. },
  1562. {
  1563. "value" : "2018-09-27T14:39:28,523",
  1564. "count" : 3
  1565. },
  1566. {
  1567. "value" : "2018-09-27T14:39:34,234",
  1568. "count" : 2
  1569. },
  1570. {
  1571. "value" : "2018-09-27T14:39:28,518",
  1572. "count" : 1
  1573. },
  1574. {
  1575. "value" : "2018-09-27T14:39:28,521",
  1576. "count" : 1
  1577. },
  1578. {
  1579. "value" : "2018-09-27T14:39:28,522",
  1580. "count" : 1
  1581. },
  1582. {
  1583. "value" : "2018-09-27T14:39:29,861",
  1584. "count" : 1
  1585. },
  1586. {
  1587. "value" : "2018-09-27T14:39:32,786",
  1588. "count" : 1
  1589. }
  1590. ]
  1591. }
  1592. }
  1593. }
  1594. ----
  1595. // NOTCONSOLE
  1596. <1> This time the `format` has been identified as `semi_structured_text`.
  1597. <2> The `multiline_start_pattern` is set on the basis that the timestamp appears
  1598. in the first line of each multi-line log message.
  1599. <3> A very simple `grok_pattern` has been created, which extracts the timestamp
  1600. and recognizable fields that appear in every analyzed message. In this case
  1601. the only field that was recognized beyond the timestamp was the log level.
  1602. [[ml-find-file-structure-example-grok]]
  1603. === Specifying `grok_pattern` as query parameter
  1604. If you recognize more fields than the simple `grok_pattern` produced by the
  1605. structure finder unaided then you can resubmit the request specifying a more
  1606. advanced `grok_pattern` as a query parameter and the structure finder will
  1607. calculate `field_stats` for your additional fields.
  1608. In the case of the {es} log a more complete Grok pattern is
  1609. `\[%{TIMESTAMP_ISO8601:timestamp}\]\[%{LOGLEVEL:loglevel} *\]\[%{JAVACLASS:class} *\] \[%{HOSTNAME:node}\] %{JAVALOGMESSAGE:message}`.
  1610. You can analyze the same log file again, submitting this `grok_pattern` as a
  1611. query parameter (appropriately URL escaped):
  1612. [source,js]
  1613. ----
  1614. curl -s -H "Content-Type: application/json" -XPOST "localhost:9200/_ml/find_file_structure?pretty&format=semi_structured_text&grok_pattern=%5C%5B%25%7BTIMESTAMP_ISO8601:timestamp%7D%5C%5D%5C%5B%25%7BLOGLEVEL:loglevel%7D%20*%5C%5D%5C%5B%25%7BJAVACLASS:class%7D%20*%5C%5D%20%5C%5B%25%7BHOSTNAME:node%7D%5C%5D%20%25%7BJAVALOGMESSAGE:message%7D" -T "$ES_HOME/logs/elasticsearch.log"
  1615. ----
  1616. // NOTCONSOLE
  1617. // Not converting to console because this shows how curl can be used
  1618. If the request does not encounter errors, the result will look something like
  1619. this:
  1620. [source,js]
  1621. ----
  1622. {
  1623. "num_lines_analyzed" : 53,
  1624. "num_messages_analyzed" : 53,
  1625. "sample_start" : "[2018-09-27T14:39:28,518][INFO ][o.e.e.NodeEnvironment ] [node-0] using [1] data paths, mounts [[/ (/dev/disk1)]], net usable_space [165.4gb], net total_space [464.7gb], types [hfs]\n[2018-09-27T14:39:28,521][INFO ][o.e.e.NodeEnvironment ] [node-0] heap size [494.9mb], compressed ordinary object pointers [true]\n",
  1626. "charset" : "UTF-8",
  1627. "has_byte_order_marker" : false,
  1628. "format" : "semi_structured_text",
  1629. "multiline_start_pattern" : "^\\[\\b\\d{4}-\\d{2}-\\d{2}[T ]\\d{2}:\\d{2}",
  1630. "grok_pattern" : "\\[%{TIMESTAMP_ISO8601:timestamp}\\]\\[%{LOGLEVEL:loglevel} *\\]\\[%{JAVACLASS:class} *\\] \\[%{HOSTNAME:node}\\] %{JAVALOGMESSAGE:message}", <1>
  1631. "timestamp_field" : "timestamp",
  1632. "joda_timestamp_formats" : [
  1633. "ISO8601"
  1634. ],
  1635. "java_timestamp_formats" : [
  1636. "ISO8601"
  1637. ],
  1638. "need_client_timezone" : true,
  1639. "mappings" : {
  1640. "@timestamp" : {
  1641. "type" : "date"
  1642. },
  1643. "class" : {
  1644. "type" : "keyword"
  1645. },
  1646. "loglevel" : {
  1647. "type" : "keyword"
  1648. },
  1649. "message" : {
  1650. "type" : "text"
  1651. },
  1652. "node" : {
  1653. "type" : "keyword"
  1654. }
  1655. },
  1656. "ingest_pipeline" : {
  1657. "description" : "Ingest pipeline created by file structure finder",
  1658. "processors" : [
  1659. {
  1660. "grok" : {
  1661. "field" : "message",
  1662. "patterns" : [
  1663. "\\[%{TIMESTAMP_ISO8601:timestamp}\\]\\[%{LOGLEVEL:loglevel} *\\]\\[%{JAVACLASS:class} *\\] \\[%{HOSTNAME:node}\\] %{JAVALOGMESSAGE:message}"
  1664. ]
  1665. }
  1666. },
  1667. {
  1668. "date" : {
  1669. "field" : "timestamp",
  1670. "timezone" : "{{ event.timezone }}",
  1671. "formats" : [
  1672. "ISO8601"
  1673. ]
  1674. }
  1675. },
  1676. {
  1677. "remove" : {
  1678. "field" : "timestamp"
  1679. }
  1680. }
  1681. ]
  1682. },
  1683. "field_stats" : { <2>
  1684. "class" : {
  1685. "count" : 53,
  1686. "cardinality" : 14,
  1687. "top_hits" : [
  1688. {
  1689. "value" : "o.e.p.PluginsService",
  1690. "count" : 26
  1691. },
  1692. {
  1693. "value" : "o.e.c.m.MetadataIndexTemplateService",
  1694. "count" : 8
  1695. },
  1696. {
  1697. "value" : "o.e.n.Node",
  1698. "count" : 7
  1699. },
  1700. {
  1701. "value" : "o.e.e.NodeEnvironment",
  1702. "count" : 2
  1703. },
  1704. {
  1705. "value" : "o.e.a.ActionModule",
  1706. "count" : 1
  1707. },
  1708. {
  1709. "value" : "o.e.c.s.ClusterApplierService",
  1710. "count" : 1
  1711. },
  1712. {
  1713. "value" : "o.e.c.s.MasterService",
  1714. "count" : 1
  1715. },
  1716. {
  1717. "value" : "o.e.d.DiscoveryModule",
  1718. "count" : 1
  1719. },
  1720. {
  1721. "value" : "o.e.g.GatewayService",
  1722. "count" : 1
  1723. },
  1724. {
  1725. "value" : "o.e.l.LicenseService",
  1726. "count" : 1
  1727. }
  1728. ]
  1729. },
  1730. "loglevel" : {
  1731. "count" : 53,
  1732. "cardinality" : 3,
  1733. "top_hits" : [
  1734. {
  1735. "value" : "INFO",
  1736. "count" : 51
  1737. },
  1738. {
  1739. "value" : "DEBUG",
  1740. "count" : 1
  1741. },
  1742. {
  1743. "value" : "WARN",
  1744. "count" : 1
  1745. }
  1746. ]
  1747. },
  1748. "message" : {
  1749. "count" : 53,
  1750. "cardinality" : 53,
  1751. "top_hits" : [
  1752. {
  1753. "value" : "Using REST wrapper from plugin org.elasticsearch.xpack.security.Security",
  1754. "count" : 1
  1755. },
  1756. {
  1757. "value" : "adding template [.monitoring-alerts] for index patterns [.monitoring-alerts-6]",
  1758. "count" : 1
  1759. },
  1760. {
  1761. "value" : "adding template [.monitoring-beats] for index patterns [.monitoring-beats-6-*]",
  1762. "count" : 1
  1763. },
  1764. {
  1765. "value" : "adding template [.monitoring-es] for index patterns [.monitoring-es-6-*]",
  1766. "count" : 1
  1767. },
  1768. {
  1769. "value" : "adding template [.monitoring-kibana] for index patterns [.monitoring-kibana-6-*]",
  1770. "count" : 1
  1771. },
  1772. {
  1773. "value" : "adding template [.monitoring-logstash] for index patterns [.monitoring-logstash-6-*]",
  1774. "count" : 1
  1775. },
  1776. {
  1777. "value" : "adding template [.triggered_watches] for index patterns [.triggered_watches*]",
  1778. "count" : 1
  1779. },
  1780. {
  1781. "value" : "adding template [.watch-history-9] for index patterns [.watcher-history-9*]",
  1782. "count" : 1
  1783. },
  1784. {
  1785. "value" : "adding template [.watches] for index patterns [.watches*]",
  1786. "count" : 1
  1787. },
  1788. {
  1789. "value" : "starting ...",
  1790. "count" : 1
  1791. }
  1792. ]
  1793. },
  1794. "node" : {
  1795. "count" : 53,
  1796. "cardinality" : 1,
  1797. "top_hits" : [
  1798. {
  1799. "value" : "node-0",
  1800. "count" : 53
  1801. }
  1802. ]
  1803. },
  1804. "timestamp" : {
  1805. "count" : 53,
  1806. "cardinality" : 28,
  1807. "earliest" : "2018-09-27T14:39:28,518",
  1808. "latest" : "2018-09-27T14:39:37,012",
  1809. "top_hits" : [
  1810. {
  1811. "value" : "2018-09-27T14:39:29,859",
  1812. "count" : 10
  1813. },
  1814. {
  1815. "value" : "2018-09-27T14:39:29,860",
  1816. "count" : 9
  1817. },
  1818. {
  1819. "value" : "2018-09-27T14:39:29,858",
  1820. "count" : 6
  1821. },
  1822. {
  1823. "value" : "2018-09-27T14:39:28,523",
  1824. "count" : 3
  1825. },
  1826. {
  1827. "value" : "2018-09-27T14:39:34,234",
  1828. "count" : 2
  1829. },
  1830. {
  1831. "value" : "2018-09-27T14:39:28,518",
  1832. "count" : 1
  1833. },
  1834. {
  1835. "value" : "2018-09-27T14:39:28,521",
  1836. "count" : 1
  1837. },
  1838. {
  1839. "value" : "2018-09-27T14:39:28,522",
  1840. "count" : 1
  1841. },
  1842. {
  1843. "value" : "2018-09-27T14:39:29,861",
  1844. "count" : 1
  1845. },
  1846. {
  1847. "value" : "2018-09-27T14:39:32,786",
  1848. "count" : 1
  1849. }
  1850. ]
  1851. }
  1852. }
  1853. }
  1854. ----
  1855. // NOTCONSOLE
  1856. <1> The `grok_pattern` in the output is now the overridden one supplied in the
  1857. query parameter.
  1858. <2> The returned `field_stats` include entries for the fields from the
  1859. overridden `grok_pattern`.
  1860. The URL escaping is hard, so if you are working interactively it is best to use
  1861. the {ml} UI!