find-file-structure.asciidoc 53 KB

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