getting-started.asciidoc 12 KB

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