find-file-structure.asciidoc 53 KB

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