You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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).
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.
616
547
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
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.
706
631
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
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:
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:
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.
810
696
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.
0 commit comments