find-structure.asciidoc 52 KB

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