getting-started.asciidoc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. [chapter]
  2. [[getting-started]]
  3. = Quick start
  4. This guide helps beginners learn how to:
  5. * Install and run {es} in a test environment
  6. * Add data to {es}
  7. * Search and sort data
  8. * Extract fields from unstructured content during a search
  9. [discrete]
  10. [[run-elasticsearch]]
  11. === Run {es}
  12. The simplest way to set up {es} is to create a managed deployment with {ess} on
  13. {ecloud}. If you prefer to manage your own test environment, you can install and
  14. run {es} using Docker.
  15. include::{es-repo-dir}/tab-widgets/quick-start-install-widget.asciidoc[]
  16. [discrete]
  17. [[send-requests-to-elasticsearch]]
  18. === Send requests to {es}
  19. You send data and other requests to {es} using REST APIs. This lets you interact
  20. with {es} using any client that sends HTTP requests, such as
  21. https://curl.se[curl]. You can also use {kib}'s console to send requests to
  22. {es}.
  23. include::{es-repo-dir}/tab-widgets/api-call-widget.asciidoc[]
  24. [discrete]
  25. [[add-data]]
  26. === Add data
  27. You add data to {es} as JSON objects called documents. {es} stores these
  28. documents in searchable indices.
  29. For time series data, such as logs and metrics, you typically add documents to a
  30. data stream made up of multiple auto-generated backing indices.
  31. A data stream requires an index template that matches its name. {es} uses this
  32. template to configure the stream's backing indices. Documents sent to a data
  33. stream must have a `@timestamp` field.
  34. [discrete]
  35. [[add-single-document]]
  36. ==== Add a single document
  37. Submit the following indexing request to add a single log entry to the
  38. `logs-my_app-default` data stream. Since `logs-my_app-default` doesn't exist, the
  39. request automatically creates it using the built-in `logs-*-*` index template.
  40. [source,console]
  41. ----
  42. POST logs-my_app-default/_doc
  43. {
  44. "@timestamp": "2099-05-06T16:21:15.000Z",
  45. "event": {
  46. "original": "192.0.2.42 - - [06/May/2099:16:21:15 +0000] \"GET /images/bg.jpg HTTP/1.0\" 200 24736"
  47. }
  48. }
  49. ----
  50. // TEST[s/_doc/_doc?refresh=wait_for/]
  51. The response includes metadata that {es} generates for the document:
  52. * The backing `_index` that contains the document. {es} automatically generates
  53. the names of backing indices.
  54. * A unique `_id` for the document within the index.
  55. [source,console-result]
  56. ----
  57. {
  58. "_index": ".ds-logs-my_app-default-2099-05-06-000001",
  59. "_id": "gl5MJXMBMk1dGnErnBW8",
  60. "_version": 1,
  61. "result": "created",
  62. "_shards": {
  63. "total": 2,
  64. "successful": 1,
  65. "failed": 0
  66. },
  67. "_seq_no": 0,
  68. "_primary_term": 1
  69. }
  70. ----
  71. // TESTRESPONSE[s/"_index": ".ds-logs-my_app-default-2099-05-06-000001"/"_index": $body._index/]
  72. // TESTRESPONSE[s/"_id": "gl5MJXMBMk1dGnErnBW8"/"_id": $body._id/]
  73. [discrete]
  74. [[add-multiple-documents]]
  75. ==== Add multiple documents
  76. Use the `_bulk` endpoint to add multiple documents in one request. Bulk data
  77. must be newline-delimited JSON (NDJSON). Each line must end in a newline
  78. character (`\n`), including the last line.
  79. [source,console]
  80. ----
  81. PUT logs-my_app-default/_bulk
  82. { "create": { } }
  83. { "@timestamp": "2099-05-07T16:24:32.000Z", "event": { "original": "192.0.2.242 - - [07/May/2020:16:24:32 -0500] \"GET /images/hm_nbg.jpg HTTP/1.0\" 304 0" } }
  84. { "create": { } }
  85. { "@timestamp": "2099-05-08T16:25:42.000Z", "event": { "original": "192.0.2.255 - - [08/May/2099:16:25:42 +0000] \"GET /favicon.ico HTTP/1.0\" 200 3638" } }
  86. ----
  87. // TEST[continued]
  88. // TEST[s/_bulk/_bulk?refresh=wait_for/]
  89. [discrete]
  90. [[qs-search-data]]
  91. === Search data
  92. Indexed documents are available for search in near real-time. The following
  93. search matches all log entries in `logs-my_app-default` and sorts them by
  94. `@timestamp` in descending order.
  95. [source,console]
  96. ----
  97. GET logs-my_app-default/_search
  98. {
  99. "query": {
  100. "match_all": { }
  101. },
  102. "sort": [
  103. {
  104. "@timestamp": "desc"
  105. }
  106. ]
  107. }
  108. ----
  109. // TEST[continued]
  110. By default, the `hits` section of the response includes up to the first 10
  111. documents that match the search. The `_source` of each hit contains the original
  112. JSON object submitted during indexing.
  113. [source,console-result]
  114. ----
  115. {
  116. "took": 2,
  117. "timed_out": false,
  118. "_shards": {
  119. "total": 1,
  120. "successful": 1,
  121. "skipped": 0,
  122. "failed": 0
  123. },
  124. "hits": {
  125. "total": {
  126. "value": 3,
  127. "relation": "eq"
  128. },
  129. "max_score": null,
  130. "hits": [
  131. {
  132. "_index": ".ds-logs-my_app-default-2099-05-06-000001",
  133. "_id": "PdjWongB9KPnaVm2IyaL",
  134. "_score": null,
  135. "_source": {
  136. "@timestamp": "2099-05-08T16:25:42.000Z",
  137. "event": {
  138. "original": "192.0.2.255 - - [08/May/2099:16:25:42 +0000] \"GET /favicon.ico HTTP/1.0\" 200 3638"
  139. }
  140. },
  141. "sort": [
  142. 4081940742000
  143. ]
  144. },
  145. ...
  146. ]
  147. }
  148. }
  149. ----
  150. // TESTRESPONSE[s/"took": 2/"took": $body.took/]
  151. // TESTRESPONSE[s/"_index": ".ds-logs-my_app-default-2099-05-06-000001"/"_index": $body.hits.hits.0._index/]
  152. // TESTRESPONSE[s/"_id": "PdjWongB9KPnaVm2IyaL"/"_id": $body.hits.hits.0._id/]
  153. // TESTRESPONSE[s/\.\.\./$body.hits.hits.1,$body.hits.hits.2/]
  154. [discrete]
  155. [[get-specific-fields]]
  156. ==== Get specific fields
  157. Parsing the entire `_source` is unwieldy for large documents. To exclude it from
  158. the response, set the `_source` parameter to `false`. Instead, use the `fields`
  159. parameter to retrieve the fields you want.
  160. [source,console]
  161. ----
  162. GET logs-my_app-default/_search
  163. {
  164. "query": {
  165. "match_all": { }
  166. },
  167. "fields": [
  168. "@timestamp"
  169. ],
  170. "_source": false,
  171. "sort": [
  172. {
  173. "@timestamp": "desc"
  174. }
  175. ]
  176. }
  177. ----
  178. // TEST[continued]
  179. // TEST[s/_search/_search?filter_path=hits.hits&size=1/]
  180. The response contains each hit's `fields` values as a flat array.
  181. [source,console-result]
  182. ----
  183. {
  184. ...
  185. "hits": {
  186. ...
  187. "hits": [
  188. {
  189. "_index": ".ds-logs-my_app-default-2099-05-06-000001",
  190. "_id": "PdjWongB9KPnaVm2IyaL",
  191. "_score": null,
  192. "fields": {
  193. "@timestamp": [
  194. "2099-05-08T16:25:42.000Z"
  195. ]
  196. },
  197. "sort": [
  198. 4081940742000
  199. ]
  200. },
  201. ...
  202. ]
  203. }
  204. }
  205. ----
  206. // TESTRESPONSE[s/\.\.\.//]
  207. // TESTRESPONSE[s/"_index": ".ds-logs-my_app-default-2099-05-06-000001"/"_index": $body.hits.hits.0._index/]
  208. // TESTRESPONSE[s/"_id": "PdjWongB9KPnaVm2IyaL"/"_id": $body.hits.hits.0._id/]
  209. // TESTRESPONSE[s/4081940742000\n \]\n \},\n/4081940742000\]}/]
  210. [discrete]
  211. [[search-date-range]]
  212. ==== Search a date range
  213. To search across a specific time or IP range, use a `range` query.
  214. [source,console]
  215. ----
  216. GET logs-my_app-default/_search
  217. {
  218. "query": {
  219. "range": {
  220. "@timestamp": {
  221. "gte": "2099-05-05",
  222. "lt": "2099-05-08"
  223. }
  224. }
  225. },
  226. "fields": [
  227. "@timestamp"
  228. ],
  229. "_source": false,
  230. "sort": [
  231. {
  232. "@timestamp": "desc"
  233. }
  234. ]
  235. }
  236. ----
  237. // TEST[continued]
  238. You can use date math to define relative time ranges. The following query
  239. searches for data from the past day, which won't match any log entries in
  240. `logs-my_app-default`.
  241. [source,console]
  242. ----
  243. GET logs-my_app-default/_search
  244. {
  245. "query": {
  246. "range": {
  247. "@timestamp": {
  248. "gte": "now-1d/d",
  249. "lt": "now/d"
  250. }
  251. }
  252. },
  253. "fields": [
  254. "@timestamp"
  255. ],
  256. "_source": false,
  257. "sort": [
  258. {
  259. "@timestamp": "desc"
  260. }
  261. ]
  262. }
  263. ----
  264. // TEST[continued]
  265. [discrete]
  266. [[extract-fields]]
  267. ==== Extract fields from unstructured content
  268. You can extract <<runtime-search-request,runtime fields>> from unstructured
  269. content, such as log messages, during a search.
  270. Use the following search to extract the `source.ip` runtime field from
  271. `event.original`. To include it in the response, add `source.ip` to the `fields`
  272. parameter.
  273. [source,console]
  274. ----
  275. GET logs-my_app-default/_search
  276. {
  277. "runtime_mappings": {
  278. "source.ip": {
  279. "type": "ip",
  280. "script": """
  281. String sourceip=grok('%{IPORHOST:sourceip} .*').extract(doc[ "event.original" ].value)?.sourceip;
  282. if (sourceip != null) emit(sourceip);
  283. """
  284. }
  285. },
  286. "query": {
  287. "range": {
  288. "@timestamp": {
  289. "gte": "2099-05-05",
  290. "lt": "2099-05-08"
  291. }
  292. }
  293. },
  294. "fields": [
  295. "@timestamp",
  296. "source.ip"
  297. ],
  298. "_source": false,
  299. "sort": [
  300. {
  301. "@timestamp": "desc"
  302. }
  303. ]
  304. }
  305. ----
  306. // TEST[continued]
  307. [discrete]
  308. [[combine-queries]]
  309. ==== Combine queries
  310. You can use the `bool` query to combine multiple queries. The following search
  311. combines two `range` queries: one on `@timestamp` and one on the `source.ip`
  312. runtime field.
  313. [source,console]
  314. ----
  315. GET logs-my_app-default/_search
  316. {
  317. "runtime_mappings": {
  318. "source.ip": {
  319. "type": "ip",
  320. "script": """
  321. String sourceip=grok('%{IPORHOST:sourceip} .*').extract(doc[ "event.original" ].value)?.sourceip;
  322. if (sourceip != null) emit(sourceip);
  323. """
  324. }
  325. },
  326. "query": {
  327. "bool": {
  328. "filter": [
  329. {
  330. "range": {
  331. "@timestamp": {
  332. "gte": "2099-05-05",
  333. "lt": "2099-05-08"
  334. }
  335. }
  336. },
  337. {
  338. "range": {
  339. "source.ip": {
  340. "gte": "192.0.2.0",
  341. "lte": "192.0.2.240"
  342. }
  343. }
  344. }
  345. ]
  346. }
  347. },
  348. "fields": [
  349. "@timestamp",
  350. "source.ip"
  351. ],
  352. "_source": false,
  353. "sort": [
  354. {
  355. "@timestamp": "desc"
  356. }
  357. ]
  358. }
  359. ----
  360. // TEST[continued]
  361. [discrete]
  362. [[aggregate-data]]
  363. ==== Aggregate data
  364. Use aggregations to summarize data as metrics, statistics, or other analytics.
  365. The following search uses an aggregation to calculate the
  366. `average_response_size` using the `http.response.body.bytes` runtime field. The
  367. aggregation only runs on documents that match the `query`.
  368. [source,console]
  369. ----
  370. GET logs-my_app-default/_search
  371. {
  372. "runtime_mappings": {
  373. "http.response.body.bytes": {
  374. "type": "long",
  375. "script": """
  376. String bytes=grok('%{COMMONAPACHELOG}').extract(doc[ "event.original" ].value)?.bytes;
  377. if (bytes != null) emit(Integer.parseInt(bytes));
  378. """
  379. }
  380. },
  381. "aggs": {
  382. "average_response_size":{
  383. "avg": {
  384. "field": "http.response.body.bytes"
  385. }
  386. }
  387. },
  388. "query": {
  389. "bool": {
  390. "filter": [
  391. {
  392. "range": {
  393. "@timestamp": {
  394. "gte": "2099-05-05",
  395. "lt": "2099-05-08"
  396. }
  397. }
  398. }
  399. ]
  400. }
  401. },
  402. "fields": [
  403. "@timestamp",
  404. "http.response.body.bytes"
  405. ],
  406. "_source": false,
  407. "sort": [
  408. {
  409. "@timestamp": "desc"
  410. }
  411. ]
  412. }
  413. ----
  414. // TEST[continued]
  415. The response’s `aggregations` object contains aggregation results.
  416. [source,console-result]
  417. ----
  418. {
  419. ...
  420. "aggregations" : {
  421. "average_response_size" : {
  422. "value" : 12368.0
  423. }
  424. }
  425. }
  426. ----
  427. // TESTRESPONSE[s/\.\.\./"took": "$body.took", "timed_out": false, "_shards": "$body._shards", "hits": "$body.hits",/]
  428. [discrete]
  429. [[explore-more-search-options]]
  430. ==== Explore more search options
  431. To keep exploring, index more data to your data stream and check out <<common-search-options>>.
  432. [discrete]
  433. [[clean-up]]
  434. === Clean up
  435. When you're done, delete your test data stream and its backing indices.
  436. [source,console]
  437. ----
  438. DELETE _data_stream/logs-my_app-default
  439. ----
  440. // TEST[continued]
  441. You can also delete your test deployment.
  442. include::{es-repo-dir}/tab-widgets/quick-start-cleanup-widget.asciidoc[]
  443. [discrete]
  444. [[whats-next]]
  445. === What's next?
  446. * Get the most out of your time series data by setting up data tiers and
  447. {ilm-init}. See <<use-elasticsearch-for-time-series-data>>.
  448. * Use {fleet} and {agent} to collect logs and metrics directly from your data
  449. sources and send them to {es}. See the
  450. {observability-guide}/ingest-logs-metrics-uptime.html[Ingest logs, metrics, and uptime data with {agent}].
  451. * Use {kib} to explore, visualize, and manage your {es} data. See the
  452. {kibana-ref}/get-started.html[{kib} quick start guide].