find-file-structure.asciidoc 49 KB

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