find-structure.asciidoc 52 KB

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