getting-started.asciidoc 12 KB

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