getting-started.asciidoc 24 KB

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