find-file-structure.asciidoc 49 KB

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