Skip to content

Commit 16ad752

Browse files
committed
[DOCS] Backporting GS search & aggs updates. (elastic#46008)
* [DOCS] Streamlined GS aggs section. (elastic#45951) * [DOCS] Streamlined GS aggs section. * Update docs/reference/getting-started.asciidoc Co-Authored-By: James Rodewig <james.rodewig@elastic.co> * [DOCS] Fix typo. (elastic#46006)
1 parent f01a836 commit 16ad752

File tree

1 file changed

+44
-157
lines changed

1 file changed

+44
-157
lines changed

docs/reference/getting-started.asciidoc

Lines changed: 44 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Follow this getting started tutorial to:
1717
Need more context?
1818

1919
Check out the <<elasticsearch-intro,
20-
Elasticsearch Introduction>> to learn the lingo and understand the basics of
20+
{es} Introduction>> to learn the lingo and understand the basics of
2121
how {es} works. If you're already familiar with {es} and want to see how it works
2222
with the rest of the stack, you might want to jump to the
2323
{stack-gs}/get-started-elastic-stack.html[Elastic Stack
@@ -26,15 +26,15 @@ Tutorial] to see how to set up a system monitoring solution with {es}, {kib},
2626

2727
TIP: The fastest way to get started with {es} is to
2828
https://www.elastic.co/cloud/elasticsearch-service/signup[start a free 14-day
29-
trial of Elasticsearch Service] in the cloud.
29+
trial of {ess}] in the cloud.
3030
--
3131

3232
[[getting-started-install]]
3333
== Get {es} up and running
3434

35-
To take {es} for a test drive, you can create a one-click cloud deployment
36-
on the https://www.elastic.co/cloud/elasticsearch-service/signup[Elasticsearch Service],
37-
or <<run-elasticsearch-local, set up a multi-node {es} cluster>> on your own
35+
To take {es} for a test drive, you can create a
36+
https://www.elastic.co/cloud/elasticsearch-service/signup[hosted deployment] on
37+
the {ess} or set up a multi-node {es} cluster on your own
3838
Linux, macOS, or Windows machine.
3939

4040
[float]
@@ -57,13 +57,14 @@ Once you've created a deployment, you're ready to <<getting-started-index>>.
5757
[[run-elasticsearch-local]]
5858
=== Run {es} locally on Linux, macOS, or Windows
5959

60-
When you create a cluster on the Elasticsearch Service, you automatically
61-
get a three-node cluster. By installing from the tar or zip archive, you can
62-
start multiple instances of {es} locally to see how a multi-node cluster behaves.
60+
When you create a deployment on the {ess}, a master node and
61+
two data nodes are provisioned automatically. By installing from the tar or zip
62+
archive, you can start multiple instances of {es} locally to see how a multi-node
63+
cluster behaves.
6364

6465
To run a three-node {es} cluster locally:
6566

66-
. Download the Elasticsearch archive for your OS:
67+
. Download the {es} archive for your OS:
6768
+
6869
Linux: https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-{version}-linux-x86_64.tar.gz[elasticsearch-{version}-linux-x86_64.tar.gz]
6970
+
@@ -107,7 +108,7 @@ Windows PowerShell:
107108
Expand-Archive elasticsearch-{version}-windows-x86_64.zip
108109
--------------------------------------------------
109110

110-
. Start elasticsearch from the `bin` directory:
111+
. Start {es} from the `bin` directory:
111112
+
112113
Linux and macOS:
113114
+
@@ -354,28 +355,8 @@ search capabilities, you use the {es} Query DSL to specify the
354355
search criteria in the request body. You specify the name of the index you
355356
want to search in the request URI.
356357

357-
As for the response, we see the following parts:
358-
359-
* `took` – time in milliseconds for Elasticsearch to execute the search
360-
* `timed_out` – tells us if the search timed out or not
361-
* `_shards` – tells us how many shards were searched, as well as a count of the successful/failed searched shards
362-
* `hits` – search results
363-
* `hits.total` – an object that contains information about the total number of documents matching our search criteria
364-
** `hits.total.value` - the value of the total hit count (must be interpreted in the context of `hits.total.relation`).
365-
** `hits.total.relation` - whether `hits.total.value` is the exact hit count, in which case it is equal to `"eq"` or a
366-
lower bound of the total hit count (greater than or equals), in which case it is equal to `gte`.
367-
* `hits.hits` – actual array of search results (defaults to first 10 documents)
368-
* `hits.sort` - sort value of the sort key for each result (missing if sorting by score)
369-
* `hits._score` and `max_score` - ignore these fields for now
370-
371-
The accuracy of `hits.total` is controlled by the request parameter `track_total_hits`, when set to true
372-
the request will track the total hits accurately (`"relation": "eq"`). It defaults to `10,000`
373-
which means that the total hit count is accurately tracked up to `10,000` documents.
374-
You can force an accurate count by setting `track_total_hits` to true explicitly.
375-
See the <<search-request-body, request body>> documentation
376-
for more details.
377-
378-
Here is the same exact search above using the alternative request body method:
358+
For example, the following request retrieves all documents in the `bank`
359+
index sorted by account number:
379360

380361
[source,js]
381362
--------------------------------------------------
@@ -440,7 +421,9 @@ succeeded, failed, or were skipped.
440421
* `hits.sort` - the document's sort position (when not sorting by relevance score)
441422
* `hits._score` - the document's relevance score (not applicable when using `match_all`)
442423

443-
Note that if `size` is not specified, it defaults to 10.
424+
Each search request is self-contained: {es} does not maintain any
425+
state information across requests. To page through the search hits, specify
426+
the `from` and `size` parameters in your request.
444427

445428
For example, the following request gets hits 10 through 19:
446429

@@ -462,65 +445,9 @@ GET /bank/_search
462445
Now that you've seen how to submit a basic search request, you can start to
463446
construct queries that are a bit more interesting than `match_all`.
464447

465-
This example does a `match_all` and sorts the results by account balance in descending order and returns the top 10 (default size) documents.
466-
467-
[source,js]
468-
--------------------------------------------------
469-
GET /bank/_search
470-
{
471-
"query": { "match_all": {} },
472-
"sort": { "balance": { "order": "desc" } }
473-
}
474-
--------------------------------------------------
475-
// CONSOLE
476-
// TEST[continued]
477-
478-
Now that we have seen a few of the basic search parameters, let's dig in some more into the Query DSL. Let's first take a look at the returned document fields. By default, the full JSON document is returned as part of all searches. This is referred to as the source (`_source` field in the search hits). If we don't want the entire source document returned, we have the ability to request only a few fields from within source to be returned.
479-
480-
This example shows how to return two fields, `account_number` and `balance` (inside of `_source`), from the search:
481-
482-
[source,js]
483-
--------------------------------------------------
484-
GET /bank/_search
485-
{
486-
"query": { "match_all": {} },
487-
"_source": ["account_number", "balance"]
488-
}
489-
--------------------------------------------------
490-
// CONSOLE
491-
// TEST[continued]
492-
493-
Note that the above example simply reduces the `_source` field. It will still only return one field named `_source` but within it, only the fields `account_number` and `balance` are included.
494-
495-
If you come from a SQL background, the above is somewhat similar in concept to the `SQL SELECT FROM` field list.
496-
497-
Now let's move on to the query part. Previously, we've seen how the `match_all` query is used to match all documents. Let's now introduce a new query called the {ref}/query-dsl-match-query.html[`match` query], which can be thought of as a basic fielded search query (i.e. a search done against a specific field or set of fields).
498-
499-
This example returns the account numbered 20:
500-
501-
[source,js]
502-
--------------------------------------------------
503-
GET /bank/_search
504-
{
505-
"query": { "match": { "account_number": 20 } }
506-
}
507-
--------------------------------------------------
508-
// CONSOLE
509-
// TEST[continued]
510-
511-
This example returns all accounts containing the term "mill" in the address:
512-
513-
[source,js]
514-
--------------------------------------------------
515-
GET /bank/_search
516-
{
517-
"query": { "match": { "address": "mill" } }
518-
}
519-
--------------------------------------------------
520-
// CONSOLE
521-
// TEST[continued]
522-
523-
This example returns all accounts containing the term "mill" or "lane" in the address:
448+
To search for specific terms within a field, you can use a `match` query.
449+
For example, the following request searches the `address` field to find
450+
customers whose addresses contain `mill` or `lane`:
524451

525452
[source,js]
526453
--------------------------------------------------
@@ -612,9 +539,15 @@ GET /bank/_search
612539
[[getting-started-aggregations]]
613540
== Analyze results with aggregations
614541

615-
Aggregations provide the ability to group and extract statistics from your data. The easiest way to think about aggregations is by roughly equating it to the SQL GROUP BY and the SQL aggregate functions. In Elasticsearch, you have the ability to execute searches returning hits and at the same time return aggregated results separate from the hits all in one response. This is very powerful and efficient in the sense that you can run queries and multiple aggregations and get the results back of both (or either) operations in one shot avoiding network roundtrips using a concise and simplified API.
542+
{es} aggregations enable you to get meta-information about your search results
543+
and answer questions like, "How many account holders are in Texas?" or
544+
"What's the average balance of accounts in Tennessee?" You can search
545+
documents, filter hits, and use aggregations to analyze the results all in one
546+
request.
616547

617-
To start with, this example groups all the accounts by state, and then returns the top 10 (default) states sorted by count descending (also default):
548+
For example, the following request uses a `terms` aggregation to group
549+
all of the accounts in the `bank` index by state, and returns the ten states
550+
with the most accounts in descending order:
618551

619552
[source,js]
620553
--------------------------------------------------
@@ -633,14 +566,10 @@ GET /bank/_search
633566
// CONSOLE
634567
// TEST[continued]
635568

636-
In SQL, the above aggregation is similar in concept to:
637-
638-
[source,sh]
639-
--------------------------------------------------
640-
SELECT state, COUNT(*) FROM bank GROUP BY state ORDER BY COUNT(*) DESC LIMIT 10;
641-
--------------------------------------------------
642-
643-
And the response (partially shown):
569+
The `buckets` in the response are the values of the `state` field. The
570+
`doc_count` shows the number of accounts in each state. For example, you
571+
can see that there are 27 accounts in `ID` (Idaho). Because the request
572+
set `size=0`, the response only contains the aggregation results.
644573

645574
[source,js]
646575
--------------------------------------------------
@@ -699,12 +628,11 @@ And the response (partially shown):
699628
--------------------------------------------------
700629
// TESTRESPONSE[s/"took": 29/"took": $body.took/]
701630

702-
We can see that there are 27 accounts in `ID` (Idaho), followed by 27 accounts
703-
in `TX` (Texas), followed by 25 accounts in `AL` (Alabama), and so forth.
704-
705-
Note that we set `size=0` to not show search hits because we only want to see the aggregation results in the response.
706631

707-
Building on the previous aggregation, this example calculates the average account balance by state (again only for the top 10 states sorted by count in descending order):
632+
You can combine aggregations to build more complex summaries of your data. For
633+
example, the following request nests an `avg` aggregation within the previous
634+
`group_by_state` aggregation to calculate the average account balances for
635+
each state.
708636

709637
[source,js]
710638
--------------------------------------------------
@@ -730,9 +658,8 @@ GET /bank/_search
730658
// CONSOLE
731659
// TEST[continued]
732660

733-
Notice how we nested the `average_balance` aggregation inside the `group_by_state` aggregation. This is a common pattern for all the aggregations. You can nest aggregations inside aggregations arbitrarily to extract pivoted summarizations that you require from your data.
734-
735-
Building on the previous aggregation, let's now sort on the average balance in descending order:
661+
Instead of sorting the results by count, you could sort using the result of
662+
the nested aggregation by specifying the order within the `terms` aggregation:
736663

737664
[source,js]
738665
--------------------------------------------------
@@ -761,54 +688,14 @@ GET /bank/_search
761688
// CONSOLE
762689
// TEST[continued]
763690

764-
This example demonstrates how we can group by age brackets (ages 20-29, 30-39, and 40-49), then by gender, and then finally get the average account balance, per age bracket, per gender:
765-
766-
[source,js]
767-
--------------------------------------------------
768-
GET /bank/_search
769-
{
770-
"size": 0,
771-
"aggs": {
772-
"group_by_age": {
773-
"range": {
774-
"field": "age",
775-
"ranges": [
776-
{
777-
"from": 20,
778-
"to": 30
779-
},
780-
{
781-
"from": 30,
782-
"to": 40
783-
},
784-
{
785-
"from": 40,
786-
"to": 50
787-
}
788-
]
789-
},
790-
"aggs": {
791-
"group_by_gender": {
792-
"terms": {
793-
"field": "gender.keyword"
794-
},
795-
"aggs": {
796-
"average_balance": {
797-
"avg": {
798-
"field": "balance"
799-
}
800-
}
801-
}
802-
}
803-
}
804-
}
805-
}
806-
}
807-
--------------------------------------------------
808-
// CONSOLE
809-
// TEST[continued]
691+
In addition to basic bucketing and metrics aggregations like these, {es}
692+
provides specialized aggregations for operating on multiple fields and
693+
analyzing particular types of data such as dates, IP addresses, and geo
694+
data. You can also feed the results of individual aggregations into pipeline
695+
aggregations for further analysis.
810696

811-
There are many other aggregations capabilities that we won't go into detail here. The {ref}/search-aggregations.html[aggregations reference guide] is a great starting point if you want to do further experimentation.
697+
The core analysis capabilities provided by aggregations enable advanced
698+
features such as using machine learning to detect anomalies.
812699

813700
[[getting-started-next-steps]]
814701
== Where to go from here

0 commit comments

Comments
 (0)