getting-started.asciidoc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. [[getting-started]]
  2. = Getting started with {es}
  3. [partintro]
  4. --
  5. Ready to take {es} for a test drive and see for yourself how you can use the
  6. REST APIs to store, search, and analyze data?
  7. Follow this getting started tutorial to:
  8. . Get an {es} cluster up and running
  9. . Index some sample documents
  10. . Search for documents using the {es} query language
  11. . Analyze the results using bucket and metrics aggregations
  12. Need more context?
  13. Check out the <<elasticsearch-intro,
  14. {es} Introduction>> to learn the lingo and understand the basics of
  15. how {es} works. If you're already familiar with {es} and want to see how it works
  16. with the rest of the stack, you might want to jump to the
  17. {stack-gs}/get-started-elastic-stack.html[Elastic Stack
  18. Tutorial] to see how to set up a system monitoring solution with {es}, {kib},
  19. {beats}, and {ls}.
  20. TIP: The fastest way to get started with {es} is to
  21. {ess-trial}[start a free 14-day
  22. trial of {ess}] in the cloud.
  23. --
  24. [[getting-started-install]]
  25. == Get {es} up and running
  26. To take {es} for a test drive, you can create a
  27. {ess-trial}[hosted deployment] on
  28. the {ess} or set up a multi-node {es} cluster on your own
  29. Linux, macOS, or Windows machine.
  30. [float]
  31. [[run-elasticsearch-hosted]]
  32. === Run {es} on Elastic Cloud
  33. When you create a deployment on the {es} Service, the service provisions
  34. a three-node {es} cluster along with Kibana and APM.
  35. To create a deployment:
  36. . Sign up for a {ess-trial}[free trial]
  37. and verify your email address.
  38. . Set a password for your account.
  39. . Click **Create Deployment**.
  40. Once you've created a deployment, you're ready to <<getting-started-index>>.
  41. [float]
  42. [[run-elasticsearch-local]]
  43. === Run {es} locally on Linux, macOS, or Windows
  44. When you create a deployment on the {ess}, a master node and
  45. two data nodes are provisioned automatically. By installing from the tar or zip
  46. archive, you can start multiple instances of {es} locally to see how a multi-node
  47. cluster behaves.
  48. To run a three-node {es} cluster locally:
  49. . Download the {es} archive for your OS:
  50. +
  51. Linux: https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-{version}-linux-x86_64.tar.gz[elasticsearch-{version}-linux-x86_64.tar.gz]
  52. +
  53. ["source","sh",subs="attributes,callouts"]
  54. --------------------------------------------------
  55. curl -L -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-{version}-linux-x86_64.tar.gz
  56. --------------------------------------------------
  57. // NOTCONSOLE
  58. +
  59. macOS: https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-{version}-darwin-x86_64.tar.gz[elasticsearch-{version}-darwin-x86_64.tar.gz]
  60. +
  61. ["source","sh",subs="attributes,callouts"]
  62. --------------------------------------------------
  63. curl -L -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-{version}-darwin-x86_64.tar.gz
  64. --------------------------------------------------
  65. // NOTCONSOLE
  66. +
  67. Windows:
  68. https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-{version}-windows-x86_64.zip[elasticsearch-{version}-windows-x86_64.zip]
  69. . Extract the archive:
  70. +
  71. Linux:
  72. +
  73. ["source","sh",subs="attributes,callouts"]
  74. --------------------------------------------------
  75. tar -xvf elasticsearch-{version}-linux-x86_64.tar.gz
  76. --------------------------------------------------
  77. +
  78. macOS:
  79. +
  80. ["source","sh",subs="attributes,callouts"]
  81. --------------------------------------------------
  82. tar -xvf elasticsearch-{version}-darwin-x86_64.tar.gz
  83. --------------------------------------------------
  84. +
  85. Windows PowerShell:
  86. +
  87. ["source","powershell",subs="attributes,callouts"]
  88. --------------------------------------------------
  89. Expand-Archive elasticsearch-{version}-windows-x86_64.zip
  90. --------------------------------------------------
  91. . Start {es} from the `bin` directory:
  92. +
  93. Linux and macOS:
  94. +
  95. ["source","sh",subs="attributes,callouts"]
  96. --------------------------------------------------
  97. cd elasticsearch-{version}/bin
  98. ./elasticsearch
  99. --------------------------------------------------
  100. +
  101. Windows:
  102. +
  103. ["source","powershell",subs="attributes,callouts"]
  104. --------------------------------------------------
  105. cd elasticsearch-{version}\bin
  106. .\elasticsearch.bat
  107. --------------------------------------------------
  108. +
  109. You now have a single-node {es} cluster up and running!
  110. . Start two more instances of {es} so you can see how a typical multi-node
  111. cluster behaves. You need to specify unique data and log paths
  112. for each node.
  113. +
  114. Linux and macOS:
  115. +
  116. ["source","sh",subs="attributes,callouts"]
  117. --------------------------------------------------
  118. ./elasticsearch -Epath.data=data2 -Epath.logs=log2
  119. ./elasticsearch -Epath.data=data3 -Epath.logs=log3
  120. --------------------------------------------------
  121. +
  122. Windows:
  123. +
  124. ["source","powershell",subs="attributes,callouts"]
  125. --------------------------------------------------
  126. .\elasticsearch.bat -E path.data=data2 -E path.logs=log2
  127. .\elasticsearch.bat -E path.data=data3 -E path.logs=log3
  128. --------------------------------------------------
  129. +
  130. The additional nodes are assigned unique IDs. Because you're running all three
  131. nodes locally, they automatically join the cluster with the first node.
  132. . Use the cat health API to verify that your three-node cluster is up running.
  133. The cat APIs return information about your cluster and indices in a
  134. format that's easier to read than raw JSON.
  135. +
  136. You can interact directly with your cluster by submitting HTTP requests to
  137. the {es} REST API. If you have Kibana installed and running, you can also
  138. open Kibana and submit requests through the Dev Console.
  139. +
  140. TIP: You'll want to check out the
  141. https://www.elastic.co/guide/en/elasticsearch/client/index.html[{es} language
  142. clients] when you're ready to start using {es} in your own applications.
  143. +
  144. [source,console]
  145. --------------------------------------------------
  146. GET /_cat/health?v
  147. --------------------------------------------------
  148. +
  149. The response should indicate that the status of the `elasticsearch` cluster
  150. is `green` and it has three nodes:
  151. +
  152. [source,txt]
  153. --------------------------------------------------
  154. epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
  155. 1565052807 00:53:27 elasticsearch green 3 3 6 3 0 0 0 0 - 100.0%
  156. --------------------------------------------------
  157. // TESTRESPONSE[s/1565052807 00:53:27 elasticsearch/\\d+ \\d+:\\d+:\\d+ integTest/]
  158. // TESTRESPONSE[s/3 3 6 3/\\d+ \\d+ \\d+ \\d+/]
  159. // TESTRESPONSE[s/0 0 -/0 \\d+ (-|\\d+(\.\\d+)?(micros|ms|s))/]
  160. // TESTRESPONSE[non_json]
  161. +
  162. NOTE: The cluster status will remain yellow if you are only running a single
  163. instance of {es}. A single node cluster is fully functional, but data
  164. cannot be replicated to another node to provide resiliency. Replica shards must
  165. be available for the cluster status to be green. If the cluster status is red,
  166. some data is unavailable.
  167. [discrete]
  168. [[gs-curl]]
  169. === Talking to {es} with cURL commands
  170. Most of the examples in this guide enable you to copy the appropriate cURL
  171. command and submit the request to your local {es} instance from the command line.
  172. A request to Elasticsearch consists of the same parts as any HTTP request:
  173. [source,sh]
  174. --------------------------------------------------
  175. curl -X<VERB> '<PROTOCOL>://<HOST>:<PORT>/<PATH>?<QUERY_STRING>' -d '<BODY>'
  176. --------------------------------------------------
  177. // NOTCONSOLE
  178. This example uses the following variables:
  179. `<VERB>`:: The appropriate HTTP method or verb. For example, `GET`, `POST`,
  180. `PUT`, `HEAD`, or `DELETE`.
  181. `<PROTOCOL>`:: Either `http` or `https`. Use the latter if you have an HTTPS
  182. proxy in front of {es} or you use {es} {security-features} to encrypt HTTP
  183. communications.
  184. `<HOST>`:: The hostname of any node in your {es} cluster. Alternatively, use
  185. +localhost+ for a node on your local machine.
  186. `<PORT>`:: The port running the {es} HTTP service, which defaults to `9200`.
  187. `<PATH>`:: The API endpoint, which can contain multiple components, such as
  188. `_cluster/stats` or `_nodes/stats/jvm`.
  189. `<QUERY_STRING>`:: Any optional query-string parameters. For example, `?pretty`
  190. will _pretty-print_ the JSON response to make it easier to read.
  191. `<BODY>`:: A JSON-encoded request body (if necessary).
  192. If the {es} {security-features} are enabled, you must also provide a valid user
  193. name (and password) that has authority to run the API. For example, use the
  194. `-u` or `--u` cURL command parameter. For details about which security
  195. privileges are required to run each API, see <<rest-apis>>.
  196. {es} responds to each API request with an HTTP status code like `200 OK`. With
  197. the exception of `HEAD` requests, it also returns a JSON-encoded response body.
  198. [float]
  199. [[gs-other-install]]
  200. === Other installation options
  201. Installing {es} from an archive file enables you to easily install and run
  202. multiple instances locally so you can try things out. To run a single instance,
  203. you can run {es} in a Docker container, install {es} using the DEB or RPM
  204. packages on Linux, install using Homebrew on macOS, or install using the MSI
  205. package installer on Windows. See <<install-elasticsearch>> for more information.
  206. [[getting-started-index]]
  207. == Index some documents
  208. Once you have a cluster up and running, you're ready to index some data.
  209. There are a variety of ingest options for {es}, but in the end they all
  210. do the same thing: put JSON documents into an {es} index.
  211. You can do this directly with a simple PUT request that specifies
  212. the index you want to add the document, a unique document ID, and one or more
  213. `"field": "value"` pairs in the request body:
  214. [source,console]
  215. --------------------------------------------------
  216. PUT /customer/_doc/1
  217. {
  218. "name": "John Doe"
  219. }
  220. --------------------------------------------------
  221. This request automatically creates the `customer` index if it doesn't already
  222. exist, adds a new document that has an ID of `1`, and stores and
  223. indexes the `name` field.
  224. Since this is a new document, the response shows that the result of the
  225. operation was that version 1 of the document was created:
  226. [source,console-result]
  227. --------------------------------------------------
  228. {
  229. "_index" : "customer",
  230. "_id" : "1",
  231. "_version" : 1,
  232. "result" : "created",
  233. "_shards" : {
  234. "total" : 2,
  235. "successful" : 2,
  236. "failed" : 0
  237. },
  238. "_seq_no" : 26,
  239. "_primary_term" : 4
  240. }
  241. --------------------------------------------------
  242. // TESTRESPONSE[s/"_seq_no" : \d+/"_seq_no" : $body._seq_no/]
  243. // TESTRESPONSE[s/"successful" : \d+/"successful" : $body._shards.successful/]
  244. // TESTRESPONSE[s/"_primary_term" : \d+/"_primary_term" : $body._primary_term/]
  245. The new document is available immediately from any node in the cluster.
  246. You can retrieve it with a GET request that specifies its document ID:
  247. [source,console]
  248. --------------------------------------------------
  249. GET /customer/_doc/1
  250. --------------------------------------------------
  251. // TEST[continued]
  252. The response indicates that a document with the specified ID was found
  253. and shows the original source fields that were indexed.
  254. [source,console-result]
  255. --------------------------------------------------
  256. {
  257. "_index" : "customer",
  258. "_id" : "1",
  259. "_version" : 1,
  260. "_seq_no" : 26,
  261. "_primary_term" : 4,
  262. "found" : true,
  263. "_source" : {
  264. "name": "John Doe"
  265. }
  266. }
  267. --------------------------------------------------
  268. // TESTRESPONSE[s/"_seq_no" : \d+/"_seq_no" : $body._seq_no/ ]
  269. // TESTRESPONSE[s/"_primary_term" : \d+/"_primary_term" : $body._primary_term/]
  270. [float]
  271. [[getting-started-batch-processing]]
  272. === Indexing documents in bulk
  273. If you have a lot of documents to index, you can submit them in batches with
  274. the {ref}/docs-bulk.html[bulk API]. Using bulk to batch document
  275. operations is significantly faster than submitting requests individually as it minimizes network roundtrips.
  276. The optimal batch size depends on a number of factors: the document size and complexity, the indexing and search load, and the resources available to your cluster. A good place to start is with batches of 1,000 to 5,000 documents
  277. and a total payload between 5MB and 15MB. From there, you can experiment
  278. to find the sweet spot.
  279. To get some data into {es} that you can start searching and analyzing:
  280. . Download the https://github.com/elastic/elasticsearch/blob/master/docs/src/test/resources/accounts.json?raw=true[`accounts.json`] sample data set. The documents in this randomly-generated data set represent user accounts with the following information:
  281. +
  282. [source,js]
  283. --------------------------------------------------
  284. {
  285. "account_number": 0,
  286. "balance": 16623,
  287. "firstname": "Bradshaw",
  288. "lastname": "Mckenzie",
  289. "age": 29,
  290. "gender": "F",
  291. "address": "244 Columbus Place",
  292. "employer": "Euron",
  293. "email": "bradshawmckenzie@euron.com",
  294. "city": "Hobucken",
  295. "state": "CO"
  296. }
  297. --------------------------------------------------
  298. // NOTCONSOLE
  299. . Index the account data into the `bank` index with the following `_bulk` request:
  300. +
  301. [source,sh]
  302. --------------------------------------------------
  303. curl -H "Content-Type: application/json" -XPOST "localhost:9200/bank/_bulk?pretty&refresh" --data-binary "@accounts.json"
  304. curl "localhost:9200/_cat/indices?v"
  305. --------------------------------------------------
  306. // NOTCONSOLE
  307. +
  308. ////
  309. This replicates the above in a document-testing friendly way but isn't visible
  310. in the docs:
  311. +
  312. [source,console]
  313. --------------------------------------------------
  314. GET /_cat/indices?v
  315. --------------------------------------------------
  316. // TEST[setup:bank]
  317. ////
  318. +
  319. The response indicates that 1,000 documents were indexed successfully.
  320. +
  321. [source,txt]
  322. --------------------------------------------------
  323. health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
  324. yellow open bank l7sSYV2cQXmu6_4rJWVIww 5 1 1000 0 128.6kb 128.6kb
  325. --------------------------------------------------
  326. // TESTRESPONSE[s/128.6kb/\\d+(\\.\\d+)?[mk]?b/]
  327. // TESTRESPONSE[s/l7sSYV2cQXmu6_4rJWVIww/.+/ non_json]
  328. [[getting-started-search]]
  329. == Start searching
  330. Once you have ingested some data into an {es} index, you can search it
  331. by sending requests to the `_search` endpoint. To access the full suite of
  332. search capabilities, you use the {es} Query DSL to specify the
  333. search criteria in the request body. You specify the name of the index you
  334. want to search in the request URI.
  335. For example, the following request retrieves all documents in the `bank`
  336. index sorted by account number:
  337. [source,console]
  338. --------------------------------------------------
  339. GET /bank/_search
  340. {
  341. "query": { "match_all": {} },
  342. "sort": [
  343. { "account_number": "asc" }
  344. ]
  345. }
  346. --------------------------------------------------
  347. // TEST[continued]
  348. By default, the `hits` section of the response includes the first 10 documents
  349. that match the search criteria:
  350. [source,console-result]
  351. --------------------------------------------------
  352. {
  353. "took" : 63,
  354. "timed_out" : false,
  355. "_shards" : {
  356. "total" : 5,
  357. "successful" : 5,
  358. "skipped" : 0,
  359. "failed" : 0
  360. },
  361. "hits" : {
  362. "total" : {
  363. "value": 1000,
  364. "relation": "eq"
  365. },
  366. "max_score" : null,
  367. "hits" : [ {
  368. "_index" : "bank",
  369. "_id" : "0",
  370. "sort": [0],
  371. "_score" : null,
  372. "_source" : {"account_number":0,"balance":16623,"firstname":"Bradshaw","lastname":"Mckenzie","age":29,"gender":"F","address":"244 Columbus Place","employer":"Euron","email":"bradshawmckenzie@euron.com","city":"Hobucken","state":"CO"}
  373. }, {
  374. "_index" : "bank",
  375. "_id" : "1",
  376. "sort": [1],
  377. "_score" : null,
  378. "_source" : {"account_number":1,"balance":39225,"firstname":"Amber","lastname":"Duke","age":32,"gender":"M","address":"880 Holmes Lane","employer":"Pyrami","email":"amberduke@pyrami.com","city":"Brogan","state":"IL"}
  379. }, ...
  380. ]
  381. }
  382. }
  383. --------------------------------------------------
  384. // TESTRESPONSE[s/"took" : 63/"took" : $body.took/]
  385. // TESTRESPONSE[s/\.\.\./$body.hits.hits.2, $body.hits.hits.3, $body.hits.hits.4, $body.hits.hits.5, $body.hits.hits.6, $body.hits.hits.7, $body.hits.hits.8, $body.hits.hits.9/]
  386. The response also provides the following information about the search request:
  387. * `took` – how long it took {es} to run the query, in milliseconds
  388. * `timed_out` – whether or not the search request timed out
  389. * `_shards` – how many shards were searched and a breakdown of how many shards
  390. succeeded, failed, or were skipped.
  391. * `max_score` – the score of the most relevant document found
  392. * `hits.total.value` - how many matching documents were found
  393. * `hits.sort` - the document's sort position (when not sorting by relevance score)
  394. * `hits._score` - the document's relevance score (not applicable when using `match_all`)
  395. Each search request is self-contained: {es} does not maintain any
  396. state information across requests. To page through the search hits, specify
  397. the `from` and `size` parameters in your request.
  398. For example, the following request gets hits 10 through 19:
  399. [source,console]
  400. --------------------------------------------------
  401. GET /bank/_search
  402. {
  403. "query": { "match_all": {} },
  404. "sort": [
  405. { "account_number": "asc" }
  406. ],
  407. "from": 10,
  408. "size": 10
  409. }
  410. --------------------------------------------------
  411. // TEST[continued]
  412. Now that you've seen how to submit a basic search request, you can start to
  413. construct queries that are a bit more interesting than `match_all`.
  414. To search for specific terms within a field, you can use a `match` query.
  415. For example, the following request searches the `address` field to find
  416. customers whose addresses contain `mill` or `lane`:
  417. [source,console]
  418. --------------------------------------------------
  419. GET /bank/_search
  420. {
  421. "query": { "match": { "address": "mill lane" } }
  422. }
  423. --------------------------------------------------
  424. // TEST[continued]
  425. To perform a phrase search rather than matching individual terms, you use
  426. `match_phrase` instead of `match`. For example, the following request only
  427. matches addresses that contain the phrase `mill lane`:
  428. [source,console]
  429. --------------------------------------------------
  430. GET /bank/_search
  431. {
  432. "query": { "match_phrase": { "address": "mill lane" } }
  433. }
  434. --------------------------------------------------
  435. // TEST[continued]
  436. To construct more complex queries, you can use a `bool` query to combine
  437. multiple query criteria. You can designate criteria as required (must match),
  438. desirable (should match), or undesirable (must not match).
  439. For example, the following request searches the `bank` index for accounts that
  440. belong to customers who are 40 years old, but excludes anyone who lives in
  441. Idaho (ID):
  442. [source,console]
  443. --------------------------------------------------
  444. GET /bank/_search
  445. {
  446. "query": {
  447. "bool": {
  448. "must": [
  449. { "match": { "age": "40" } }
  450. ],
  451. "must_not": [
  452. { "match": { "state": "ID" } }
  453. ]
  454. }
  455. }
  456. }
  457. --------------------------------------------------
  458. // TEST[continued]
  459. Each `must`, `should`, and `must_not` element in a Boolean query is referred
  460. to as a query clause. How well a document meets the criteria in each `must` or
  461. `should` clause contributes to the document's _relevance score_. The higher the
  462. score, the better the document matches your search criteria. By default, {es}
  463. returns documents ranked by these relevance scores.
  464. The criteria in a `must_not` clause is treated as a _filter_. It affects whether
  465. or not the document is included in the results, but does not contribute to
  466. how documents are scored. You can also explicitly specify arbitrary filters to
  467. include or exclude documents based on structured data.
  468. For example, the following request uses a range filter to limit the results to
  469. accounts with a balance between $20,000 and $30,000 (inclusive).
  470. [source,console]
  471. --------------------------------------------------
  472. GET /bank/_search
  473. {
  474. "query": {
  475. "bool": {
  476. "must": { "match_all": {} },
  477. "filter": {
  478. "range": {
  479. "balance": {
  480. "gte": 20000,
  481. "lte": 30000
  482. }
  483. }
  484. }
  485. }
  486. }
  487. }
  488. --------------------------------------------------
  489. // TEST[continued]
  490. [[getting-started-aggregations]]
  491. == Analyze results with aggregations
  492. {es} aggregations enable you to get meta-information about your search results
  493. and answer questions like, "How many account holders are in Texas?" or
  494. "What's the average balance of accounts in Tennessee?" You can search
  495. documents, filter hits, and use aggregations to analyze the results all in one
  496. request.
  497. For example, the following request uses a `terms` aggregation to group
  498. all of the accounts in the `bank` index by state, and returns the ten states
  499. with the most accounts in descending order:
  500. [source,console]
  501. --------------------------------------------------
  502. GET /bank/_search
  503. {
  504. "size": 0,
  505. "aggs": {
  506. "group_by_state": {
  507. "terms": {
  508. "field": "state.keyword"
  509. }
  510. }
  511. }
  512. }
  513. --------------------------------------------------
  514. // TEST[continued]
  515. The `buckets` in the response are the values of the `state` field. The
  516. `doc_count` shows the number of accounts in each state. For example, you
  517. can see that there are 27 accounts in `ID` (Idaho). Because the request
  518. set `size=0`, the response only contains the aggregation results.
  519. [source,console-result]
  520. --------------------------------------------------
  521. {
  522. "took": 29,
  523. "timed_out": false,
  524. "_shards": {
  525. "total": 5,
  526. "successful": 5,
  527. "skipped" : 0,
  528. "failed": 0
  529. },
  530. "hits" : {
  531. "total" : {
  532. "value": 1000,
  533. "relation": "eq"
  534. },
  535. "max_score" : null,
  536. "hits" : [ ]
  537. },
  538. "aggregations" : {
  539. "group_by_state" : {
  540. "doc_count_error_upper_bound": 20,
  541. "sum_other_doc_count": 770,
  542. "buckets" : [ {
  543. "key" : "ID",
  544. "doc_count" : 27
  545. }, {
  546. "key" : "TX",
  547. "doc_count" : 27
  548. }, {
  549. "key" : "AL",
  550. "doc_count" : 25
  551. }, {
  552. "key" : "MD",
  553. "doc_count" : 25
  554. }, {
  555. "key" : "TN",
  556. "doc_count" : 23
  557. }, {
  558. "key" : "MA",
  559. "doc_count" : 21
  560. }, {
  561. "key" : "NC",
  562. "doc_count" : 21
  563. }, {
  564. "key" : "ND",
  565. "doc_count" : 21
  566. }, {
  567. "key" : "ME",
  568. "doc_count" : 20
  569. }, {
  570. "key" : "MO",
  571. "doc_count" : 20
  572. } ]
  573. }
  574. }
  575. }
  576. --------------------------------------------------
  577. // TESTRESPONSE[s/"took": 29/"took": $body.took/]
  578. You can combine aggregations to build more complex summaries of your data. For
  579. example, the following request nests an `avg` aggregation within the previous
  580. `group_by_state` aggregation to calculate the average account balances for
  581. each state.
  582. [source,console]
  583. --------------------------------------------------
  584. GET /bank/_search
  585. {
  586. "size": 0,
  587. "aggs": {
  588. "group_by_state": {
  589. "terms": {
  590. "field": "state.keyword"
  591. },
  592. "aggs": {
  593. "average_balance": {
  594. "avg": {
  595. "field": "balance"
  596. }
  597. }
  598. }
  599. }
  600. }
  601. }
  602. --------------------------------------------------
  603. // TEST[continued]
  604. Instead of sorting the results by count, you could sort using the result of
  605. the nested aggregation by specifying the order within the `terms` aggregation:
  606. [source,console]
  607. --------------------------------------------------
  608. GET /bank/_search
  609. {
  610. "size": 0,
  611. "aggs": {
  612. "group_by_state": {
  613. "terms": {
  614. "field": "state.keyword",
  615. "order": {
  616. "average_balance": "desc"
  617. }
  618. },
  619. "aggs": {
  620. "average_balance": {
  621. "avg": {
  622. "field": "balance"
  623. }
  624. }
  625. }
  626. }
  627. }
  628. }
  629. --------------------------------------------------
  630. // TEST[continued]
  631. In addition to basic bucketing and metrics aggregations like these, {es}
  632. provides specialized aggregations for operating on multiple fields and
  633. analyzing particular types of data such as dates, IP addresses, and geo
  634. data. You can also feed the results of individual aggregations into pipeline
  635. aggregations for further analysis.
  636. The core analysis capabilities provided by aggregations enable advanced
  637. features such as using machine learning to detect anomalies.
  638. [[getting-started-next-steps]]
  639. == Where to go from here
  640. Now that you've set up a cluster, indexed some documents, and run some
  641. searches and aggregations, you might want to:
  642. * {stack-gs}/get-started-elastic-stack.html#install-kibana[Dive in to the Elastic
  643. Stack Tutorial] to install Kibana, Logstash, and Beats and
  644. set up a basic system monitoring solution.
  645. * {kibana-ref}/add-sample-data.html[Load one of the sample data sets into Kibana]
  646. to see how you can use {es} and Kibana together to visualize your data.
  647. * Try out one of the Elastic search solutions:
  648. ** https://swiftype.com/documentation/site-search/crawler-quick-start[Site Search]
  649. ** https://swiftype.com/documentation/app-search/getting-started[App Search]
  650. ** https://swiftype.com/documentation/enterprise-search/getting-started[Enterprise Search]