find-file-structure.asciidoc 52 KB

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